SQL fundamentals

The smallest complete SQL mental model for PLOMID.

Version
Latest
v0.1.0 · latest 1 min read
On this page
  1. The five statements you need first
  2. Clause order (fixed)
  3. Filtering, sorting, paginating
  4. Grouping and aggregating
  5. Joining
  6. NULL in one paragraph
  7. Next

The five statements you need first#

sqlsource
CREATE TABLE users (id BIGINT PRIMARY KEY, name TEXT NOT NULL);
INSERT INTO users VALUES (1, 'ada'), (2, 'grace');
SELECT id, name FROM users WHERE id > 1 ORDER BY id LIMIT 10;
UPDATE users SET name = 'ADA' WHERE id = 1;
DELETE FROM users WHERE id = 2;

Clause order (fixed)#

textsource
SELECT targets → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT → OFFSET

WHERE filters rows, GROUP BY + aggregates summarize, HAVING filters groups, ORDER BY sorts (with NULLS FIRST|LAST), LIMIT/OFFSET paginate. Full surface: SELECT.

Filtering, sorting, paginating#

sqlsource
SELECT * FROM orders WHERE total > 100 AND placed_at >= DATE '2026-01-01'
ORDER BY placed_at DESC NULLS LAST LIMIT 20 OFFSET 40;

LIMIT/OFFSET accept only integer literals or casts thereof (LIMIT 10, LIMIT '10'::int); anything else errors at parse time.

Grouping and aggregating#

sqlsource
SELECT user_id, count(*), sum(total), avg(total)
FROM orders GROUP BY user_id HAVING count(*) > 2 ORDER BY user_id;

Also: GROUPING SETS, ROLLUP, CUBE, FILTER (WHERE …), ORDER BY inside aggregates. See Aggregates.

Joining#

sqlsource
SELECT u.name, o.total FROM users u JOIN orders o ON o.user_id = u.id;

INNER/LEFT/RIGHT/FULL/CROSS, USING(a,b) (rewritten to L.a=R.a AND …), comma cross joins, lateral and table functions. See Joins.

NULL in one paragraph#

NULL = NULL is NULL, not true. WHERE keeps only true. Aggregates skip NULLs (COUNT(*) excepted). ORDER BY defaults: ASC → NULLS LAST, DESC → NULLS FIRST. Full contract: NULL semantics.

Next#

SELECT reference · Operators · Cookbook

Was this page helpful?