You are reviewing a colleague's query that should list all departments and the number of employees hired on or after 2022-01-01. Departments with no qualifying hires must still appear with a count of 0.
SELECT d.department_name,
COUNT(e.employee_id) AS hires_since_2022
FROM departments d
LEFT JOIN employees e
ON d.department_id = e.department_id
WHERE e.hire_date >= '2022-01-01'
GROUP BY d.department_name;
When the query runs, departments without recent hires are missing. Which single change will correct the query so that those departments are kept and the count still reflects only post-2022 hires?
Add the keyword DISTINCT to the SELECT list.
Include e.hire_date in the GROUP BY clause.
Move the e.hire_date >= '2022-01-01' condition from the WHERE clause to the ON clause of the LEFT JOIN.
Replace the LEFT JOIN with an INNER JOIN and leave the WHERE clause as is.
The LEFT JOIN is intended to keep every row from the departments table, substituting NULLs when no matching employees exist. However, placing a filter that references the right-hand table (employees) in the WHERE clause forces the database engine to discard all rows where e.hire_date is NULL, effectively converting the outer join into an inner join. Moving the date condition into the ON clause applies the filter while the join is being formed, so unmatched department rows are preserved and COUNT(e.employee_id) correctly returns 0 for them.
The other options fail:
Replacing the LEFT JOIN with an INNER JOIN would still exclude departments without hires.
Adding DISTINCT does not address the join logic problem.
Adding e.hire_date to the GROUP BY clause changes the level of aggregation and still filters out NULL rows.
Therefore, the only effective fix is to move the predicate into the ON clause.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
Why does putting the condition in the WHERE clause affect the behavior of the LEFT JOIN?
Open an interactive chat with Bash
What is the difference between a LEFT JOIN and an INNER JOIN?
Open an interactive chat with Bash
How does COUNT() handle NULL values in this query?
Open an interactive chat with Bash
CompTIA Data+ DA0-002 (V2)
Data Analysis
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
Pass with Confidence.
IT & Cybersecurity Package
You have hit the limits of our free tier, become a Premium Member today for unlimited access.
Military, Healthcare worker, Gov. employee or Teacher? See if you qualify for a Community Discount.
Monthly
$19.99 $11.99
$11.99/mo
Billed monthly, Cancel any time.
$19.99 after promotion ends
3 Month Pass
$44.99 $26.99
$8.99/mo
One time purchase of $26.99, Does not auto-renew.
$44.99 after promotion ends
Save $18!
MOST POPULAR
Annual Pass
$119.99 $71.99
$5.99/mo
One time purchase of $71.99, Does not auto-renew.
$119.99 after promotion ends
Save $48!
BEST DEAL
Lifetime Pass
$189.99 $113.99
One time purchase, Good for life.
Save $76!
What You Get
All IT & Cybersecurity Package plans include the following perks and exams .