Master SQL Query & Database Commands Cheat Sheet
Essential SQL database cheat sheet: SELECT, JOINS, GROUP BY, indexes, transactions, and performance profiling with 1-click copy.
Querying Data (SELECT) (6 commands)
SELECT * FROM users;
Retrieves all rows and columns from the specified table.
SELECT DISTINCT country FROM customers;
Filters out duplicate values and returns unique entries only.
SELECT id, email, status FROM accounts WHERE status = 'active' AND created_at >= '2026-01-01';
Filters rows according to one or more boolean criteria.
SELECT * FROM articles WHERE slug LIKE '%seo%';
Matches text using wildcards (% for zero or more, _ for single character).
SELECT name, price FROM products ORDER BY price DESC, name ASC;
Sorts rows in ascending (ASC) or descending (DESC) order by multiple columns.
SELECT * FROM audit_logs ORDER BY created_at DESC LIMIT 20 OFFSET 40;
Skips 40 rows and returns the subsequent 20 records (Page 3).
Aggregations & Grouping (3 commands)
SELECT COUNT(*) AS total_users FROM users WHERE verified = true;
Returns the total number of matching rows.
SELECT department_id, COUNT(*) AS headcount, AVG(salary) AS avg_sal FROM employees GROUP BY department_id;
Groups rows sharing common values into summary rows.
SELECT customer_id, SUM(total_amount) AS total_spent FROM orders GROUP BY customer_id HAVING SUM(total_amount) > 1000;
Filters aggregated grouped results (unlike WHERE, which filters before aggregation).
Table Joins (3 commands)
SELECT u.name, o.id, o.amount FROM users u INNER JOIN orders o ON u.id = o.user_id;
Returns only records where the join key matches in both tables.
SELECT u.name, o.id AS order_id FROM users u LEFT JOIN orders o ON u.id = o.user_id;
Preserves all records from the left table even if right table has no matches (nulls).
SELECT o.id, u.email FROM users u RIGHT JOIN orders o ON u.id = o.user_id;
Preserves all records from the right table regardless of left table presence.
Data Modification (DML) (4 commands)
INSERT INTO products (name, sku, price, in_stock) VALUES ('Cloud CDN', 'CDN-01', 19.99, true);
Inserts one or more new rows into a database table.
UPDATE accounts SET tier = 'pro', updated_at = NOW() WHERE id = 142;
Modifies existing table data matching the predicate.
DELETE FROM sessions WHERE expires_at < NOW();
Removes records matching condition. Always include WHERE to prevent wiping table!
INSERT INTO user_settings (user_id, dark_mode) VALUES (42, true) ON CONFLICT (user_id) DO UPDATE SET dark_mode = EXCLUDED.dark_mode;
Inserts new row or updates existing row if unique constraint fails.
Schema & Indexing (DDL) (3 commands)
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
Creates a new table with primary key, unique constraint, and default timestamp.
CREATE INDEX idx_orders_user_id ON orders(user_id);
Creates a B-Tree index on foreign key or frequently filtered column for $O(\log n)$ query lookup.
EXPLAIN ANALYZE SELECT * FROM transactions WHERE account_id = 9012 ORDER BY created_at DESC;
Outputs query execution plan, index scan vs seq scan, and exact millisecond timing.