my-sql β SQL Learning Repository
A fully self-contained SQL learning database for practising:
Aggregation & GROUP BY
Subqueries & CTE (Common Table Expressions)
Window Functions
Stored Procedures (T-SQL)
Transactions (T-SQL)
Indexes (T-SQL β clustered, nonclustered, columnstore, filtered, covering)
The repository contains two parallel sets of scripts:
sql/ β MySQL dialect
tsql/ β SQL Server / T-SQL dialect (same schema and data, T-SQL syntax)
File
Description
sql/01_ddl_schema.sql
DDL β CREATE TABLE statements for all 9 tables
sql/02_dml_data.sql
Seed data β INSERT statements (25 customers, 30 products, 50 orders, β¦)
sql/03_aggregation_group_by.sql
~20 annotated examples of aggregation and GROUP BY
sql/04_subqueries_cte.sql
~20 annotated examples of subqueries and CTEs
sql/05_window_functions.sql
~20 annotated examples of window functions
SQL Server / T-SQL (tsql/)
File
Description
tsql/01_ddl_schema.sql
DDL β T-SQL CREATE TABLE (IDENTITY, NVARCHAR, CHECK constraints)
tsql/02_dml_data.sql
Seed data β SET IDENTITY_INSERT ON/OFF + INSERT statements
tsql/03_aggregation_group_by.sql
Aggregation & GROUP BY β T-SQL syntax (TOP, GROUPING SETS, CUBE)
tsql/04_subqueries_cte.sql
Subqueries & CTEs β T-SQL syntax (recursive CTE without RECURSIVE keyword, REPLICATE, FORMAT)
tsql/05_window_functions.sql
Window functions β T-SQL syntax (inline OVER(), no named WINDOW clause)
tsql/06_stored_procedures.sql
Stored Procedures β CREATE OR ALTER PROCEDURE, @params, OUTPUT, TRY-CATCH, RAISERROR, THROW
tsql/07_transactions.sql
Transactions β BEGIN TRAN, COMMIT, ROLLBACK, SAVE TRAN, @@TRANCOUNT, XACT_ABORT, isolation levels
tsql/08_indexes.sql
Indexes β clustered, nonclustered, unique, composite, covering (INCLUDE), filtered, columnstore, FILLFACTOR, fragmentation
departments ββ< employees (self-ref manager_id)
categories (self-ref parent_category_id)
β
products ββ< order_items >ββ orders ββ< customers
β
payments
products ββ< reviews >ββ customers
Table
Rows
Key columns
departments
8
department_id, budget
employees
20
salary, department_id, manager_id (self-ref)
categories
12
parent_category_id (self-ref, 2-level tree)
products
30
price, cost, stock_quantity, category_id
customers
25
country, city, registration_date
orders
50
status, order_date, customer_id, employee_id
order_items
~90
quantity, unit_price, discount
payments
40
amount, payment_method
reviews
30
rating (1β5)
π Quick Start β MySQL
-- 1. Create a fresh database
CREATE DATABASE IF NOT EXISTS shop;
USE shop;
-- 2. Create tables
SOURCE sql/ 01_ddl_schema .sql ;
-- 3. Load seed data
SOURCE sql/ 02_dml_data .sql ;
-- 4. Start learning!
SOURCE sql/ 03_aggregation_group_by .sql ;
SOURCE sql/ 04_subqueries_cte .sql ;
SOURCE sql/ 05_window_functions .sql ;
Or using the mysql CLI:
mysql -u root -p -e " CREATE DATABASE IF NOT EXISTS shop; USE shop;"
mysql -u root -p shop < sql/01_ddl_schema.sql
mysql -u root -p shop < sql/02_dml_data.sql
π Quick Start β SQL Server / T-SQL
-- 1. Create a fresh database
CREATE DATABASE shop ;
GO
USE shop;
GO
-- 2. Create tables (run in SSMS using :r β Windows paths shown below;
-- use forward slashes on macOS/Linux)
:r tsql/ 01_ddl_schema .sql
:r tsql/ 02_dml_data .sql
-- 3. Start learning!
:r tsql/ 03_aggregation_group_by .sql
:r tsql/ 04_subqueries_cte .sql
:r tsql/ 05_window_functions .sql
:r tsql/ 06_stored_procedures .sql
:r tsql/ 07_transactions .sql
:r tsql/ 08_indexes .sql
Or using sqlcmd:
# Linux / macOS
sqlcmd -S localhost -d shop -i tsql/01_ddl_schema.sql
sqlcmd -S localhost -d shop -i tsql/02_dml_data.sql
# Windows (PowerShell / cmd)
sqlcmd -S localhost -d shop -i tsql\0 1_ddl_schema.sql
sqlcmd -S localhost -d shop -i tsql\0 2_dml_data.sql
03 β Aggregation & GROUP BY
#
Topic
1
COUNT, SUM, AVG, MIN, MAX on full table
2
GROUP BY single column
3
GROUP BY multiple columns (year + month, country + status)
4
HAVING β filter on aggregated values
5
Expressions inside aggregates (gross vs net revenue, profit margin)
6
Conditional aggregation β CASE inside SUM / COUNT
7
ORDER BY aggregate result, TOP (T-SQL) / LIMIT (MySQL)
8
COUNT(DISTINCT β¦)
9
Multi-table aggregation with JOIN
10
ROLLUP, CUBE, GROUPING SETS (T-SQL) / WITH ROLLUP (MySQL)
#
Topic
1
Scalar subquery in SELECT
2
Scalar subquery in WHERE
3
Correlated subquery (references outer query row)
4
Subquery in FROM β derived table / inline view
5
EXISTS / NOT EXISTS β semi-join and anti-join
6
IN / NOT IN with subquery
7
ALL / ANY comparisons
8
Basic CTE (WITH clause)
9
Multiple CTEs chained together
10
CTE for DRY aggregation (compute once, use multiple times)
11
Recursive CTE β employee org hierarchy tree
12
Recursive CTE β category tree path
#
Topic
1
ROW_NUMBER β unique sequential row number
2
RANK / DENSE_RANK β rankings with / without gaps
3
NTILE β buckets / quartiles / terciles
4
Aggregate window functions (SUM, AVG, COUNT, MIN, MAX OVER)
5
Running total β cumulative SUM with ROWS UNBOUNDED PRECEDING
6
Moving average β sliding window frame
7
LAG / LEAD β access previous / next rows
8
FIRST_VALUE / LAST_VALUE β boundary values in frame
9
PERCENT_RANK / CUME_DIST β relative position
10
Partitioned windows β analytics within groups
11
Named WINDOW clause (MySQL) / inline OVER() (T-SQL)
12
Combined analytical report β all techniques together
06 β Stored Procedures (T-SQL only)
#
Topic
1
Basic procedure β no parameters, SET NOCOUNT ON
2
Input parameters, optional filter (= NULL default)
3
OUTPUT parameter to return a scalar value
4
Default parameter values
5
Procedure with recursive CTE inside
6
Procedure using a #temp table
7
TRY-CATCH error handling + RAISERROR / THROW
8
Nested procedure call (EXEC inside a procedure)
9
Procedure with an embedded transaction
10
CREATE OR ALTER PROCEDURE β idempotent definition
07 β Transactions (T-SQL only)
#
Topic
1
BEGIN TRANSACTION / COMMIT
2
ROLLBACK on a business-rule violation
3
@@TRANCOUNT β nesting counter
4
SAVE TRANSACTION β named savepoints (partial rollback)
5
SET XACT_ABORT ON β automatic rollback on error
6
TRY-CATCH wrapping a transaction
7
Named transactions
8
Isolation levels (READ UNCOMMITTED, REPEATABLE READ, SNAPSHOT)
9
SET IMPLICIT_TRANSACTIONS ON
10
sys.dm_tran_active_transactions β inspect open transactions
08 β Indexes (T-SQL only)
#
Topic
1
Clustered index β concept and syntax
2
Nonclustered index β basic single-column
3
Unique index
4
Composite (multi-column) index β column order matters
5
Covering index β INCLUDE columns for index-only scans
6
Filtered index β partial index with WHERE predicate
7
Nonclustered columnstore index
8
Clustered columnstore index (fact-table / DWH use case)
9
Index options: FILLFACTOR, PAD_INDEX, ONLINE, SORT_IN_TEMPDB
10
sys.indexes / sys.index_columns β index metadata
11
sys.dm_db_index_usage_stats β usage statistics
12
sys.dm_db_index_physical_stats β fragmentation + dynamic maintenance
π MySQL vs T-SQL Quick Reference
Feature
MySQL
T-SQL (SQL Server)
Auto-increment
AUTO_INCREMENT
IDENTITY(1,1)
ENUM type
ENUM('a','b')
NVARCHAR(n) + CHECK (col IN (...))
String concat
CONCAT(a,b)
a + b or CONCAT(a,b)
Limit rows
LIMIT n
TOP n or OFFSET 0 ROWS FETCH NEXT n ROWS ONLY
Date format
DATE_FORMAT(d, '%Y-%m')
FORMAT(d, 'yyyy-MM')
Repeat string
REPEAT(s, n)
REPLICATE(s, n)
Recursive CTE
WITH RECURSIVE cte AS (β¦)
WITH cte AS (β¦) (no RECURSIVE keyword)
Named window
WINDOW w AS (β¦)
Not supported β inline OVER(β¦) required
Rollup syntax
GROUP BY a, b WITH ROLLUP
GROUP BY ROLLUP(a, b)
Cube / grouping sets
β
GROUP BY CUBE(a,b) / GROUP BY GROUPING SETS(β¦)
Stored procedure
DELIMITER // β¦ CREATE PROCEDURE β¦
CREATE [OR ALTER] PROCEDURE β¦ AS BEGINβ¦END
Transaction save
SAVEPOINT name
SAVE TRANSACTION name
Run 01_ddl_schema.sql and 02_dml_data.sql once to set up your environment.
Open each topic file and run queries one block at a time β every query is preceded by a comment explaining what it demonstrates.
Experiment: modify WHERE, HAVING, PARTITION BY conditions to build intuition.
The recursive CTEs (section 04, queries 11β12) require MySQL 8.0+ / SQL Server 2005+.
All window functions require MySQL 8.0+ / SQL Server 2012+.
For T-SQL files, use SQL Server Management Studio (SSMS) or Azure Data Studio for the best experience.
T-SQL 06_stored_procedures.sql, 07_transactions.sql, and 08_indexes.sql cover SQL Server-specific features with no MySQL equivalents.