SQL Cheat Sheet
Complete SQL reference: SELECT, JOINs, aggregation, subqueries, and functions.
Data Querying (SELECT)
SELECT * FROM table;Select all columnsSELECT col1, col2 FROM table;Select specific columnsSELECT DISTINCT col FROM table;Select unique valuesSELECT * FROM t WHERE col = val;Filter rows with WHERESELECT * FROM t WHERE col LIKE '%pat%';Pattern matching with LIKESELECT * FROM t WHERE col IN (1, 2, 3);Filter by list of valuesSELECT * FROM t WHERE col BETWEEN 10 AND 20;Filter by rangeSELECT * FROM t WHERE col IS NULL;Filter NULL valuesSELECT * FROM t ORDER BY col ASC;Sort results ascendingSELECT * FROM t ORDER BY col DESC;Sort results descendingSELECT * FROM t LIMIT 10;Limit number of rows returnedSELECT * FROM t LIMIT 10 OFFSET 20;Pagination — skip 20, return 10Aggregation
SELECT COUNT(*) FROM table;Count all rowsSELECT COUNT(col) FROM table;Count non-NULL valuesSELECT SUM(col) FROM table;Sum of column valuesSELECT AVG(col) FROM table;Average of column valuesSELECT MIN(col), MAX(col) FROM table;Minimum and maximumSELECT col, COUNT(*) FROM t GROUP BY col;Group and countSELECT col, COUNT(*) FROM t GROUP BY col HAVING COUNT(*) > 5;Filter groups with HAVINGJOINs
SELECT * FROM a INNER JOIN b ON a.id = b.a_id;Inner join — matching rows onlySELECT * FROM a LEFT JOIN b ON a.id = b.a_id;Left join — all rows from aSELECT * FROM a RIGHT JOIN b ON a.id = b.a_id;Right join — all rows from bSELECT * FROM a FULL OUTER JOIN b ON a.id = b.a_id;Full outer join — all rowsSELECT * FROM a CROSS JOIN b;Cartesian product of both tablesData Manipulation
INSERT INTO t (c1, c2) VALUES (v1, v2);Insert a rowINSERT INTO t (c1, c2) VALUES (v1, v2), (v3, v4);Insert multiple rowsUPDATE t SET col = val WHERE id = 1;Update specific rowsDELETE FROM t WHERE id = 1;Delete specific rowsDELETE 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 tableDROP TABLE t;Delete a tableALTER TABLE t ADD col INT;Add a columnALTER TABLE t DROP COLUMN col;Remove a columnALTER TABLE t RENAME TO new_name;Rename a tableCREATE INDEX idx ON t (col);Create an indexDROP INDEX idx;Remove an indexSubqueries & CTEs
SELECT * FROM t WHERE id IN (SELECT id FROM other);Subquery in WHERESELECT * 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 functionUseful Functions
COALESCE(col, default_val)Return first non-NULL valueNULLIF(a, b)Return NULL if a = b, else aCASE WHEN cond THEN val ELSE other ENDConditional expressionCAST(col AS VARCHAR)Type castingCONCAT(str1, str2)Concatenate stringsUPPER(col) / LOWER(col)Change caseTRIM(col)Remove leading/trailing whitespaceLENGTH(col)String lengthSUBSTRING(col, 1, 5)Extract substringNOW() / CURRENT_TIMESTAMPCurrent date and timeDATE_TRUNC('month', col)Truncate date to month (PostgreSQL)About This Tool
A comprehensive SQL quick-reference covering SELECT, INSERT, UPDATE, DELETE, JOINs, aggregation, and common functions.
Use the search box to instantly filter commands by keyword — ideal when you remember a fragment of a statement but not the full syntax.
How to Use
- Browse sections or type a keyword in the search box.
- The list filters instantly as you type.
- Use the SQL snippet directly in your query editor or terminal.
- Clear the search to see all sections again.
Use Cases
Useful for developers writing ad-hoc queries, students learning SQL syntax, and engineers doing code reviews or database migrations.
FAQ
- Does it cover PostgreSQL, MySQL, and SQLite? — Most syntax is ANSI SQL and works across major databases. Dialect-specific functions (e.g. DATE_TRUNC) are noted.
- Can I search for a specific command? — Yes — type any keyword, function name, or clause in the search box and the list filters instantly.
- Is the list exhaustive? — No. It covers the most common day-to-day SQL. Refer to your database's official docs for advanced features.