My solutions to SQL exercises, written while learning SQL for data analytics and engineering.
Each file includes the problem link, the table schema, and short notes on my approach.
- Filtering:
WHERE, BETWEEN, IN, LIKE with % / _ wildcards
- Sorting & pagination:
ORDER BY, LIMIT, OFFSET, DISTINCT
- Joins:
INNER, LEFT, CROSS and self-joins; anti-join pattern (LEFT JOIN ... WHERE key IS NULL)
- NULL handling:
IS NULL, COALESCE, why NULL != x is never TRUE, COUNT(*) vs COUNT(column)
- Aggregation:
COUNT, SUM, AVG, MAX, GROUP BY, HAVING vs WHERE
- Conditional aggregation:
AVG(CASE WHEN ... THEN 1 ELSE 0 END) for rates
- Expressions & functions: computed columns, aliases, integer vs decimal division,
ROUND, DATEDIFF, CHAR_LENGTH vs LENGTH
- Query execution order: why
SELECT aliases work in ORDER BY but not in WHERE
- Modifying data:
INSERT, UPDATE, DELETE (verify the WHERE with a SELECT first)
- Schema:
CREATE TABLE, ALTER TABLE, DROP TABLE, primary & foreign keys
| # |
Lesson |
Key concept |
| 01 |
SELECT queries |
Selecting columns |
| 02 |
Queries with constraints (Pt. 1) |
WHERE, BETWEEN |
| 03 |
Queries with constraints (Pt. 2) |
LIKE, wildcards |
| 04 |
Filtering & sorting |
DISTINCT, ORDER BY, LIMIT |
| 05 |
Simple SELECT review |
Combining clauses |
| 06 |
JOINs |
INNER JOIN |
| 07 |
OUTER JOINs |
LEFT JOIN |
| 08 |
NULLs |
IS NULL, anti-join |
| 09 |
Expressions |
Computed columns, aliases |
| 10 |
Aggregates (Pt. 1) |
GROUP BY |
| 11 |
Aggregates (Pt. 2) |
HAVING vs WHERE |
| 12 |
Order of execution |
Logical query order |
| 13 |
Inserting rows |
INSERT, primary keys |
| 14 |
Updating rows |
UPDATE, escaping quotes |
| 15 |
Deleting rows |
DELETE, orphan rows |
| 16 |
Creating tables |
CREATE TABLE, constraints |
| 17 |
Altering tables |
ALTER TABLE, defaults |
| 18 |
Dropping tables |
DROP TABLE |
- SQLBolt runs on SQLite and LeetCode on MySQL, so some solutions include comments on where
standard SQL (e.g. PostgreSQL) behaves differently.
- String literals use single quotes; columns are table-qualified in multi-table queries.