SQL-Engineering-Handbook

πŸ“Š Database Schema Foundation

The unified employee management database behind all SQL Engineering Handbook examples


🎯 Overview

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.


πŸ“ Contents

Files in this directory:

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

πŸ—‚οΈ Schema Structure

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             β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Relationships:


πŸ“‹ Table Details

πŸ‘₯ employes Table

Stores 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 Table

Stores 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 Table

Stores 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:


πŸš€ Quick Start

1. Create the Schema

Run the DDL to create all tables:

# Execute 01_CREATE_TABLES.sql in your SQL environment

2. Load Sample Data

Populate with 50 employees across 10 departments:

# Execute 02_INSERT_DATA.sql

3. Verify Setup

SELECT COUNT(*) FROM employes;      -- Should return 50
SELECT COUNT(*) FROM departments;   -- Should return 10
SELECT COUNT(*) FROM locations;     -- Should return 5

πŸ“š Learning Objectives

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


πŸ’Ό Real-World Use Cases

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


πŸŽ“ How This Schema Connects to Modules

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

πŸ“Š Dataset Statistics

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

⚠️ Important Notes


πŸ“ž Support & Questions

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