🛠️ ToolsPilot
← Back to ToolsPilot

SQL Cheat Sheet

SQL quick reference. Queries, joins, aggregation, and common functions. Bookmark this page or print it.

Basic Queries

SELECT * FROM tableSelect all
SELECT col1, col2 FROM tableSelect cols
SELECT DISTINCT col FROM tableUnique values
SELECT * FROM t WHERE col > 5Filter
SELECT * FROM t ORDER BY col DESCSort
SELECT * FROM t LIMIT 10 OFFSET 5Paginate

Joins

INNER JOINMatching rows
LEFT JOINAll left + matching right
RIGHT JOINAll right + matching left
FULL OUTER JOINAll rows both
CROSS JOINCartesian product
SELF JOINJoin to itself

Aggregation

COUNT(*) / COUNT(DISTINCT col)Count
SUM(col) / AVG(col)Sum / Avg
MIN(col) / MAX(col)Min / Max
GROUP BY colGroup rows
HAVING COUNT(*) > 5Filter groups

Modifying

INSERT INTO t (c1) VALUES (v1)Insert
UPDATE t SET c1 = v1 WHERE ...Update
DELETE FROM t WHERE ...Delete
CREATE TABLE t (id INT PRIMARY KEY)Create table
ALTER TABLE t ADD col TYPEAdd column
DROP TABLE tDelete table

Functions

COALESCE(a, b)First non-null
CASE WHEN x THEN y ELSE z ENDConditional
CAST(col AS TYPE)Type conversion
CONCAT(a, b)String concat
SUBSTRING(col, 1, 5)Substring
NOW() / CURRENT_DATECurrent date