Correct answer: B
SELECT firstName, lastName, SUM(income) FROM companyRoster GROUP BY
firstName, lastName
Explanation
Explanation:
Question Restatement
This question asks: When using an aggregate function like SUM() in SQL, how can you correctly group and retrieve non-aggregated columns to avoid an error?
Correct Answer Justification — Why B Is Correct
In SQL, when you include an aggregate function like SUM(income) in a SELECT statement alongside non-aggregated columns (firstName, lastName), you must use a GROUP BY clause to tell the database how to group the rows before aggregation. Without GROUP BY, SQL does not know how to combine multiple rows for each person.
The corrected code:
SELECT firstName, lastName, SUM(income)
FROM companyRoster
GROUP BY firstName, lastName
ORDER BY lastName, SUM(income);
This syntax correctly groups income by each first and last name, then allows sorting or ordering by the aggregated values. This is exactly what Option B specifies — adding GROUP BY firstName, lastName fixes the error.
Incorrect Answer Analysis
A. HAVING SUM(income) > 10000000: HAVING is used to filter after aggregation, but it does not resolve the requirement to group non-aggregated columns. You still need a GROUP BY even if you use HAVING.
C. ORDER BY firstName, income: ORDER BY alone does not fix the aggregation issue. The original error is not about sorting—it’s about missing GROUP BY.
D. SELECT firstName, lastName, SUM(income) FROM companyRoster: Leaving it as-is still produces an error because of mixing aggregated and non-aggregated columns without a GROUP BY.
Key Concepts and Terminology
Aggregate Function: A function (SUM, COUNT, AVG, MAX, MIN) that returns a single value from multiple rows.
GROUP BY Clause: Groups rows sharing values of specified columns into summary rows, one for each unique group. Required when mixing aggregates and non-aggregates.
HAVING Clause: Filters groups after aggregation (similar to WHERE but for grouped data).
ORDER BY Clause: Sorts the returned rows; does not affect grouping or aggregation.
Real-World Application
If you’re analyzing company payroll, you might want to see total income per employee. Using GROUP BY firstName, lastName aggregates multiple paychecks or commissions under each employee’s name. Without it, SQL cannot compute a single SUM per employee and throws an error.
References and Resources
CompTIA Data+ (DA0-002) Exam Objectives: Domain 2.1 (data manipulation using SQL).
W3Schools SQL GROUP BY documentation.
ANSI SQL standard on aggregate functions.
Common Mistakes
Trying to mix aggregate and non-aggregate columns without using GROUP BY.
Confusing HAVING (filter groups) with WHERE (filter rows before grouping).
Using SORT BY instead of ORDER BY—SQL uses ORDER BY as the correct syntax.
Domain Cross-Reference
Domain 2: Data Mining and Manipulation (performing aggregations and grouping).
Domain 3: Data Analysis and Visualization (summarizing and displaying data properly).
Summary
The SQL fails because it mixes aggregated and non-aggregated columns without grouping. Adding GROUP BY firstName, lastName (Option B) fixes the issue, letting SQL correctly aggregate income per person. HAVING filters groups after aggregation but does not replace GROUP BY. ORDER BY only sorts results and does not resolve the aggregation error.