After this lesson, you will be able to: Use SELECT, FROM, WHERE, ORDER BY, LIMIT to query tables; INSERT, UPDATE, DELETE to mutate them.
The four basic SQL operations cover 90% of daily work. Get these clean and the rest builds easily.
Read data from tables.
-- EverythingSELECT * FROM users;-- Specific columnsSELECT id, email, name FROM users;-- With aliasSELECT name AS user_name FROM users;-- FilteringSELECT * FROM users WHERE created_at >= '2026-01-01';SELECT * FROM posts WHERE published = TRUE;-- SortingSELECT * FROM users ORDER BY created_at DESC;SELECT * FROM posts ORDER BY published DESC, created_at DESC;-- LimitingSELECT * FROM users LIMIT 10;SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 20; -- page 3 of 10
Mutating data.
-- INSERT-- Multi-row INSERTINSERT INTO users (email, name) VALUES-- INSERT and return the row (Postgres-specific, very handy)RETURNING id, email;-- UPDATE, ALWAYS include WHERE or you'll update every rowUPDATE users SET name = 'Alex Chen' WHERE id = 1;UPDATE posts SET published = TRUE WHERE user_id = 1 AND published = FALSE;-- DELETE, same warning, WHERE mattersDELETE FROM posts WHERE published = FALSE AND created_at < '2025-01-01';-- Truncate empties a whole table fast (no WHERE)TRUNCATE TABLE temp_logs;
`SELECT *` in production code (returns more than you need; breaks when schema changes). Forgetting WHERE on UPDATE/DELETE (immortalized disaster). Using OFFSET for big paginations (slow, use keyset pagination instead). Sorting on unindexed columns at scale (full table scans).
Sign in and purchase access to unlock this lesson.