Complete the code to select employees whose salary is greater than the average salary in their department.
SELECT employee_id, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.[1]);The subquery compares each employee's salary to the average salary of their own department. The correlation is done using department_id.
Complete the code to find customers who have placed more orders than the average number of orders per customer.
SELECT customer_id FROM orders o GROUP BY customer_id HAVING COUNT(*) > (SELECT AVG([1]) FROM (SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id) AS subquery);The subquery calculates the average number of orders per customer using the order_count column from the derived table.
Fix the error in the correlated subquery to find products priced higher than the average price in their category.
SELECT product_id, price FROM products p WHERE price > (SELECT AVG(price) FROM products WHERE category_id = [1].category_id);The subquery must refer to the alias of the outer query table, which is p, to correlate by category_id.
Fill both blanks to select employees who earn more than the average salary of their department and have a job title starting with 'Senior'.
SELECT employee_id, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.[1]) AND job_title [2] 'Senior%';
The first blank needs the department_id to correlate the subquery. The second blank uses LIKE to match job titles starting with 'Senior'.
Fill both blanks to select orders where the order amount is greater than the average order amount for the same customer and the order status is 'Completed'.
SELECT order_id, order_amount FROM orders o WHERE order_amount > (SELECT AVG(order_amount) FROM orders WHERE customer_id = o.[1]) AND order_status = [2];
The subquery correlates on customer_id. The order status comparison requires the string 'Completed' enclosed in single quotes.