…
…
In the employee table of a fictional software company, resolve the management chain with a self join, find the gaps with LEFT JOIN and write the department report in eight steps.
Compass Software is a fictional software company with 33 people. Before the new year's budget meeting, human resources wants to update the org chart: who reports to whom, how does the approval chain run, which department has been left empty, and who is not on any project? Three people have left the company in recent months and the records were not updated everywhere; some questions ask you to find these gaps. The real difficulty is this: an employee's manager is an employee too, in the same employee table.
Data: four tables, fictional sample data.
department: id, name, parent_id (the parent department it belongs to; the id of another department in the same table), manager_id (the id of the employee who manages the department) (11 departments)employee: id, full_name, job_title, department_id (still empty for new hires), manager_id (the id of the manager in the same table; empty for the general manager), hire_date, departure_date (empty for people who still work at the company) (33 employees)project: id, name, department_id (the department running the project) (8 projects)assignment: employee_id, project_id, role (an employee's role on a project; 32 assignments)An "active employee" is one whose departure_date is empty.
Lessons used: NULL values (7), grouping (12–13), relationships and joins (14–18), especially inner joins (15), outer joins and the pattern for finding what is missing (16), multiple joins and the self join (17). No new concept.
The cells contain only comment lines and ___: you write the query (in step 6 a query comes ready for you to fix). The first line of every cell starts with "Why this query:"; finish it with one sentence before you write the query (it is not checked, it just makes thinking easier). Every step only reads; you can re-run the cells as many times as you like.
What you'll do
Required topics
compass.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Human resources wants to start with a single department: who works on the Data Team right now, and since when? An employee's department is stored as a number in the employee table; the department's name is in the department table.
List only people who still work at the company. Show the employee's name (full_name), job title (job_title) and hire date (hire_date); the longest-serving employee comes first.
How many people are in the result? If someone had just joined the Data Team but their department was not yet recorded, would they appear in this list? That question comes back in steps 5 and 8.
This step's code cell is for members.Sign up free
The foundation of the chart is this question: who is each active employee's manager? The manager's number is in the employee's manager_id column, but the manager's name is in the same employee table, on another row. You need to use the same table in two roles: once as the employee, once as the manager.
The general manager must be on the list too; they have no manager, so the manager column should show empty. The result has 30 rows.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Show the employee's name (employee), job title (job_title) and the manager's name (manager); order by employee number. Is there anyone on the list whose manager has left the company? For now just take note; you will come back to this in step 6.
This step's code cell is for members.Sign up free
The Product Management department has leave requests approved in two steps: first the employee's manager, then that manager's manager. Human resources wants both approvers' names for everyone in this department.
This time the employee table plays three roles: employee, manager and senior manager. Each new role means a new alias and a new join. You reach the senior manager from the manager, not from the employee.
The department's director reports to the general manager, and the general manager has no manager; the director must stay on the list too. For everyone in Product Management (all employees whose department is Product Management), show the employee's name (employee), manager (manager) and senior manager (senior_manager); order by employee number.
This step's code cell is for members.Sign up free
At the budget meeting someone will ask, "are too many people reporting to one manager?" Human resources wants the managers who have at least 3 active employees reporting to them directly, and the size of their teams. Only direct reports count: Hiro Nakamura's team is the employees whose manager is Hiro, not the people below them.
Show the manager's name (manager) and the active team size (team_size); the largest team comes first, ties go to the manager with the smaller number.
Look at the result carefully: is there a manager you do not recognize? Did that person appear as an employee in the list from step 2?
This step's code cell is for members.Sign up free
The project office wants to know who is free next period: active employees who have no role on any project. An employee's role on a project is found in the rows of the assignment table; an employee with no role has no rows there at all.
Show each employee's department on the list too. Careful: new hires have not had their department recorded yet; they must not drop out of the list, and their department column should stay empty.
Show the employee's name (full_name), job title (job_title) and the department's name (department); order by employee number. Some of the people listed may look odd to you: is it a data error that directors have no project role, or is it natural?
This step's code cell is for members.Sign up free
Human resources noticed a problem: some employees' recorded manager has left the company, but manager_id was never updated. These people's leave requests land in nobody's queue. A teammate wrote the query below to list them. The query raises no error and returns three rows; but the list shows Oliver Chen, Sofia Lindberg and Ingrid Solberg, and all three work with their managers every day.
Run the query, compare one row with the list from step 2 and find the problem. Then fix it: the list should show active employees whose manager has left, the departed manager's name and the departure date. The columns stay the same: employee, departed_manager, departure_date.
This step's code cell is for members.Sign up free
The lead of the Sales Dashboard project wants the list of everyone to invite to the project meeting: who, in what role on the project, from which department, and who is their manager? This information is spread across four tables, and you need to use one table a second time for the employee's manager.
First draw the route: which table will you start from, and which column will you follow at each step? Grow the chain step by step; after each new join, check whether the row count changed. One employee on the project has no department recorded yet; they must stay on the list.
Show the employee's name (full_name), role on the project (role), the department's name (department) and the manager's name (manager); order by employee number.
This step's code cell is for members.Sign up free
Finally you will write the report that goes to the budget meeting. One row per department:
department)parent_department): the parent is also a row in the department tablemanager)active_employees)Every department must appear: the one with no parent, the one with no manager assigned and the one with no active employees. A department with no active employees must show 0. Order by department number; the result has 11 rows.
Think before you write: where will you put the active condition? A department with no employees at all and a department with only departed employees behave differently depending on where the condition goes. If the result is not 11 rows, move the condition.
When the report works, here is an interpretation question (not checked): are there departments in the report that show a manager but are not really managed? Using what you saw in earlier steps, write one sentence on which departments need separate discussion at the budget meeting. What do the numbers say for these departments, and what do they not say?
This step's code cell is for members.Sign up free