From departments, IDs with location 'LA' are 2 and 3.
Step 2: Find employees in those departments
Employees with department_id 2 and 3 are Bob and Carol.
Final Answer:
Bob, Carol -> Option A
Quick Check:
Subquery filters departments = Bob, Carol [OK]
Hint: Match outer query filter with subquery results [OK]
Common Mistakes:
Including employees from other departments
Ignoring subquery filtering
Assuming all employees are returned
4. Identify the error in this query:
SELECT name FROM employees WHERE department_id = SELECT id FROM departments WHERE location = 'NY';
medium
A. Missing WHERE clause in outer query
B. Using = instead of IN for multiple results
C. Missing parentheses around the subquery
D. Wrong table name 'employees'
Solution
Step 1: Check subquery syntax
The subquery must be enclosed in parentheses to be valid.
Step 2: Identify missing parentheses
The query lacks parentheses around SELECT id FROM departments WHERE location = 'NY'.
Final Answer:
Missing parentheses around the subquery -> Option C
Quick Check:
Subqueries need parentheses [OK]
Hint: Always wrap subqueries in parentheses [OK]
Common Mistakes:
Forgetting parentheses causes syntax error
Confusing = and IN without checking subquery results
Assuming table names are incorrect without checking
5. You want to find all employees who earn more than the average salary in their department. Which query correctly uses a subquery to achieve this?
hard
A. SELECT name, salary FROM employees WHERE salary > AVG(salary);
B. SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id);
C. SELECT name, salary FROM employees WHERE salary > (SELECT salary FROM employees);
D. SELECT name, salary FROM employees WHERE salary > ALL (SELECT AVG(salary) FROM employees GROUP BY department_id);
Solution
Step 1: Understand the problem
We want employees earning more than their department's average salary.
Step 2: Analyze subquery usage
SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id); uses a correlated subquery that calculates average salary per employee's department.
Step 3: Check other options
SELECT name, salary FROM employees WHERE salary > AVG(salary); lacks grouping, C compares salary to all salaries, D compares to all averages without correlation.
Final Answer:
SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id); -> Option B
Quick Check:
Correlated subquery per department = SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id); [OK]
Hint: Use correlated subqueries to compare per group [OK]
Common Mistakes:
Using aggregate functions without grouping
Not correlating subquery with outer query
Comparing salary to unrelated values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.