Readable query patterns for selecting, filtering and joining data. Examples use PostgreSQL-compatible syntax.
Write a query whose result you can explain
Start a SQL query by describing the rows you want, the columns you need and the order a reader should see. An explicit column list makes a report more stable than selecting everything, while a filter should express the intended condition without depending on presentation order. The examples below teach query structure; this reference does not connect to a database or execute statements.
Use a small, invented dataset when reasoning about a new query. Include duplicate names, missing values and an empty result so a query that works on one convenient row is not mistaken for a complete solution. Syntax and date functions vary between database engines, so check your engine's documentation before using a statement in a real application.
Choose only the columns needed by the task. Keep unrelated personal or internal fields out of an export.
Write the WHERE condition and check how it treats NULL values. Use IS NULL for a missing value rather than equality with NULL.
Add an ORDER BY clause when order matters. Include a tie-breaker if several rows can share the primary sort value.
For application input, use the database driver's parameter mechanism instead of assembling SQL by concatenating user text.
Make a sorted stock list explicit
Example input
SELECT sku, quantity
FROM inventory
WHERE quantity > 0
ORDER BY quantity DESC, sku ASC;
Expected result
A17 · 8
B04 · 3
For an invented table containing A17 with quantity eight, B04 with three and C09 with zero, the query returns the two positive-stock rows in the shown order. The secondary sku ordering makes equal quantities predictable. Changing the filter or data changes the result; the reference table is a worked explanation, not a live database response.
Check before you continue
UPDATE and DELETE statements can affect every row when their filter is missing or wrong. Inspect the matching rows and use the safeguards supported by your database workflow.
A formatter only changes source presentation. It does not execute a query, verify table names or establish that an operation is safe.
Literal INSERT exports are not a replacement for parameterized application queries, database permissions or a tested import process.
8 of 8 entries
Read
Select columns
SELECT id, name FROM customers;
Name the columns you need rather than relying on SELECT *.
Read
Filter rows
SELECT id, name FROM customers WHERE active = TRUE;
WHERE keeps rows for which the condition is true.
Read
Sort and limit
SELECT id, created_at FROM orders ORDER BY created_at DESC, id DESC LIMIT 20;
Use a stable tie-breaker when selecting a limited ordered result.
Read
Handle NULL
SELECT id FROM customers WHERE phone IS NULL;
NULL represents missing or unknown data. Use IS NULL rather than = NULL.
Aggregate
Group and count
SELECT country, COUNT(*) FROM customers GROUP BY country;
Aggregation combines rows into one result per group.
Aggregate
Filter groups
SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id HAVING COUNT(*) > 2;
HAVING filters after grouping; WHERE filters before grouping.
Join
Left join
SELECT c.name, o.id FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.id;
A left join retains customers without matching orders.
Structure
Common table expression
WITH recent AS (SELECT * FROM orders WHERE created_at >= DATE '2026-11-01') SELECT COUNT(*) FROM recent;
A CTE names a query expression within a statement. Optimizer behavior varies.