emp_id | full_name | dept_id
-------|-----------|--------
101 | Alice | 1
102 | Bob | 1
103 | Carol | 3
104 | Dave | 3
105 | Eve | NULL
The analyst runs the query:
SELECT d.dept_name, COUNT(e.emp_id) AS emp_count
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
GROUP BY d.dept_name
HAVING COUNT(e.emp_id) = 0;
Which rows will the query return?
HR and Marketing only
All four departments with their respective employee counts
Finance and IT only
Employees whose dept_id is NULL (for example, Eve)
A LEFT JOIN keeps every row from the left-hand (departments) table and substitutes NULLs for columns from employees when no match exists. Because COUNT(e.emp_id) tallies only non-NULL values, departments that have no matching employees appear with a count of 0. Finance and IT each have at least one matching employee, so they are filtered out by the HAVING clause. HR and Marketing have no matches, giving a zero count that meets the HAVING condition, so those two department names are returned. Rows from the employees table where dept_id is NULL (such as Eve) are not included because the query groups by departments and the LEFT JOIN condition fails when dept_id is NULL.
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.
What is a LEFT JOIN in SQL?
Open an interactive chat with Bash
What does the HAVING clause do in SQL?
Open an interactive chat with Bash
Why are rows with NULL dept_id excluded in this query?
Open an interactive chat with Bash
CompTIA Data+ DA0-002 (V2)
Data Acquisition and Preparation
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 .