Foundations

SQL Simplified

SELECT, JOINs, GROUP BY and window functions explained one small table at a time.

  • Handwritten-style
  • Diagrams and tables
  • Worked examples

Who this notebook is for

Beginners learning SQL for the first time, and working analysts who want the gaps filled before a data engineering interview.

What you will be able to do

  • Write SELECT, WHERE, GROUP BY and JOIN queries and predict their output
  • Handle NULLs correctly instead of getting silently wrong counts
  • Use CTEs, subqueries and window functions for ranking and running totals
  • Read a query plan and know why an index or a rewrite makes a query faster

What's inside

Pick a chapter and the notebook opens right there.

  1. Relational FoundationsRDBMS Engine Architecture, Primary/Foreign Keys & ConstraintsStart here
  2. DDL, DML & ACIDCREATE/ALTER/DROP, TRUNCATE vs DELETE, Transactions & IsolationStart here
  3. Retrieval & NULL LogicLogical Query Order, 3-Valued Logic (3VL), NULL Pitfalls & Truth TablesStart here
  4. Patterns, Sort & PaginationLIKE, REGEXP, ORDER BY NULLS FIRST/LAST, Keyset CursorsStart here
  5. Grouping & AggregationsCOUNT(*) vs COUNT(col), GROUP BY Pipeline & Single- Value RuleStart here
  6. Advanced GroupingHAVING vs WHERE boundary, GROUPING SETS, ROLLUP & CUBEStart here
  7. Core JoinsINNER, LEFT, RIGHT, FULL OUTER, CROSS joins & Multi-table GraphsStart here
  8. Complex Joins & EnginesSELF Joins, Non-Equi Joins, Nested Loop, Hash & Merge JoinsStart here
  9. SubqueriesScalar, Column, Correlated Subqueries, EXISTS vs IN & Derived TablesStart here
  10. CTEs & RecursionWITH Common Table Expressions, Recursive Trees & Date SequencesStart here
  11. Window Functions (Ranking)OVER, PARTITION BY, ROW_NUMBER, RANK, DENSE_RANK, NTILEStart here
  12. Window Navigation & FramesLEAD, LAG, FIRST_VALUE, ROWS BETWEEN vs RANGE BETWEENStart here
  13. Built-in FunctionsText parsing (SUBSTRING, TRIM), Date arithmetic & Math roundingsStart here
  14. Conditionals & UPSERTCASE expressions, Pivoting, COALESCE, NULLIF & ON CONFLICTStart here
  15. Indexing StructuresB-Trees, Clustered vs Secondary Indexes, Composite Leftmost RuleStart here
  16. Query TuningReading EXPLAIN plans, Cost models, Sargability & Optimizer statisticsStart here
  17. Database Design1NF, 2NF, 3NF, BCNF Normalization, ER Modeling & Foreign Key cascadesStart here
  18. FAANG Interview Part INth Highest Salary, Consecutives, Retention & Self-joinsStart here
  19. FAANG Interview Part IIGaps & Islands, Medians, Churn, Trees & Market BasketStart here
  20. Modern Analytical SQL & VectorsQUALIFY, PIVOT/UNPIVOT, HyperLogLog, pgvector & 12 CommandmentsStart here
  21. Gap-Fill SupplementLATERAL/CROSS APPLY, row-level security & SQL injection, CTE materialization/hints/TABLESAMPLE, write skew & shardingStart here

Practice this

Put it to work on LakeBench.

Read the guide first

Short explainers that pair with this notebook.

Common questions

Which SQL dialect do the notes use?
They teach standard SQL ideas that carry across databases. Examples use small tables so you can follow each result by eye.
Does it cover interview questions?
Yes. The last chapters are interview-style problems, and the SQL Scenarios notebook goes much deeper on patterns such as gaps and islands and sessionization.
Can I read a sample before I commit?
Yes. The contents pages and the first pages of every notebook can be read online, so you can judge the style before going further.

More in Foundations

All notebooks