☼
⌃
☰

Structured Query Language

Table of Contents

Standard language for interacting with relational databases. Covers four sub-languages: DDL (structure), DML (data), DCL (permissions), and DQL (queries). Understanding valid SQL syntax is prerequisite to understanding SQL injection.

Related: Database SQL Injection

DDL — Data Definition Language

Defines and modifies database structure.

SQL
-- Create table
CREATE TABLE users (
    id       INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL UNIQUE,
    email    VARCHAR(255),
    role     VARCHAR(20) DEFAULT 'user',
    created  DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- Add column
ALTER TABLE users ADD COLUMN password_hash VARCHAR(255);

-- Create index
CREATE INDEX idx_username ON users(username);

-- Delete table
DROP TABLE users;

DML — Data Manipulation Language

Inserts, updates, and deletes data.

SQL
-- Insert
INSERT INTO users (username, email, role)
VALUES ('alice', 'alice@example.com', 'admin');

-- Update
UPDATE users SET role = 'user' WHERE id = 5;

-- Delete
DELETE FROM users WHERE id = 5;

-- Upsert (MySQL)
INSERT INTO users (id, username) VALUES (1, 'alice')
ON DUPLICATE KEY UPDATE username = 'alice';

DQL — Queries

Basic SELECT

SQL
-- All columns
SELECT * FROM users;

-- Specific columns
SELECT username, email FROM users;

-- Filter
SELECT * FROM users WHERE role = 'admin';

-- Multiple conditions
SELECT * FROM users WHERE role = 'admin' AND created > '2025-01-01';

-- Pattern match
SELECT * FROM users WHERE username LIKE 'a%';

-- Sort
SELECT * FROM users ORDER BY created DESC;

-- Limit
SELECT * FROM users LIMIT 10 OFFSET 20;

-- Distinct values
SELECT DISTINCT role FROM users;

Joins

SQL
-- INNER JOIN — only matching rows in both tables
SELECT u.username, o.total
FROM users u
INNER JOIN orders o ON u.id = o.user_id;

-- LEFT JOIN — all rows from left, matched rows from right (NULL if no match)
SELECT u.username, o.total
FROM users u
LEFT JOIN orders o ON u.id = o.user_id;

-- Multiple joins
SELECT u.username, o.total, p.name AS product
FROM users u
INNER JOIN orders o ON u.id = o.user_id
INNER JOIN products p ON o.product_id = p.id;

Aggregation

SQL
-- Count
SELECT COUNT(*) FROM users;
SELECT COUNT(*) FROM users WHERE role = 'admin';

-- Sum, average, min, max
SELECT SUM(total), AVG(total), MIN(total), MAX(total) FROM orders;

-- Group by
SELECT role, COUNT(*) AS count FROM users GROUP BY role;

-- Having (filter on aggregated values)
SELECT role, COUNT(*) AS count
FROM users
GROUP BY role
HAVING COUNT(*) > 5;

Subqueries

SQL
-- In WHERE clause
SELECT username FROM users
WHERE id IN (SELECT user_id FROM orders WHERE total > 100);

-- As derived table
SELECT u.username, order_counts.total_orders
FROM users u
JOIN (
    SELECT user_id, COUNT(*) AS total_orders FROM orders GROUP BY user_id
) AS order_counts ON u.id = order_counts.user_id;

DCL — Data Control Language

SQL
-- Grant permissions
GRANT SELECT, INSERT ON database.users TO 'appuser'@'localhost';

-- Grant all on a database
GRANT ALL PRIVILEGES ON mydb.* TO 'admin'@'%';

-- Revoke
REVOKE INSERT ON database.users FROM 'appuser'@'localhost';

-- Show current user's permissions
SHOW GRANTS;

Useful Administrative Queries

SQL
-- List all databases
SHOW DATABASES;                          -- MySQL
SELECT datname FROM pg_database;         -- PostgreSQL

-- List all tables
SHOW TABLES;                             -- MySQL
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public';           -- PostgreSQL

-- Describe table structure
DESCRIBE users;                          -- MySQL
\d users                                 -- PostgreSQL

-- List all users (MySQL)
SELECT user, host FROM mysql.user;

-- Current database
SELECT DATABASE();                       -- MySQL
SELECT current_database();               -- PostgreSQL

-- Version
SELECT VERSION();

Information Schema

Useful during SQL injection enumeration — the informationschema database exists in MySQL, PostgreSQL, and MSSQL and contains metadata about all tables, columns, and databases.

SQL
-- List all tables across all databases
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE';

-- List all columns in a specific table
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'users';

-- Find tables containing a specific column name
SELECT table_name
FROM information_schema.columns
WHERE column_name LIKE '%password%';

See SQL Injection for how these are used in enumeration payloads.

Tips

  • LIMIT 1 is commonly appended in SQL injection payloads to prevent errors from multi-row returns — understanding why requires knowing that many contexts expect exactly one value
  • -- and # are both comment delimiters — -- is standard SQL, # is MySQL-specific; this matters when crafting or understanding injection payloads
  • The information_schema is your map of the database during post-exploitation — knowing the table and column structure tells you where credentials and sensitive data live
  • MySQL runs as root by default on many misconfigured systems — INTO OUTFILE can write arbitrary files if permissions allow