Return the names of employees with jobtitle Sales Rep who appear as repid on ZERO orders. Table definitions (from the…
SQLLevel: IntermediateAnti-joins and missing data
Return the names of employees with job_title Sales Rep who appear as rep_id on ZERO orders.
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);
CREATE TABLE orders(order_id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, amount REAL, rep_id INTEGER, discount REAL);
Hints
- Define one output row and the source-table keys.
- Check duplicates, NULLs and the order of filtering and aggregation.
Answer
Approach: Anti-join employees (filtered to reps) against orders.rep_id.
SELECT e.name
FROM employees e
LEFT JOIN orders o ON o.rep_id = e.emp_id
WHERE e.job_title = 'Sales Rep'
AND o.order_id IS NULL;
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