Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 113 additions & 55 deletions 10_Schema/DATABASE_SCHEMA.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<p align="center">
<i>Part of the <a href="../">SQL Engineering Handbook</a></i>
</p>