Complete the code to select all customers who have at least one order.
SELECT customer_id FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.[1]);
The subquery checks if there is any order with the same customer_id as the customer. So, the correct column to compare is customer_id.
Complete the code to find products that have never been ordered.
SELECT product_id FROM products WHERE NOT EXISTS (SELECT 1 FROM order_items WHERE order_items.product_id = products.[1]);
The subquery checks if there is any order item with the same product_id as the product. To find products never ordered, we use NOT EXISTS and match on product_id.
Fix the error in the query to select employees who manage at least one department.
SELECT employee_id FROM employees WHERE EXISTS (SELECT 1 FROM departments WHERE departments.manager_id = employees.[1]);
The subquery checks if the manager_id in departments matches the employee_id in employees. So, the correct column to compare in employees is employee_id.
Fill both blanks to select customers who have placed orders with total amount greater than 1000.
SELECT customer_id FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.[1] AND orders.[2] > 1000);
The subquery checks orders where the customer_id matches the customer and the total_amount is greater than 1000.
Fill all three blanks to select products that have total ordered quantity greater than 10.
SELECT product_id FROM products WHERE EXISTS (SELECT 1 FROM order_items WHERE order_items.[1] = products.[2] GROUP BY order_items.[3] HAVING SUM(quantity) > 10);
The subquery matches order_items.product_id to products.product_id, groups by order_items.product_id, and filters groups with total quantity over 10.