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)

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

  1. Browse sections or type a keyword in the search box.
  2. The list filters instantly as you type.
  3. Use the SQL snippet directly in your query editor or terminal.
  4. 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.