How to use the SQL Cheat Sheet
- 1. Search or browseType a word like the function or task you need, or pick a section.
- 2. CopyEach snippet has its own Copy button, so you can paste it straight into your editor.
- 3. DownloadSave the whole SQL cheat sheet as plain text or Markdown for offline revision.
What's in it
The sheet uses standard SQL that works in MySQL 8, PostgreSQL and SQLite, noting where databases differ. The 36 snippets are organised into 13 sections: Query, Aggregate, Joins, Subqueries, CTE, Window, Functions, Modify, Schema, Transactions, Set ops, Classic, Order.
Every complete statement in the sheet (18 of them) was run against SQLite with a sample schema, so the syntax is valid.
Example
Search "second" to get the classic second-highest-salary query, and "duplicates" for the GROUP BY … HAVING COUNT(*) > 1 pattern. The last snippet lists the logical order a query runs in, FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT, which explains why you can't use a SELECT alias in WHERE.
Tips for using it
- Practise each query in the SQL online compiler with your own small table.
- Always write the WHERE clause first when you write an UPDATE or DELETE.
A quick look, one snippet per section
- Query:
SELECT name, marks FROM students WHERE marks >= 60 ORDER BY marks DESC;(filter and sort) - Aggregate:
SELECT dept, COUNT(*), AVG(salary) FROM emp GROUP BY dept;(group and aggregate) - Joins:
SELECT o.id, c.name FROM orders o JOIN customers c ON c.id = o.customer_id;(inner join) - Subqueries:
SELECT * FROM emp WHERE salary > (SELECT AVG(salary) FROM emp);(compare with an aggregate) - CTE:
WITH top AS (SELECT * FROM sales WHERE amt > 1000) SELECT COUNT(*) FROM top;(named temporary result) - Window:
ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC)(rank within each group) - Functions:
COALESCE(phone, 'N/A')(first non-null) - Modify:
INSERT INTO students (name, marks) VALUES ('Asha', 91);(insert a row) - Schema:
CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE);(create a table with constraints)
Search the box above for the rest of the SQL snippets, or download the full sheet.
Frequently asked questions
Will these work in SQL Server?
Mostly. SQL Server uses TOP or OFFSET … FETCH instead of LIMIT.
Were the queries checked?
Yes. Every complete statement was run against SQLite with a sample schema.
Is the SQL cheat sheet free?
Yes. You can search, read and copy every snippet in the SQL cheat sheet without signing up. The first time you download it, we ask once for your name and WhatsApp number; after that downloads and every other Uncodemy tool open straight away.