SQL Course Outline: From First Query to Advanced SQL
A complete SQL course for students and working developers: queries, joins, aggregation, subqueries, window functions, data changes, types and functions, reporting, design, DDL, indexes, security, transactions, procedural SQL, performance and graded exercises.
kvi5hnuCode Sprout team member8 min readOn this page
This course teaches SQL from the first SELECT to the techniques experienced developers use every day: window functions, recursive queries, pivots, upserts, transactions, query plans and procedural code inside the database. It's written for two kinds of reader:
- Students and beginners who have never written a query. Part 1 starts from what a table is, and every article builds only on the ones before it.
- Working developers and analysts who use SQL already and want to fill gaps, understand why queries behave the way they do, or prepare for interviews. Each article has a "Common questions" section for the doubts people really have, and the delicate details are tested in the quizzes.
How the course works
- Standard SQL first, with dialect notes. The course teaches the SQL that works across products, and calls out the differences you'll meet in PostgreSQL, MySQL, SQL Server, Oracle and SQLite.
- Runnable examples. Most examples run on a real PostgreSQL 17 database in the page. Each run starts from an empty database and creates its own tables, so you can run them in any order. Article 3 explains how to read the output.
- One example business. The examples use a small fictional outdoor-gear shop called Trailhead, with customers, products, orders, employees and stores, so you can focus on the SQL instead of learning a new data model each time.
- Practice at every step. Every article ends with practice questions and answers, most parts end with a review quiz, and Part 15 is a set of graded exercises on one shop database.
What you'll learn
| Part | Topic | What it covers | Articles |
|---|---|---|---|
| 1 | Foundations | What a relational database is, how SQL is organized, the tools, and how a query is evaluated. | 1–4 |
| 2 | Querying one table | SELECT, WHERE, patterns, NULL, sorting, DISTINCT, row limits, pagination and CASE. | 5–11 |
| 3 | Joins and set operations | Combining tables: inner, outer, self and cross joins, set operators, semi- and anti-joins, and common join mistakes. | 12–17 |
| 4 | Aggregation | Summarizing data: aggregate functions, GROUP BY and HAVING, subtotals, and conditional aggregation. | 18–21 |
| 5 | Subqueries and CTEs | Queries inside queries, correlated subqueries, derived tables, LATERAL, and common table expressions including recursion. | 22–25 |
| 6 | Window functions | Calculations across related rows without collapsing them: ranking, running totals, moving averages, comparisons with neighbouring rows, and gaps and islands. | 26–30 |
| 7 | Changing data | INSERT, UPDATE, DELETE, MERGE and upserts, and getting changed rows back. | 31–34 |
| 8 | Data types and functions | Types and conversions, strings, numbers, dates and times, regular expressions and JSON. | 35–43 |
| 9 | Reporting and advanced queries | Pivoting, unpivoting, buckets and histograms, top-n and extreme values, hierarchies, and a collection of tricky problems. | 44–49 |
| 10 | Database design | Turning requirements into tables: entities, relationships and normalization. | 50–51 |
| 11 | Tables, indexes and views | Creating and changing schema objects: tables and constraints, keys, indexes, views and the system catalog. | 52–57 |
| 12 | Security and transactions | Users, roles and privileges, SQL injection, transactions, isolation levels, locking and MVCC. | 58–61 |
| 13 | Procedural SQL | Programming inside the database: PL/SQL and PL/pgSQL blocks, cursors and exceptions, procedures, functions, triggers, and dynamic SQL. | 62–66 |
| 14 | Performance | Reading execution plans, tuning queries, and the anti-patterns to avoid. | 67–68 |
| 15 | Practice | Graded exercise sets on one shop database, from first queries to interview-level challenges, with explained solutions. | 69–72 |
Part 1: Foundations
What a relational database is, how SQL is organized, the tools, and how a query is evaluated.
- Relational Databases Explained: Tables, Rows, Columns, Primary Keys and Foreign Keys
- The SQL Language: Statement Types, Standards and Dialects
- SQL Tools and How to Run This Course's Examples
- How a SQL Query Is Processed: The Logical Order of SELECT Clauses
Part 2: Querying one table
SELECT, WHERE, patterns, NULL, sorting, DISTINCT, row limits, pagination and CASE.
- SQL SELECT Basics: Choosing Columns, Expressions and Aliases
- Filtering Rows With WHERE: Comparison and Logical Operators
- SQL IN, BETWEEN and LIKE: Lists, Ranges and Pattern Matching
- NULL in SQL: Three-Valued Logic, IS NULL, COALESCE and NULLIF
- Sorting With ORDER BY: Multiple Keys, Expressions, NULLs and Custom Orders
- DISTINCT, LIMIT, FETCH FIRST and Pagination in SQL
- Conditional Logic in SQL With CASE Expressions
Review quiz: Quiz: Relational Basics and Single-Table Queries (SQL Parts 1 and 2)
Part 3: Joins and set operations
Combining tables: inner, outer, self and cross joins, set operators, semi- and anti-joins, and common join mistakes.
- SQL Inner Joins: Combining Rows From Related Tables
- SQL Outer Joins: LEFT, RIGHT and FULL JOIN
- Self Joins, Cross Joins and Non-Equi Joins in SQL
- SQL Set Operations: UNION, UNION ALL, INTERSECT and EXCEPT
- Finding Matches and Missing Rows: EXISTS, NOT EXISTS, IN and NOT IN
- SQL Join Pitfalls: Fan-Out, Duplicates and Comparing Two Tables
Review quiz: Quiz: Joins and Set Operations Review (SQL Part 3)
Part 4: Aggregation
Summarizing data: aggregate functions, GROUP BY and HAVING, subtotals, and conditional aggregation.
- SQL Aggregate Functions: COUNT, SUM, AVG, MIN and MAX
- GROUP BY and HAVING: Summarizing Groups of Rows
- Subtotals and Grand Totals With ROLLUP, CUBE and GROUPING SETS
- Conditional Aggregation in SQL: CASE Inside Aggregates and FILTER
Review quiz: Quiz: Aggregation Review (SQL Part 4)
Part 5: Subqueries and CTEs
Queries inside queries, correlated subqueries, derived tables, LATERAL, and common table expressions including recursion.
- SQL Subqueries: Scalar, IN, EXISTS, ANY and ALL
- Correlated Subqueries, Derived Tables and LATERAL Joins
- Common Table Expressions (WITH): Writing Readable SQL in Steps
- Recursive CTEs: Sequences, Hierarchies and Graph Walks
Review quiz: Quiz: Subqueries and CTEs Review (SQL Part 5)
Part 6: Window functions
Calculations across related rows without collapsing them: ranking, running totals, moving averages, comparisons with neighbouring rows, and gaps and islands.
- SQL Window Functions: OVER, PARTITION BY and ORDER BY
- Ranking in SQL: ROW_NUMBER, RANK, DENSE_RANK, NTILE and Top-N per Group
- Window Frames: Running Totals and Moving Averages With ROWS, RANGE and GROUPS
- LAG, LEAD, FIRST_VALUE and LAST_VALUE: Comparing Rows With Their Neighbours
- Gaps and Islands: Finding Consecutive Ranges and Missing Values
Review quiz: Quiz: Window Functions Review (SQL Part 6)
Part 7: Changing data
INSERT, UPDATE, DELETE, MERGE and upserts, and getting changed rows back.
- Inserting Rows With INSERT: Values, Defaults and INSERT ... SELECT
- Updating Rows With UPDATE: Expressions, Subqueries and Joins
- Deleting Rows: DELETE, TRUNCATE and Removing Duplicates
- MERGE, Upserts and RETURNING: Insert-or-Update in One Statement
Review quiz: Quiz: Changing Data Review (SQL Part 7)
Part 8: Data types and functions
Types and conversions, strings, numbers, dates and times, regular expressions and JSON.
- SQL Data Types: Numbers, Text, Dates, Booleans and CAST
- SQL String Functions: Length, Case, Trim, Substring, Replace and Collations
- String Techniques in SQL: Splitting, Aggregating and Extracting Text
- Numbers in SQL: Rounding, Division, Median, Mode and Percentiles
- Dates and Times in SQL: Types, Current Time, Parts, Formatting and Time Zones
- Date Arithmetic in SQL: Adding Intervals and Measuring Differences
- Working With Calendars in SQL: Month Boundaries, Date Series and Missing Dates
- Regular Expressions in SQL
- JSON in SQL: Storing, Querying and Building JSON
Review quiz: Quiz: Data Types and Functions Review (SQL Part 8)
Part 9: Reporting and advanced queries
Pivoting, unpivoting, buckets and histograms, top-n and extreme values, hierarchies, and a collection of tricky problems.
- Pivoting in SQL: Turning Rows Into Columns
- Unpivoting in SQL: Turning Columns Into Rows
- SQL Reporting Techniques: Buckets, Histograms, Shares and Running Percentages
- Advanced Searching in SQL: Top-N, Extremes, Skipping Rows and Previous Values
- Hierarchical Queries: Trees, Paths and Levels in SQL
- Tricky SQL Problems and How to Solve Them
Review quiz: Quiz: Reporting and Advanced Queries Review (SQL Part 9)
Part 10: Database design
Turning requirements into tables: entities, relationships and normalization.
- Database Design: Entities, Relationships and ER Diagrams
- Normalization: 1NF, 2NF, 3NF, BCNF and When to Denormalize
Part 11: Tables, indexes and views
Creating and changing schema objects: tables and constraints, keys, indexes, views and the system catalog.
- CREATE TABLE and Constraints: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK and DEFAULT
- Changing Tables: ALTER TABLE, DROP and Foreign Key Actions
- Generating Keys: Identity Columns, Sequences and UUIDs
- SQL Indexes: How They Work and When They Help
- SQL Views and Materialized Views
- Database Metadata: information_schema and the System Catalogs
Review quiz: Quiz: Database Design and Defining Tables Review (SQL Parts 10 and 11)
Part 12: Security and transactions
Users, roles and privileges, SQL injection, transactions, isolation levels, locking and MVCC.
- Users, Roles and Privileges: GRANT, REVOKE and Row-Level Security
- SQL Injection and Prepared Statements
- Transactions: COMMIT, ROLLBACK, Savepoints and ACID
- Isolation Levels, Locking and MVCC
Review quiz: Quiz: Security and Transactions Review (SQL Part 12)
Part 13: Procedural SQL
Programming inside the database: PL/SQL and PL/pgSQL blocks, cursors and exceptions, procedures, functions, triggers, and dynamic SQL.
- PL/pgSQL Basics: Blocks, Variables and Control Flow
- PL/pgSQL Cursors and Error Handling
- Stored Procedures and Functions
- SQL Triggers
- Dynamic SQL and Large Objects
Review quiz: Quiz: Procedural SQL Review (SQL Part 13)
Part 14: Performance
Reading execution plans, tuning queries, and the anti-patterns to avoid.
Review quiz: Quiz: Performance Review (SQL Part 14)
Part 15: Practice
Graded exercise sets on one shop database, from first queries to interview-level challenges, with explained solutions.
- SQL Exercises With Solutions: Beginner
- SQL Exercises With Solutions: Intermediate
- SQL Exercises With Solutions: Advanced
- SQL Exercises With Solutions: Challenge Set
After the course: final quizzes
- Quiz: SQL Course Final Exam (All 15 Parts)
- Quiz: SQL Interview Questions
- Quiz: SQL Delicate Details
- Quiz: SQL Dialect Differences (PostgreSQL, MySQL, SQL Server, Oracle, SQLite)
Start here: Relational Databases Explained: Tables, Rows, Columns, Primary Keys and Foreign Keys

More in SQL Course
Quiz: SQL Dialect Differences (PostgreSQL, MySQL, SQL Server, Oracle, SQLite)
Twenty-four questions on how the major SQL databases differ: limiting rows, concatenation, empty strings, identifier case, upserts, identity columns, transactional DDL, error handling, isolation defaults, booleans, set operations, RETURNING, dates and typing.
Quiz: SQL Delicate Details
Twenty-five questions on SQL's subtle corners: NULLs in aggregates, IN and CHECK, sort order of NULLs, rounding, CHAR padding, window frame defaults, identity gaps, LIKE escapes, empty groups, set operations and multi-column updates.
Quiz: SQL Interview Questions
Thirty SQL questions of the kind asked in developer and analyst interviews: WHERE vs HAVING, DELETE vs TRUNCATE, keys, joins, duplicates, Nth-highest values, window functions, ACID, isolation, locking, indexes, views, procedures and performance.
Quiz: SQL Course Final Exam (All 15 Parts)
The final exam for the SQL course: thirty questions across all fifteen parts, from query processing, NULLs, joins and aggregation through windows, data changes, types, design, indexes, security, transactions, procedural SQL and tuning.
Discussion
Sign in to ask a question or share what you learned.