Complete the code to select employees whose salary is greater than the average salary in their department.
SELECT emp_name FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.[1]);The correlated subquery compares each employee's salary to the average salary of their own department. The correlation happens via dept_id.
Complete the code to find products with price higher than the average price in their category.
SELECT product_name FROM products p WHERE price > (SELECT AVG(price) FROM products WHERE category_id = p.[1]);The subquery is correlated by matching the category ID of the outer product to compute the average price per category.
Fix the error in the correlated subquery to find customers who placed orders with total amount greater than average order amount for that customer.
SELECT customer_id FROM orders o WHERE total_amount > (SELECT AVG(total_amount) FROM orders WHERE customer_id = o.[1]);The correlation must be on customer_id to compare each order's total amount to the average for that customer.
Fill both blanks to select employees whose salary is above the average salary in their department and who joined after 2010.
SELECT emp_name FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.[1]) AND join_year [2] 2010;
The first blank correlates the subquery by department ID. The second blank uses '>' to filter employees who joined after 2010.
Fill all three blanks to select products with price above average in their category and stock quantity greater than 50.
SELECT product_name FROM products p WHERE price > (SELECT AVG(price) FROM products WHERE category_id = p.[1]) AND stock_quantity [2] [3];
The first blank correlates by category ID. The second blank uses '>' to filter stock quantity, and the third blank is the number 50.