A retail company maintains a SQL Server database that includes a table named Projects with the columns project_id (primary key), project_name, dept_id, and budget. Management wants a list of every project whose budget is greater than the average project budget within the same department, generated in a single query without creating any temporary objects. Which SQL statement meets the requirement by using a nested query?
SELECT p.project_name
FROM Projects p
JOIN (
SELECT dept_id, AVG(budget) AS avg_budget
FROM Projects
GROUP BY dept_id
) a ON p.dept_id = a.dept_id
WHERE p.budget < a.avg_budget;
SELECT project_name
FROM Projects p
WHERE p.budget > (
SELECT AVG(budget)
FROM Projects
WHERE dept_id = p.dept_id
);
SELECT project_name
FROM Projects p
WHERE (
SELECT AVG(budget)
FROM Projects
WHERE dept_id = p.dept_id
) > p.budget;
SELECT project_name
FROM Projects
WHERE budget > (
SELECT AVG(budget)
FROM Projects
);
The request calls for comparing each project's budget to the average budget calculated for its own department. A correlated sub-query (nested query that references the outer query) solves this:
SELECT project_name FROM Projects p WHERE p.budget > ( SELECT AVG(budget) FROM Projects WHERE dept_id = p.dept_id);
Inside the sub-query, dept_id = p.dept_id ties the inner query to the current row of the outer query, so the average is recalculated for each department. The other statements are incorrect because they either compare against the company-wide average, reverse the comparison operator, or use a join but keep the wrong operator. Any of those would return projects that do not satisfy the business rule.
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 correlated sub-query in SQL?
Open an interactive chat with Bash
Why doesn't the second SQL statement work for this task?
Open an interactive chat with Bash
How does the WHERE clause with dept_id in the sub-query help?
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 .