DA0-002 practice question 10 of 25

[Data Analysis]The following SQL code returns an error in the program console: SELECT firstName, lastName, SUM(income) FROM companyRoster SORT BY…

Full set: 111 questions

[Data Analysis]

The following SQL code returns an error in the program console: SELECT firstName, lastName, SUM(income) FROM companyRoster SORT BY lastName, income Which of the following changes allows this SQL code to run?

Answer options
A: SELECT firstName, lastName, SUM(income) FROM companyRoster HAVING SUM(income) > 10000000
B: SELECT firstName, lastName, SUM(income) FROM companyRoster GROUP BY firstName, lastName Correct answer
C: SELECT firstName, lastName, SUM(income) FROM companyRoster ORDER BY firstName, income
D: SELECT firstName, lastName, SUM(income) FROM companyRoster
Correct Answer:
B. SELECT firstName, lastName, SUM(income) FROM companyRoster GROUP BY firstName, lastName
Explanation

The provided SQL query fails because it attempts to use an aggregate function, SUM(income), alongside non-aggregated columns, firstName and lastName, without specifying how to group the data. According to standard SQL rules, when a SELECT list contains both aggregate functions and regular column names, all non-aggregated columns must be included in a GROUP BY clause. This clause tells the database how to group the rows before applying the aggregate function. Adding GROUP BY firstName, lastName resolves this fundamental error by instructing the database to calculate the sum of income for each unique combination of first and last names.

Why Incorrect

A. Adding a HAVING clause is used for filtering after aggregation, but it does not resolve the primary error of the missing GROUP BY clause.
C. While ORDER BY is the correct syntax for sorting (not SORT BY), this change alone does not fix the more critical aggregation error.
D. Simply removing the sorting clause does not address the core requirement of a GROUP BY clause when mixing aggregate and non-aggregate columns. ---

References

1. PostgreSQL 16 Documentation, Section 7.2.4. Aggregate Functions: "In an aggregate query, the SELECT list can only contain aggregate function calls and grouping columns... The reason for this restriction is that otherwise, there would be no way to decide which value of the non-aggregated column to return." This directly explains why the original query is invalid and why a GROUP BY clause is necessary.

2. Stanford University, CS 145 Introduction to Databases, Lecture Notes on "SQL - Aggregation and Grouping": The notes state the following rule: "If SELECT uses aggregation, then any attribute in SELECT list that is not aggregated must be in GROUP BY list." This academic source confirms the fundamental SQL principle violated by the original query.

3. Microsoft SQL Server Documentation, "GROUP BY (Transact-SQL)": "A SELECT statement clause that divides the query result into groups of rows... When a GROUP BY clause is used, each group has a single row that summarizes it, and the SELECT list can only contain... expressions from the GROUP BY clause [and] aggregate functions." This official vendor documentation reinforces the requirement for non-aggregated columns in the SELECT list to be present in the GROUP BY clause.

Scroll to Top