SQL
Ask questions of data: select, filter, join, and summarize.
Learning plan overview
38 lessonsabout 5 hr 40 min of readingPractice in every lesson
One path from your first SELECT to the patterns analysts use every day. Every lesson uses the same small sales database and ends with a query you write and check right on the page, so there is nothing to install.
What you'll be able to do
- Pull exactly the rows and columns you need
- Turn raw rows into totals, averages and rates a report can use
- Join tables without losing rows or counting them twice
- Answer ranking, trend and "compared to last month" questions
- Build tables and change data safely
The path
- 1BeginnerSelecting, filtering, sorting, NULLs, totals and groups, your first joins, and changing rows.
- 2IntermediateCASE logic, percentages, dates and text, subqueries and CTEs, EXISTS, and the joins that multiply rows.
- 3AdvancedWindow functions, ranking, running totals, medians, streaks, pivots, and building tables.
Beginner
Start here. No experience needed.
- Picking columns with SELECTThe first query everyone writes, and the habits that make it a good one.6 min
- Filtering rows with WHEREReturn only the rows that match a condition.7 min
- How a query actually runsSQL is written in one order and run in another. Knowing both explains most beginner errors.6 min
- Ranges, lists and patternsBETWEEN, IN and LIKE make WHERE clauses shorter and easier to read.7 min
- Missing values and NULLNULL means unknown. It never equals anything, and it's behind most wrong counts.7 min
- Sorting and limiting resultsORDER BY sorts the rows you get back. LIMIT keeps only the first few.5 min
- DISTINCT and aliasesRemove duplicate rows, and give columns and tables short, clear names.6 min
- Comments and commenting out codeLeave notes for the next reader, and switch lines off to debug a query one piece at a time.5 min
- Data typesA column's type decides what it can hold and how it compares, sorts and adds up.8 min
- Calculations and roundingArithmetic row by row, and the four traps behind most wrong numbers in reports.8 min
- Counting and totalingCOUNT, SUM, AVG, MIN and MAX turn many rows into one number.9 min
- Grouping with GROUP BYOne row per account, per rep or per month, with totals for each.8 min
- Filtering groups with HAVINGWHERE filters rows before grouping. HAVING filters the groups afterwards.6 min
Intermediate
For when the basics feel comfortable.
- Joining tables with INNER JOINCombine rows from two tables by matching an ID, and know what one row of the result means.9 min
- Keeping every row with LEFT JOINKeep all rows from one table, find what's missing, and pick the right join for the question.9 min
- Adding, changing and removing rowsINSERT, UPDATE and DELETE, plus the one habit that stops you deleting the wrong data.7 min
- If-then logic with CASELabel rows, build your own groups, sort in a custom order and count only the rows you want.10 min
- Ratios, percentages and ratesTurn a business question into a formula, and dodge the traps behind most wrong report numbers.12 min
- Filling in and creating NULLsCOALESCE swaps a missing value for one you choose. NULLIF turns a value you don't want into NULL.8 min
- Converting types with CASTTurn text into numbers, numbers into text, and know what each database does when a value won't convert.8 min
- Working with datesFilter, group, shift and measure dates, and avoid the timestamp traps that make month-end numbers wrong.11 min
- Cleaning and splitting textChange case, trim spaces, join columns together and pull pieces out of text.10 min
- Queries inside queriesUse one query's answer inside another, as a single value, a list, or a whole table.10 min
- Breaking a query into steps with WITHName each step of a hard question, then read the query top to bottom like a recipe.10 min
- Stacking and comparing resultsUNION stacks two results into one. INTERSECT and EXCEPT find what two results share, or what one has that the other doesn't.9 min
- Checking for matches with EXISTSAsk "is there at least one?" or "are there none?" without duplicating rows, and dodge the NOT IN trap.10 min
- Full outer, self and cross joinsThree joins for special jobs, reconciling two lists, matching a table to itself and building every combination.10 min
- When joins multiply rowsWhy totals come out too high after a join, how to spot it, and the aggregate-first pattern that fixes it.10 min
Advanced
Deeper patterns for real-world work.
- Totals beside every row with OVERWindow functions calculate across a group of rows but keep every row, so you can compare each one to its group.11 min
- Ranking rows and top N per groupNumber rows within each group with ROW_NUMBER, RANK and DENSE_RANK, then keep the top few or remove duplicates.11 min
- Comparing with the previous row using LAG and LEADLook one row back or ahead, to measure change between periods or time spent between events.10 min
- Running totals and moving averagesAdd ORDER BY and a frame inside OVER to build year-to-date totals and smooth out bumpy numbers.11 min
- Medians, percentiles and bucketsWhen a few big values skew the average, use the median, percentiles and NTILE buckets for a more honest picture.10 min
- Recursive CTEs and date spinesA CTE that refers to itself, for generating every date in a range and walking hierarchies of any depth.12 min
- Streaks and gapsGroup consecutive rows into streaks, and find the gaps between them, with one subtraction trick.11 min
- Turning rows into columnsPivot with SUM and CASE to put categories side by side, and unpivot with UNION ALL to stack them back.10 min
- Creating tables with keys and rulesDefine a table's columns, types and constraints, build tables and views from queries, and know which rules your database actually enforces.12 min
- Transactions and upsertsMake several changes succeed or fail together, and insert-or-update rows in one statement.10 min