Bird
0
0

Identify the error in this query:

medium📝 Debug Q14 of 15
PostgreSQL - Subqueries in PostgreSQL
Identify the error in this query:
SELECT sub.name, sub.order_count FROM (SELECT name, COUNT(*) FROM users GROUP BY name) sub;
ANo error, query is correct
BMissing alias for COUNT(*) column
CIncorrect table name 'users'
DSubquery missing parentheses
Step-by-Step Solution
Solution:
  1. Step 1: Check subquery SELECT list

    COUNT(*) needs an alias to be referenced outside the subquery.
  2. Step 2: Understand outer query references

    Outer query uses sub.order_count, but COUNT(*) column has no alias, causing error.
  3. Final Answer:

    Missing alias for COUNT(*) column -> Option B
  4. Quick Check:

    Aggregate columns need aliases in subqueries [OK]
Quick Trick: Always alias aggregate columns in subqueries [OK]
Common Mistakes:
  • Forgetting to alias aggregate functions
  • Assuming subquery columns auto-named
  • Ignoring parentheses around subquery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes