mahir_data

Free SQL tutorial

Learn SQL, clause by clause

26 short chapters from your first SELECT to window functions, grouped Basic → Intermediate → Advanced. Every example is a live editor: read a little, run it, change it, see what happens. When a concept clicks, jump straight into the matching interview questions.

Basic

13 chapters

Single-table fundamentals: read, filter, sort, and reshape data.

  1. 01Start here: SQL & this tutorial4 minWhat SQL is, how these hands-on lessons work, and the one thing to know about the database underneath.
  2. 02SELECT: reading data6 minPull columns out of a table, the one statement every query starts with.
  3. 03WHERE: filtering rows6 minKeep only the rows you care about, using a condition.
  4. 04NULL: the three-valued logic trap5 minNULL means 'unknown' and it breaks the comparisons you'd expect to just work.
  5. 05AND, OR, NOT: combining conditions6 minChain multiple conditions together, and flip one on its head with NOT.
  6. 06IN & BETWEEN: matching sets and ranges6 minTwo shortcuts that make filtering against a list of values or a numeric range much easier to read.
  7. 07LIKE: pattern matching text6 minSearch inside text with wildcards: % for any run of characters, _ for exactly one.
  8. 08ORDER BY & LIMIT: top-N6 minSort results and keep only the top few, the pattern behind every leaderboard.
  9. 09Arithmetic operators & precedence6 minCompute derived columns with +, -, *, / and watch out for SQL's biggest number gotcha: integer division.
  10. 10Math functions: ROUND, ABS, CEIL, FLOOR6 minRound, floor, ceiling, and absolute value: the functions that clean up numbers for display and billing.
  11. 11CASE WHEN: conditional logic7 minTurn raw values into labels: tiers, statuses, categories, right inside a SELECT.
  12. 12String functions7 minClean up, combine, and search text: TRIM, UPPER/LOWER, CONCAT, and SUBSTRING.
  13. 13Date & time functions7 minTruncate, extract, and do arithmetic on dates: the backbone of every time-series report.

Intermediate

7 chapters

Summarize data and combine it across tables.

  1. 14Aggregate functions: SUM, AVG, COUNT, MIN, MAX7 minSqueeze many rows into one number, and know exactly which COUNT you need.
  2. 15GROUP BY: one row per group6 minRun an aggregate once per category instead of once for the whole table.
  3. 16HAVING: filtering groups, not rows5 minWHERE filters before grouping; HAVING filters after, and only HAVING can test an aggregate.
  4. 17NULL, deeper: COALESCE & NULLIF7 minNULL poisons arithmetic and comparisons; here's how to catch it, fall back gracefully, and count around it.
  5. 18JOINs: combining tables10 minStitch two tables together on a shared key, and see what LEFT JOIN keeps that INNER JOIN drops.
  6. 19Self-joins & joining 3+ tables8 minJoin a table to itself for hierarchies, and chain multiple JOINs when data spans more than two tables.
  7. 20Set operations: UNION, INTERSECT, EXCEPT6 minCombine, intersect, or diff the results of two queries with the same shape.

Advanced

6 chapters

Multi-step reasoning: the questions that separate candidates.

  1. 21Query execution order8 minThe order you type clauses isn't the order SQL runs them, and that gap explains half of SQL's weird rules.
  2. 22Subqueries & CTEs9 minUse the result of one query inside another, and name intermediate steps with WITH.
  3. 23Window functions11 minRank rows, number them, and compute running totals, without collapsing the table.
  4. 24LAG, LEAD & NTILE8 minCompare a row to its neighbors, and bucket rows into equal-sized groups.
  5. 25Pivoting: rows into columns8 minPlain SQL has no PIVOT keyword; here's the CASE + GROUP BY pattern that does the same job.
  6. 26Writing clean, maintainable SQL7 minTwo queries can return the same rows and still be very different to read six months later.

Prefer to learn by doing? Skip ahead to the question bank any time, the lessons are here whenever you get stuck.