Assignment Overview
In this assignment, you will work with an Organization Database containing employees, departments, and hierarchical reporting structures. You'll use subqueries and CTEs to solve complex business problems.
Subqueries
Scalar, inline, correlated
CTEs
WITH clause, chained CTEs
Recursive CTEs
Hierarchies, trees, graphs
The Scenario
Corporate HR System
You're working on the HR database for a large corporation. The database tracks employees, their managers (hierarchical), departments, and salary history.
"We need reports showing organizational hierarchies, salary comparisons across departments, and employees who report to specific managers at any level."
Query Requirements
Subquery Basics (5 queries)
- Find employees earning above department average (scalar subquery)
- List departments with more than 5 employees (subquery in WHERE)
- Find employees without direct reports (NOT EXISTS)
- Get employees in the same department as a specific person (IN subquery)
- Correlated subquery for row-by-row comparison
CTE Queries (5 queries)
- Simple CTE for readable multi-step calculation
- Multiple CTEs chained together
- CTE with aggregation and JOIN
- Reusable CTE referenced multiple times
- CTE replacing complex subquery
Recursive CTE Queries (5 queries)
- Build complete organizational hierarchy
- Find all subordinates of a manager (any level)
- Calculate management chain depth for each employee
- Generate date series for reporting
- Category hierarchy with full path
Submission Guidelines
GitHub Repository
- Create a repository named
sql-assignment-5-cte - Include
schema.sqlwith hierarchical table structure - Include
queries.sqlwith all 15 CTE/subquery examples - Document the organizational hierarchy in your README
Grading Rubric
| Criteria | Points |
|---|---|
| Subquery basics (5 × 7 pts) | 35 |
| CTE queries (5 × 8 pts) | 40 |
| Recursive CTE queries (5 × 10 pts) | 50 |
| Total | 125 |