Code & computing / QUICK REFERENCE
SQL query essentials
Read, filter, join and summarize data without turning a reference lookup into an accidental database change.
Select and sort
- Choose columns
SELECT id, name FROM employees;- Distinct values
SELECT DISTINCT dept_id FROM employees;Removes duplicate result rows for the selected expression. [1][6]
- Sort ascending
SELECT id, name FROM employees ORDER BY name, id;- Sort descending
SELECT id, salary FROM employees ORDER BY salary DESC;- Limit a result
SELECT id, name FROM employees ORDER BY id LIMIT 10;- Offset a result
SELECT id FROM employees ORDER BY id LIMIT 10 OFFSET 10;Skips ten ordered rows; large offsets can be expensive. [1][6]
- Inspect a query plan
EXPLAIN SELECT id FROM employees WHERE id = 1;Plain EXPLAIN does not run the query like EXPLAIN ANALYZE does. [7]
Filter deliberately
- Numeric threshold
SELECT id FROM employees WHERE salary > 50000;- Combine conditions
SELECT id FROM employees WHERE dept_id = 2 AND salary > 50000;- Either condition
SELECT id FROM employees WHERE dept_id = 1 OR dept_id = 2;- Set membership
SELECT id FROM employees WHERE dept_id IN (1, 2);Equivalent to several equality alternatives for these literals. [1][6]
- Inclusive range
SELECT id FROM employees WHERE salary BETWEEN 40000 AND 60000;
Join and summarize
- Inner join
SELECT e.name, d.name FROM employees e JOIN departments d ON d.id = e.dept_id;- Left join
SELECT e.name, d.name FROM employees e LEFT JOIN departments d ON d.id = e.dept_id;Keeps every employee, including those without a matching department. [2][6]
- Find unmatched rows
SELECT e.id FROM employees e LEFT JOIN departments d ON d.id = e.dept_id WHERE d.id IS NULL;- Group totals
SELECT dept_id, COUNT(*) FROM employees GROUP BY dept_id;- Filter grouped results
SELECT dept_id, COUNT(*) FROM employees GROUP BY dept_id HAVING COUNT(*) >= 3;
Useful query patterns
- Fallback for NULL
SELECT name, COALESCE(salary, 0) FROM employees;The fallback is a presentation choice, not a claim that unknown pay is zero. [5][6]
- Conditional value
SELECT name, CASE WHEN salary >= 50000 THEN 'higher' ELSE 'other' END FROM employees;- Common table expression
WITH known AS (SELECT * FROM employees WHERE salary IS NOT NULL) SELECT COUNT(*) FROM known;- Rank within department
SELECT name, ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC, id) FROM employees;- Running total
SELECT id, SUM(salary) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM employees;- Keep rows while adding averages
SELECT name, AVG(salary) OVER (PARTITION BY dept_id) FROM employees;A window aggregate does not collapse rows like GROUP BY. [4][6]
- Combine and remove duplicates
SELECT dept_id FROM employees UNION SELECT id FROM departments;
Reserved placement, separate from reference controls. No ad network is active.
No matching entry. Try a shorter word or clear the search to see the complete sheet.
Sources
cheatsheetatlas.com/sheets/sql/ · Updated 2026-10-19 · SQL · PostgreSQL-oriented SELECT patterns