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

Level MSSQL MySQL Chapters Engines

๐Ÿš€ 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 -- ๐ŸŸฅ MSSQL section followed by a -- ๐ŸŸฆ MySQL section. 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)

  1. What a database really is
  2. What SQL is, and the two engines we use
  3. Installing and connecting
  4. The sample database: ShopDB
  5. How a query actually runs

๐Ÿงฑ Part 2 โ€” Building blocks

  1. Data types
  2. Creating databases and schemas
  3. Creating tables
  4. Constraints, the rules that protect your data
  5. Auto-numbering: IDENTITY vs AUTO_INCREMENT
  6. ALTER and DROP

๐Ÿ“ฅ Part 3 โ€” Working with data (CRUD)

  1. INSERT, putting data in
  2. SELECT, the heart of SQL
  3. WHERE, asking precise questions
  4. NULL, the value that is not there
  5. ORDER BY, sorting results
  6. Paging: TOP, LIMIT, OFFSET, FETCH
  7. UPDATE, changing data
  8. DELETE, TRUNCATE, DROP

๐Ÿงฎ Part 4 โ€” Functions

  1. String functions
  2. Numeric and math functions
  3. Date and time functions
  4. Conversion and casting
  5. Conditional logic: CASE, IIF, IF

๐Ÿ“Š Part 5 โ€” Grouping and aggregation

  1. Aggregate functions
  2. GROUP BY and HAVING
  3. ROLLUP, CUBE, and GROUPING SETS

๐Ÿ”— Part 6 โ€” Combining tables

  1. JOINs, the complete picture
  2. Set operators: UNION, INTERSECT, EXCEPT
  3. Subqueries and EXISTS
  4. CTEs and recursive queries
  5. APPLY and LATERAL

๐Ÿ“ˆ Part 7 โ€” Analytics

  1. Window functions
  2. Ranking, running totals, and moving averages
  3. PIVOT and UNPIVOT

๐Ÿ—๏ธ Part 8 โ€” Database objects

  1. Views
  2. Stored procedures
  3. User-defined functions
  4. Triggers
  5. Temporary tables and table variables
  6. Sequences and generated columns

๐Ÿ” Part 9 โ€” Transactions and integrity

  1. Transactions and ACID
  2. Isolation levels, locking, and deadlocks
  3. MERGE and UPSERT
  4. Error handling

โšก Part 10 โ€” Performance

  1. Indexes, the complete guide
  2. Execution plans
  3. Query optimization and SARGability
  4. Statistics and the optimizer
  5. Partitioning and very large tables

๐Ÿ“ Part 11 โ€” Design

  1. Normalization, 1NF to BCNF
  2. Denormalization and when to break the rules
  3. Data modeling and the star schema

๐Ÿ›ก๏ธ Part 12 โ€” Administration and security

  1. Security: users, roles, and permissions
  2. SQL injection and how to stop it
  3. Backup, restore, and disaster recovery
  4. Monitoring and maintenance

๐Ÿš€ Part 13 โ€” Modern and expert topics

  1. JSON, XML, and semi-structured data
  2. Dynamic SQL, cursors, and set-based thinking
  3. Collation, character sets, and Unicode
  4. Time zones and global data
  5. Temporal tables, history, and change tracking

๐Ÿงฐ Part 14 โ€” Engineering practice

  1. Concurrency patterns for real applications
  2. Database DevOps: migrations, standards, and testing
  3. SQL from the application layer

๐Ÿ Part 15 โ€” Putting it all together

  1. Anti-patterns, cheat sheets, exercises, and the capstone

Part index Prerequisites Next Full course