SQL
The lingua franca of data. Query anything, anywhere.
⚠ pattern-based checks (code is analyzed, not compiled)
About SQL
SQL (Structured Query Language) is the standard language for relational databases. You'll use it with PostgreSQL (the engine behind Lovable Cloud / Supabase), SQL Server, MySQL, SQLite, and many more. NOTE: Your queries are validated by pattern matching, not executed against a real database. Focus on writing idiomatic, readable SQL. Keywords are case-insensitive in SQL — we accept both UPPER and lower case.
Quick-reference cheat sheet
-- SQL cheat-sheet
-- Read
SELECT col1, col2 FROM table WHERE col1 = 'x' ORDER BY col2 DESC LIMIT 10;
-- Aggregate
SELECT dept, COUNT(*), AVG(salary)
FROM employees
GROUP BY dept
HAVING COUNT(*) > 5;
-- Join
SELECT u.name, o.total
FROM users u
INNER JOIN orders o ON o.user_id = u.id;
-- Write
INSERT INTO users (name, email) VALUES ('Ada', 'ada@x.com');
UPDATE users SET email = 'a@x.com' WHERE id = 1;
DELETE FROM users WHERE id = 1;
-- DDL
CREATE TABLE notes (
id SERIAL PRIMARY KEY,
body TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
-- CTE + window
WITH ranked AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS rn
FROM employees
)
SELECT * FROM ranked WHERE rn = 1;
Tasks
- 01introHello, SELECTRun your first SELECT.
- 02introSELECT a stringReturn a text literal.
- 03introColumn alias with ASRename an output column.
- 04introSELECT * FROM tableRead every column of every row.
- 05introSelect specific columnsProject only what you need.
- 06introFilter with WHEREEqual to a value.
- 07introNumeric comparisonGreater-than filter.
- 08introAND / ORCombine conditions.
- 09introNOT operatorNegate a condition.
- 10introDISTINCT valuesDrop duplicate rows.
- 11easyORDER BYSort the result.
- 12easyORDER BY ... DESCNewest first.
- 13easyLIMIT nTop-N rows (Postgres / MySQL syntax).
- 14easyLIMIT + OFFSET (pagination)Page 3 with 10 per page.
- 15easyPattern match with LIKE% wildcard.
- 16easyWHERE ... IN (...)Match a set of values.
- 17easyBETWEEN rangeInclusive range filter.
- 18easyIS NULLFind missing values.
- 19easyCOUNT(*)How many rows?
- 20easyMIN / MAX / AVGThree aggregates in one query.
- 21easyGROUP BYAggregate per group.
- 22mediumGROUP BY + HAVINGFilter aggregated groups.
- 23easyString concatenationANSI || operator.
- 24easyLOWER / UPPERCase-insensitive matching trick.
- 25mediumINNER JOINMatch rows in two tables.
- 26mediumLEFT JOINKeep all rows from the left table.
- 27mediumRIGHT JOINMirror image of LEFT JOIN.
- 28mediumFULL OUTER JOINKeep both sides, NULLs where missing.
- 29mediumSelf joinManager / employee hierarchy.
- 30mediumCROSS JOINCartesian product.
- 31mediumSubquery with INFilter by a derived list.
- 32mediumEXISTSCorrelated existence check.
- 33mediumScalar subquery in SELECTOne value per row from another query.
- 34mediumUNION ALLStack two result sets.
- 35mediumCASE expressionInline if/else inside SELECT.
- 36mediumCOALESCE & NULLIFDefault values and NULL conversion.
- 37mediumCREATE TABLEDefine your first table.
- 38mediumINSERT a rowAdd data.
- 39mediumMulti-row INSERTInsert several rows in one statement.
- 40mediumUPDATE with WHEREAlways include a WHERE clause!
- 41mediumDELETE with WHERETargeted delete.
- 42mediumALTER TABLE — add columnSchema evolution.
- 43mediumForeign key constraintEnforce referential integrity.
- 44mediumUNIQUE + CHECKMultiple constraints in one column.
- 45mediumCREATE INDEXSpeed up lookups.
- 46mediumCREATE VIEWSave a query as a virtual table.
- 47hardWITH (Common Table Expression)Name a subquery for readability.
- 48hardWindow — ROW_NUMBER()Rank rows inside partitions.
- 49hardRANK vs DENSE_RANKTwo ways to break ties.
- 50hardRunning total with SUM() OVERCumulative aggregate.
- 51hardLAG / LEADCompare to previous / next row.
- 52hardTransactionsAll-or-nothing money transfer.
- 53hardUpsert (ON CONFLICT)INSERT or UPDATE in one shot (Postgres).
- 54hardJSON access (Postgres ->>)Read a key out of a JSONB column.
- 55hardRow Level Security (Postgres / Supabase)Lock down a table.
- 56hardTrigger to keep updated_at freshFunction + BEFORE UPDATE trigger.
- 57hardRecursive CTEWalk a hierarchy.
- 58hardEXPLAIN ANALYZEInspect a query plan.
- 59hardMERGE (SQL Server / Postgres 15+)Standard upsert with MERGE.
- 60hardCapstone — Blog schema with RLSBuild a complete table, index, trigger and policy.
