Skip to content
Quantoros

Return employee name, their hiredate, manager name, manager hiredate for employees hired BEFORE their manager. Table…

SQLLevel: IntermediateJoins and self-joins

Return employee name, their hire_date, manager name, manager hire_date for employees hired BEFORE their manager.

Table definitions (from the source exercise):

CREATE TABLE employees(emp_id INTEGER PRIMARY KEY, name TEXT, department_id INTEGER, manager_id INTEGER, salary INTEGER, hire_date TEXT, job_title TEXT, gender TEXT);
Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: Self-join and compare hire dates.

SELECT e.name AS employee, e.hire_date,
       m.name AS manager, m.hire_date AS manager_hire
FROM employees e
JOIN employees m ON m.emp_id = e.manager_id
WHERE e.hire_date < m.hire_date;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions