The unified employee management database behind all SQL Engineering Handbook examples
The 00_Schema module contains the complete database schema, setup instructions, and documentation for the employee management database used throughout the SQL Engineering Handbook.
Instead of using different toy tables for each lesson, every query across all 65+ files runs against this single, consistent schema. This approach mirrors real-world database work and allows concepts to build on each otherβa JOIN technique you learn in Module 3 uses the exact same tables as Window Functions in Module 7.
| File | Purpose |
|---|---|
| 01_CREATE_TABLES.sql | Database table definitions (DDL) for creating the three core tables |
| 02_INSERT_DATA.sql | Seed data with 50 employees, 10 departments, and 5 locations |
| DATABASE_SCHEMA.md | Comprehensive schema documentation with table definitions and relationships |
| ERD.md | Entity Relationship Diagram showing table structure and connections |
| README.md | This file |
The database consists of three related tables:
βββββββββββββββββββββββ
β employes β βββ 50 employees
βββββββββββββββββββββββ€
β emp_id (PK) β
β emp_name β
β dept_id (FK) βββ β
β manager_id (FKβ β
β hire_date β β
ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β departments β βββ 10 departments
βββββββββββββββββββββββ€
β dept_id (PK) β
β dept_name β
β location_id (FK)βββ β
βββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β locations β βββ 5 locations
βββββββββββββββββββββββ€
β location_id (PK) β
β city β
β country β
βββββββββββββββββββββββ
employes TableStores individual employee records with manager hierarchy.
| Column | Type | Constraints | Notes |
|---|---|---|---|
emp_id |
INT | PRIMARY KEY | Unique employee identifier |
emp_name |
VARCHAR(50) | nullable | Employeeβs full name |
dept_id |
INT | FOREIGN KEY | References departments |
manager_id |
INT | FOREIGN KEY (self) | Reports to (nullable for top managers) |
hire_date |
DATE | NOT NULL | Join date at company |
Sample Data:
departments TableStores department metadata.
| Column | Type | Constraints | Notes |
|---|---|---|---|
dept_id |
INT | PRIMARY KEY | Unique department identifier |
dept_name |
VARCHAR(50) | nullable | Department name |
location_id |
INT | FOREIGN KEY | References locations |
Sample Departments:
locations TableStores city and country information.
| Column | Type | Constraints | Notes |
|---|---|---|---|
location_id |
INT | PRIMARY KEY | Unique location identifier |
city |
VARCHAR(50) | nullable | City name |
country |
VARCHAR(50) | nullable | Country name |
Locations:
Run the DDL to create all tables:
# Execute 01_CREATE_TABLES.sql in your SQL environment
Populate with 50 employees across 10 departments:
# Execute 02_INSERT_DATA.sql
SELECT COUNT(*) FROM employes; -- Should return 50
SELECT COUNT(*) FROM departments; -- Should return 10
SELECT COUNT(*) FROM locations; -- Should return 5
This schema teaches and reinforces:
β
Primary Keys & Foreign Keys β Understanding table uniqueness and referential integrity
β
One-to-Many Relationships β How data normalizes across related tables
β
Self-Joins & Hierarchies β Manager-to-employee reporting chains
β
Multi-Table Joins β Combining 3+ tables in a single query
β
NULL Handling β Optional foreign keys for top-level managers
This schema models common business scenarios:
π DATABASE_SCHEMA.md β Full schema documentation with detailed descriptions
π ERD.md β Visual entity-relationship diagram
πΎ 01_CREATE_TABLES.sql β Table creation DDL
π₯ 02_INSERT_DATA.sql β Sample data insertion
| Module | Uses Tables | Key Concept |
|---|---|---|
| 01_Fundamentals | employes | SELECT, WHERE basics |
| 03_Joins | employes, departments, locations | INNER/LEFT/RIGHT JOINs |
| 06_CTEs | all tables | Hierarchical queries with WITH |
| 07_Window_Functions | employes, departments | ROW_NUMBER over departments |
| 08_WINDOW_BUSINESS_CASES | all tables | Real-world analytics |
| 12_ADVANCED_AGGREGATIONS | all tables | GROUP BY with HAVING |
| 13_SET_OPERATORS | employes | UNION, EXCEPT operations |
| Metric | Value |
|---|---|
| Total Employees | 50 |
| Total Departments | 10 |
| Total Locations | 5 |
| Manager-Employee Pairs | 40+ |
| Top-Level Managers (NULL manager_id) | 10 |
| Date Range (hire_date) | 2018β2025 |
manager_id is intentionally NULL for top-level managers to teach NULL handling.For questions about:
π‘ Tip: Bookmark this schema reference! You'll return to it often as you progress through the handbook. Understanding the relationships between these three tables unlocks mastery of JOINs, aggregations, and advanced SQL concepts.
Part of the SQL Engineering Handbook
Foundation module for all SQL learning examples