Bird
0
0

What is wrong with the following SQL query?

medium📝 Debug Q6 of 15
SQL - GROUP BY and HAVING
What is wrong with the following SQL query?

SELECT team, position, AVG(score) FROM players GROUP BY team;
AThe query is missing position in the GROUP BY clause.
BAVG(score) cannot be used with GROUP BY.
Cteam should not be in the SELECT clause.
DThere is no error; the query is correct.
Step-by-Step Solution
Solution:
  1. Step 1: Identify selected columns

    The query selects team, position, and AVG(score).
  2. Step 2: Check GROUP BY clause

    GROUP BY only includes team, but position is also selected without aggregation.
  3. Step 3: Understand SQL rules

    All non-aggregated columns in SELECT must be in GROUP BY.
  4. Final Answer:

    The query is missing position in the GROUP BY clause. -> Option A
  5. Quick Check:

    Non-aggregated columns must be grouped [OK]
Quick Trick: Include all non-aggregated columns in GROUP BY [OK]
Common Mistakes:
MISTAKES
  • Forgetting to add all selected non-aggregated columns to GROUP BY
  • Assuming aggregate functions restrict GROUP BY columns
  • Thinking GROUP BY can be omitted if aggregate functions are used

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes