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 compare 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 employee's employee_id matches the manager_id in departments. So, the employee column to compare 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.[1] = customers.[2] AND orders.total_amount > 1000);
The subquery matches orders.customer_id with customers.customer_id to find orders by the customer. So both blanks use customer_id.
Fill all three blanks to select products that appear in orders with quantity greater than 5.
SELECT product_id FROM products WHERE EXISTS (SELECT 1 FROM order_items WHERE order_items.[1] = products.[2] AND order_items.[3] > 5);
The subquery links order_items.product_id to products.product_id and checks if quantity is greater than 5.