diff --git a/10_Schema/DATABASE_SCHEMA.md b/10_Schema/DATABASE_SCHEMA.md index 79f82d9..ca561a0 100644 --- a/10_Schema/DATABASE_SCHEMA.md +++ b/10_Schema/DATABASE_SCHEMA.md @@ -2,107 +2,165 @@ ## Overview -This handbook uses a custom employee management database designed for learning SQL concepts ranging from fundamentals to advanced analytics. +Every query in the SQL Engineering Handbook runs against a single, consistent +employee management database. Using one schema across all 65+ files — instead +of a different toy table per lesson — mirrors how you'd actually work against +a real company database, and lets concepts compound: a JOIN you learn in +Module 3 uses the exact same tables a Window Function uses in Module 7. The schema contains three related tables: ```text -employes +locations + ▲ │ - ▼ departments + ▲ │ - ▼ -locations +employes ``` +Read the diagram bottom-up: many **employes** roll up into a **department**, +and many **departments** roll up into a **location**. + --- -## Table: employes +## Table: `employes` + +Stores individual employee records, including a self-referencing manager +relationship. + +| Column | Data Type | Nullable | Description | +|--------------|--------------|----------|-------------------------------------------| +| `emp_id` | `INT` | No (PK) | Unique employee identifier | +| `emp_name` | `VARCHAR(50)`| Yes | Employee's full name | +| `dept_id` | `INT` | Yes (FK) | References `departments.dept_id` | +| `manager_id` | `INT` | Yes (FK) | References `employes.emp_id` (self-join) | +| `hire_date` | `DATE` | **No** | Date the employee joined the company | -Stores employee information. +> `manager_id` is `NULL` for top-level managers who report to no one in this +> dataset (e.g. `emp_id = 3`, `Sahil`). -| Column | Data Type | Description | -|----------|----------|----------| -| emp_id | INT | Employee ID | -| emp_name | VARCHAR(50) | Employee Name | -| dept_id | INT | Department ID | -| manager_id | INT | Reporting Manager | +**Sample rows:** + +| emp_id | emp_name | dept_id | manager_id | hire_date | +|--------|----------|---------|------------|------------| +| 1 | Ammar | 1 | 11 | 2023-01-15 | +| 3 | Sahil | 1 | NULL | 2022-11-10 | +| 11 | Rohit | 1 | NULL | 2020-04-11 | --- -## Table: departments +## Table: `departments` + +Stores department metadata. -Stores department information. +| Column | Data Type | Nullable | Description | +|---------------|--------------|----------|-------------------------------------| +| `dept_id` | `INT` | No (PK) | Unique department identifier | +| `dept_name` | `VARCHAR(50)`| Yes | Department name | +| `location_id` | `INT` | Yes (FK) | References `locations.location_id` | -| Column | Data Type | Description | -|----------|----------|----------| -| dept_id | INT | Department ID | -| dept_name | VARCHAR(50) | Department Name | -| location_id | INT | Location ID | +**Sample rows:** + +| dept_id | dept_name | location_id | +|---------|------------------|-------------| +| 1 | Data Analytics | 1 | +| 2 | Engineering | 2 | --- -## Table: locations +## Table: `locations` + +Stores city/country metadata for each department's base of operations. -Stores location information. +| Column | Data Type | Nullable | Description | +|----------------|--------------|----------|--------------------| +| `location_id` | `INT` | No (PK) | Unique location ID | +| `city` | `VARCHAR(50)`| Yes | City name | +| `country` | `VARCHAR(50)`| Yes | Country name | -| Column | Data Type | Description | -|----------|----------|----------| -| location_id | INT | Location ID | -| city | VARCHAR(50) | City Name | -| country | VARCHAR(50) | Country Name | +**Sample rows:** + +| location_id | city | country | +|-------------|-----------|---------| +| 1 | Nagpur | India | +| 3 | Mumbai | India | --- ## Relationships -### Employee → Department - -Many employees belong to one department. +### `employes` → `departments` (many-to-one) ```text -employes.dept_id - ↓ -departments.dept_id +employes.dept_id → departments.dept_id ``` -### Department → Location +Many employees belong to one department. -Many departments can belong to one location. +### `departments` → `locations` (many-to-one) ```text -departments.location_id - ↓ -locations.location_id +departments.location_id → locations.location_id ``` -### Employee → Manager +Many departments can be based in one city. -Self-referencing relationship. +### `employes` → `employes` (self-referencing, many-to-one) ```text -employes.manager_id - ↓ -employes.emp_id +employes.manager_id → employes.emp_id ``` +An employee reports to at most one manager, who is themself a row in the +same `employes` table. This relationship is what makes `03_Joins/04_SELF_JOIN.md` +and hierarchical query examples possible. + +--- + +## Dataset Size + +| Table | Row Count | +|---------------|-----------| +| `locations` | 5 | +| `departments` | 10 | +| `employes` | 50 | + +Source of truth for both structure and data: +- Structure: [`01_CREATE_TABLES.sql`](./01_CREATE_TABLES.sql) +- Data: [`02_INSERT_DATA.sql`](./02_INSERT_DATA.sql) + --- -## Concepts Demonstrated +## Concepts This Schema Is Designed to Teach -- Primary Keys -- Foreign Keys -- Self Join Relationships -- Multi-table Joins -- Hierarchical Data +- Primary keys and foreign keys +- One-to-many relationships +- Self-joins and hierarchical (manager → report) data +- Multi-table joins (3+ tables) +- NULL handling on optional foreign keys --- -## Business Use Cases +## Business Use Cases Modeled by This Schema + +- Workforce and headcount analytics +- Department-level performance reporting +- Manager span-of-control analysis +- Location-based organizational reporting +- Tenure and hiring trend analysis (via `hire_date`) + +--- + +## Related Documentation + +- [ERD.md](./ERD.md) — visual entity-relationship diagram +- [01_CREATE_TABLES.sql](./01_CREATE_TABLES.sql) — DDL +- [02_INSERT_DATA.sql](./02_INSERT_DATA.sql) — seed data + +--- -- Workforce Analytics -- Department Reporting -- Manager Analysis -- Location Analysis -- Organizational Structure Reporting +

+ Part of the SQL Engineering Handbook +