| Part index | Prerequisites | Next | Full course |
๐๏ธ The Complete SQL Guide โ MSSQL & MySQL
From โwhat is a table?โ to โI can design, tune, secure, and scale a real database.โ
By Gehan Fernando
๐ Start here ย ยทย ๐ Contents ย ยทย ๐จ Box legend ย ยทย ๐ Glossary
๐ What this guide is
A complete, self-contained course in SQL, written twice for every single topic: once for Microsoft SQL Server (its version of SQL is called T-SQL) and once for MySQL.
Every concept is explained as if to someone who has never seen a database before, and then demonstrated with code from a real online store you can run today.
Nothing is assumed. Nothing is skipped. Nothing is left as โyouโll figure it out.โ
๐ฏ Who this guide is for
| You areโฆ | What this guide gives you |
|---|---|
| ๐ฑ A complete beginner | Plain-English explanations with zero jargon. Start at Chapter 1 and never feel lost. |
| ๐ A student | Every exam and interview topic, with practice exercises and a capstone project. |
| ๐จโ๐ป A developer | The queries, joins, window functions, and anti-patterns you use daily in both engines. |
| ๐๏ธ A senior engineer | Execution plans, indexing strategy, isolation levels, partitioning, and design trade-offs. |
| ๐ก๏ธ A DBA (database administrator) | Security, backup/restore, replication, monitoring, and maintenance for both platforms. |
| ๐ An analyst | Aggregations, pivots, ranking, cohort and running-total recipes that answer business questions. |
[!NOTE]
- ๐งฐ What you need: SQL Server 2019 or newer (the free Developer or Express edition is fine) and/or MySQL 8.4 LTS. You can start with just one of them.
- โ Verification: see What was verified, and what was not below โ it states precisely which scripts were not executed, and what that means for you.
- ๐ฏ Goal: take you from โI donโt know what a row isโ to โI can design, query, speed up, secure, and run a real database.โ
- โฑ๏ธ How long: about 35 focused days if you follow the plan in Chapter 66.
- ๐ MySQL version note: MySQL 8.0 reached end of life in April 2026. Install 8.4 LTS. Scripts here generally work on 8.0.31+, but new installs should not start on an unsupported release.
๐ Start here โ your first hour
New to databases? Do these four steps in this order. Each one takes about 15 minutes.
| Step | What to do | Why | What you should see at the end |
|---|---|---|---|
| 1 | Read Chapter 1 and Chapter 2 | Learn the handful of words everything else uses | You can explain what a table, a row, and a query are |
| 2 | Install one database and one tool โ Chapter 3 | You need somewhere to run SQL | A window where you can type SQL, connected to your own database server |
| 3 | Build the practice shop database โ Chapter 4 | Every example in this guide uses it | The row counts in section 4.6 match the guide exactly |
| 4 | Run your first real questions โ Chapter 13 | This is the moment SQL starts to make sense | Real customers and products from the shop appear on your screen |
[!TIP] You only need one engine to begin. Choose SQL Server if you use Windows or work with Microsoft products. Choose MySQL if you build websites or use a Mac or Linux. Every lesson shows both, so you can add the other one later.
๐งญ The learning path
๐ฑ BEGINNER ๐ INTERMEDIATE ๐ EXPERT
โโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ
โ 1 Foundations โ โ 6 Combining tables โ โ 10 Performance โ
โ 2 Building blocks โ โโโโบ โ 7 Analytics โ โโโบ โ 11 Design โ
โ 3 CRUD โ โ 8 Database objects โ โ 12 Admin & security โ
โ 4 Functions โ โ 9 Transactions โ โ 13 Modern & expert โ
โ 5 Grouping โ โ โ โ 14 Engineering โ
โโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ
Ch 1โ27 ยท ~12 days Ch 28โ45 ยท ~10 days Ch 46โ66 ยท ~10 days
you can now you can now you can now
query one table answer real business make it fast, safe,
and build a schema questions across tables and production-ready
| Part | Chapters | Level | What you unlock |
|---|---|---|---|
| 1 ยท Foundations | 1โ5 | ๐ข | Understand what a database is and how a query runs |
| 2 ยท Building blocks | 6โ11 | ๐ข | Design and create tables that protect their own data |
| 3 ยท CRUD (create, read, update, delete) | 12โ19 | ๐ข | Read and change data safely |
| 4 ยท Functions | 20โ24 | ๐ข | Turn raw data into readable output |
| 5 ยท Grouping | 25โ27 | ๐ข๐ก | Summarize millions of rows into answers |
| 6 ยท Combining tables | 28โ32 | ๐ก | Joins โ the skill that separates beginners from users |
| 7 ยท Analytics | 33โ35 | ๐ก | Window functions โ ranking, running totals, trends |
| 8 ยท Database objects | 36โ41 | ๐ก | Views, procedures, functions, triggers |
| 9 ยท Transactions | 42โ45 | ๐ก๐ด | Correctness under concurrency |
| 10 ยท Performance | 46โ50 | ๐ด | Indexes, plans, and making slow things fast |
| 11 ยท Design | 51โ53 | ๐ด | Normalization, star schemas, modelling |
| 12 ยท Admin & security | 54โ57 | ๐ด | Permissions, injection, backups, monitoring |
| 13 ยท Modern & expert | 58โ62 | ๐ด | JSON, dynamic SQL, collation, time zones, row history |
| 14 ยท Engineering practice | 63โ65 | ๐ด | Concurrency patterns, migrations and CI, calling SQL from code |
| 15 ยท Putting it together | 66 | ๐ด | Anti-patterns, cheat sheets, exercises, the capstone |
๐ Seeing words you do not know yet โ concurrency, normalization, JSON, collation, CI? That is completely normal. Every one is explained in plain English in its own chapter, and all of them are in the Glossary. You do not need to understand this table to start.
๐ฆ In a hurry? Five honest shortcuts:
- โI just need to write queries this week.โ โ Chapters 4, 13, 14, 16, 25, 26, 28 โ then stop. That is 80% of daily SQL.
- โI have an interview on Friday.โ โ Chapters 5, 15, 19, 26, 28, 30, 33, 42, 46, 51 + the question bank in 66.3.
- โSomething is slow in production right now.โ โ Go straight to Chapters 46, 47, 48. Start with 48.1.
- โI write application code that talks to a database.โ โ Chapters 55, 63, 65 โ parameterization, concurrency, and the calling patterns that cause most outages.
- โWe change our schema by hand and it scares me.โ โ Chapter 64, then 56.
๐งญ The two recurring blocks
Besides the inline icons, two larger blocks appear at fixed places. They are the ones to read if you are short of time.
| Block | Where | What it gives you |
|---|---|---|
| ๐งญ In practice | End of every teaching chapter (1โ65) | The five things you need before using anything: how to use it (steps, prerequisites, expected result), when to use it, where in a real system it belongs, when not to โ with the limitation and the alternative โ and the best practices, including the specific mistakes people make and any safety, security, or performance cost. |
| โ Checkpoint | At five milestones through the guide | A teach-back test. If you can answer โWhat is this? Why does it matter? How does it work? Can I give an example?โ in your own words, carry on. Sample answers are collapsed so you can try first. |
๐ What was verified, and what was not
A guide that says โeverything is testedโ without saying how is asking for trust it has not earned. Here is the precise position.
โ ๏ธ Not executed โ and you should know which
| What | Why not | What that means for you |
|---|---|---|
Data-modifying and DDL blocks outside Chapter 4 โ INSERT/UPDATE/DELETE demonstrations, CREATE VIEW, CREATE PROCEDURE, CREATE TRIGGER, temporal and partitioned tables |
Running them would move the shared practice database out of the exact state Chapter 4 builds | Their syntax was reviewed. Build these objects yourself as you work through each chapter โ later queries in the same chapter depend on them |
| Stored-procedure, function, and trigger bodies (Chapters 37โ39) | They need DELIMITER handling (MySQL) or whole-batch submission (MSSQL) |
Reviewed by reading. Run them in MySQL Workbench or SSMS, which handle this correctly |
Administrative scripts โ BACKUP, RESTORE, CREATE LOGIN, DBCC, replication, sp_configure |
Running them would alter or damage a real server | Read them; run them only on an instance you own and can rebuild |
| Partitioning, Full-Text, and columnstore examples (Ch 50, Ch 58) | Require features or editions not present on Express | Syntax reviewed; behaviour not observed |
Anything requiring Enterprise edition โ for example WITH (ONLINE = ON) index rebuilds |
Express edition refuses them (Msg 1712) |
The syntax is correct; the feature needs the right edition |
| Timing and โthis is fasterโ statements | No benchmark was run for this guide | Every such claim is explained by mechanism and stated with conditions. None is a measured figure. Measure on your own data before acting on any of them |
๐ Version sensitivity
The guide targets SQL Server 2019+ and MySQL 8.4 LTS. Where a feature needs a newer
release the text says so inline โ for example STRING_AGG (2017+), AT TIME ZONE (2016+),
GENERATE_SERIES (2022+), and TRIM with characters (2017+; LEADING/TRAILING/BOTH require 2022+ and compatibility 160). If a script fails on your server, check
the version note beside it first.
๐งพ How to read the code blocks
Every SQL block in this guide carries an engine marker as its first comment line. That marker is the most important thing on the block โ SQL Server and MySQL disagree about syntax far more often than beginners expect.
| Marker | Meaning |
|---|---|
-- โ
Works in BOTH |
Standard SQL. Paste into either engine unchanged |
-- ๐ฅ MSSQL or -- โ
MSSQL |
SQL Server only. Will not run on MySQL |
-- ๐ฆ MySQL or -- โ
MySQL |
MySQL only. Will not run on SQL Server |
-- โ ...FAILS... |
Deliberately broken, to show you the error. The expected message is given with it |
[!WARNING] โ ๏ธ Some blocks show both engines in one box, with a
-- ๐ฅ MSSQLsection followed by a-- ๐ฆ MySQLsection. Do not paste the whole box into one query window โ run only the half for the engine you are using. The other half will raise a syntax error, and it is not a mistake in the script.
๐ Blocks that build on earlier ones
Most chapters are cumulative: a query in ยง11.4 may drop a column that ยง11.1 added. If a script complains that an object does not exist or already exists, you have almost certainly run the chapter out of order.
The reset is always the same, and it is cheap:
1. Re-run the build script for your engine โ ยง4.3 (MSSQL) or ยง4.4 (MySQL)
2. Re-run the seed data โ ยง4.5
3. Re-run the verification query โ ยง4.6, and check the eight counts
That takes a few seconds and puts ShopDB back exactly as this guide assumes. Do this whenever a result stops matching the guide โ it is faster than working out which experiment left something behind, and the chapters from 8 onwards deliberately create, alter, and drop objects.
[!TIP] ๐ก Keep the three scripts from Chapter 4 in a file called
rebuild_shopdb.sql. You will use it more than any other script in this guide, and being able to destroy your practice data without worrying is what lets you experiment properly.
๐บ๏ธ How to read this guide (please read this first!)
[!IMPORTANT] Do not read this like a novel. Use it like a lab manual. ๐ฌ SQL is learned with your fingers, not your eyes. A query you only read is a query you do not know.
For every topic, follow this 7-step loop:
| Step | Action | Why it matters |
|---|---|---|
| 1๏ธโฃ | Read the plain-English explanation | Understand what problem it solves |
| 2๏ธโฃ | Type the example by hand | Muscle memory beats copy-paste |
| 3๏ธโฃ | Run it against the sample database | See real rows come back |
| 4๏ธโฃ | Break it on purpose | Errors are your best teacher |
| 5๏ธโฃ | Fix it | Learn how SQL fails and recovers |
| 6๏ธโฃ | Change it into your own question | Make the idea yours |
| 7๏ธโฃ | Check the plan with EXPLAIN โ the database shows how it ran your query |
Learn why it was fast or slow (skip this step until you reach Chapter 47) |
๐จ How to read the colored boxes (your legend)
The guide uses the same colored boxes everywhere, so you always know what kind of information you are looking at:
[!NOTE] ๐ In plain words โ the idea explained with zero jargon. If you read nothing else, read these.
[!TIP] ๐ก Pro tip โ a shortcut or a good habit that experienced people use.
[!IMPORTANT] ๐ฏ When to use it โ the situations where this feature is the right choice.
[!WARNING] โ ๏ธ Watch out โ a trap that catches people, and how to avoid it.
[!CAUTION] ๐ Common mistake โ an error you will very likely hit, and how to fix it.
๐ Analogy โ an everyday comparison that makes the idea stick (shown in a plain grey box).
Other icons you will meet:
| Icon | Meaning |
|---|---|
| ๐ | MSSQL vs MySQL โ where the two engines behave differently |
| ๐งช | Try it yourself โ hands-on exercises at the end of every chapter |
| ๐ | Go deeper โ a link to the chapter that covers this properly |
| ๐ข ๐ก ๐ด | Level โ beginner, intermediate, expert |
Every code block is labelled on its first line, so you always know where to run it:
| First line of the block | What it means |
|---|---|
-- ๐ฅ MSSQL or -- โ
MSSQL |
Run it in SQL Server only |
-- ๐ฆ MySQL or -- โ
MySQL |
Run it in MySQL only |
-- โ
Works in BOTH |
Runs unchanged in both engines |
-- โ โฆ |
This example is meant to fail. Run it anyway and read the error message โ the error is the lesson |
[!TIP] ๐ก How to run a code block: copy it into a new query window in your tool, then run it โ F5 in SSMS, Ctrl + Shift + Enter in MySQL Workbench, Ctrl + Shift + E in VS Code, Alt + X in DBeaver. The results appear in a grid under your query.
-- โ
MSSQL (T-SQL)
SELECT TOP 5 * FROM customers;
-- โ
MySQL
SELECT * FROM customers LIMIT 5;
๐ Table of contents
๐ Click to expand the full 66-chapter map
๐ฑ Part 1 โ Foundations (start here if you know nothing)
- What a database really is
- What SQL is, and the two engines we use
- Installing and connecting
- The sample database: ShopDB
- How a query actually runs
๐งฑ Part 2 โ Building blocks
- Data types
- Creating databases and schemas
- Creating tables
- Constraints, the rules that protect your data
- Auto-numbering: IDENTITY vs AUTO_INCREMENT
- ALTER and DROP
๐ฅ Part 3 โ Working with data (CRUD)
- INSERT, putting data in
- SELECT, the heart of SQL
- WHERE, asking precise questions
- NULL, the value that is not there
- ORDER BY, sorting results
- Paging: TOP, LIMIT, OFFSET, FETCH
- UPDATE, changing data
- DELETE, TRUNCATE, DROP
๐งฎ Part 4 โ Functions
- String functions
- Numeric and math functions
- Date and time functions
- Conversion and casting
- Conditional logic: CASE, IIF, IF
๐ Part 5 โ Grouping and aggregation
๐ Part 6 โ Combining tables
- JOINs, the complete picture
- Set operators: UNION, INTERSECT, EXCEPT
- Subqueries and EXISTS
- CTEs and recursive queries
- APPLY and LATERAL
๐ Part 7 โ Analytics
๐๏ธ Part 8 โ Database objects
- Views
- Stored procedures
- User-defined functions
- Triggers
- Temporary tables and table variables
- Sequences and generated columns
๐ Part 9 โ Transactions and integrity
โก Part 10 โ Performance
- Indexes, the complete guide
- Execution plans
- Query optimization and SARGability
- Statistics and the optimizer
- Partitioning and very large tables
๐ Part 11 โ Design
- Normalization, 1NF to BCNF
- Denormalization and when to break the rules
- Data modeling and the star schema
๐ก๏ธ Part 12 โ Administration and security
- Security: users, roles, and permissions
- SQL injection and how to stop it
- Backup, restore, and disaster recovery
- Monitoring and maintenance
๐ Part 13 โ Modern and expert topics
- JSON, XML, and semi-structured data
- Dynamic SQL, cursors, and set-based thinking
- Collation, character sets, and Unicode
- Time zones and global data
- Temporal tables, history, and change tracking
๐งฐ Part 14 โ Engineering practice
- Concurrency patterns for real applications
- Database DevOps: migrations, standards, and testing
- SQL from the application layer
๐ Part 15 โ Putting it all together
| Part index | Prerequisites | Next | Full course |