TL
Tool Lab
πŸ’°Donate
πŸ’°Donate

SQL Cheat Sheet

Complete SQL reference: SELECT, JOINs, aggregation, subqueries, and functions.

Data Querying (SELECT)

SELECT * FROM table;Select all columns
SELECT col1, col2 FROM table;Select specific columns
SELECT DISTINCT col FROM table;Select unique values
SELECT * FROM t WHERE col = val;Filter rows with WHERE
SELECT * FROM t WHERE col LIKE '%pat%';Pattern matching with LIKE
SELECT * FROM t WHERE col IN (1, 2, 3);Filter by list of values
SELECT * FROM t WHERE col BETWEEN 10 AND 20;Filter by range
SELECT * FROM t WHERE col IS NULL;Filter NULL values
SELECT * FROM t ORDER BY col ASC;Sort results ascending
SELECT * FROM t ORDER BY col DESC;Sort results descending
SELECT * FROM t LIMIT 10;Limit number of rows returned
SELECT * FROM t LIMIT 10 OFFSET 20;Pagination β€” skip 20, return 10

Aggregation

SELECT COUNT(*) FROM table;Count all rows
SELECT COUNT(col) FROM table;Count non-NULL values
SELECT SUM(col) FROM table;Sum of column values
SELECT AVG(col) FROM table;Average of column values
SELECT MIN(col), MAX(col) FROM table;Minimum and maximum
SELECT col, COUNT(*) FROM t GROUP BY col;Group and count
SELECT col, COUNT(*) FROM t GROUP BY col HAVING COUNT(*) > 5;Filter groups with HAVING

JOINs

SELECT * FROM a INNER JOIN b ON a.id = b.a_id;Inner join β€” matching rows only
SELECT * FROM a LEFT JOIN b ON a.id = b.a_id;Left join β€” all rows from a
SELECT * FROM a RIGHT JOIN b ON a.id = b.a_id;Right join β€” all rows from b
SELECT * FROM a FULL OUTER JOIN b ON a.id = b.a_id;Full outer join β€” all rows
SELECT * FROM a CROSS JOIN b;Cartesian product of both tables

Data Manipulation

INSERT INTO t (c1, c2) VALUES (v1, v2);Insert a row
INSERT INTO t (c1, c2) VALUES (v1, v2), (v3, v4);Insert multiple rows
UPDATE t SET col = val WHERE id = 1;Update specific rows
DELETE FROM t WHERE id = 1;Delete specific rows
DELETE FROM t;Delete all rows (keep table)
TRUNCATE TABLE t;Delete all rows fast (DDL)

Table & Schema

CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(100));Create a table
DROP TABLE t;Delete a table
ALTER TABLE t ADD col INT;Add a column
ALTER TABLE t DROP COLUMN col;Remove a column
ALTER TABLE t RENAME TO new_name;Rename a table
CREATE INDEX idx ON t (col);Create an index
DROP INDEX idx;Remove an index

Subqueries & CTEs

SELECT * FROM t WHERE id IN (SELECT id FROM other);Subquery in WHERE
SELECT * FROM (SELECT * FROM t LIMIT 5) sub;Subquery in FROM (derived table)
WITH cte AS (SELECT * FROM t) SELECT * FROM cte;Common Table Expression (CTE)
SELECT *, ROW_NUMBER() OVER (PARTITION BY col ORDER BY date) FROM t;Window function

Useful Functions

COALESCE(col, default_val)Return first non-NULL value
NULLIF(a, b)Return NULL if a = b, else a
CASE WHEN cond THEN val ELSE other ENDConditional expression
CAST(col AS VARCHAR)Type casting
CONCAT(str1, str2)Concatenate strings
UPPER(col) / LOWER(col)Change case
TRIM(col)Remove leading/trailing whitespace
LENGTH(col)String length
SUBSTRING(col, 1, 5)Extract substring
NOW() / CURRENT_TIMESTAMPCurrent date and time
DATE_TRUNC('month', col)Truncate date to month (PostgreSQL)