Bird
0
0

Find the error in this query:

medium📝 Debug Q7 of 15
SQL - Subqueries
Find the error in this query:
SELECT dept.name, emp_count FROM (SELECT department_id, COUNT(*) AS emp_count FROM employees GROUP BY department_id) dept_emp JOIN departments ON dept_emp.department_id = departments.id;
AGROUP BY clause is missing
BIncorrect alias for subquery
CMissing alias for departments table
DJOIN condition is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Check table aliases in query

    The subquery is aliased as dept_emp, but the departments table is used without alias.
  2. Step 2: Identify usage of alias in SELECT

    The SELECT clause uses 'dept.name' but 'dept' alias is not defined for departments table.
  3. Final Answer:

    Missing alias for departments table -> Option C
  4. Quick Check:

    Alias used in SELECT must be defined in FROM/JOIN [OK]
Quick Trick: Alias all tables used in SELECT clause [OK]
Common Mistakes:
MISTAKES
  • Using undefined alias in SELECT
  • Assuming alias is optional when referenced
  • Confusing alias names between tables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes