Complete the code to select all customers who have placed orders but exclude those who have returned items.
SELECT customer_id FROM orders WHERE status = 'placed' [1] SELECT customer_id FROM returns WHERE status = 'returned';
The EXCEPT keyword returns rows from the first query that are not in the second query. MySQL does not support EXCEPT directly, but this is the standard SQL syntax.
Complete the code to find products sold but not returned using a LEFT JOIN and filtering NULLs.
SELECT p.product_id FROM products p LEFT JOIN returns r ON p.product_id = r.product_id WHERE r.product_id [1];When using LEFT JOIN, rows from the left table without matches in the right table have NULLs. Filtering with IS NULL finds products not returned.
Fix the error in the query that tries to find employees who worked on projects but not on project 5.
SELECT employee_id FROM project_assignments WHERE project_id != [1];The query should exclude project 5 by using project_id != 5. Using NULL or quotes incorrectly causes errors or wrong results.
Fill both blanks to create a query that selects all students who took Math but not Science using NOT EXISTS.
SELECT student_id FROM enrollments e1 WHERE course = [1] AND NOT EXISTS (SELECT 1 FROM enrollments e2 WHERE e2.student_id = e1.student_id AND course = [2]);
The query finds students enrolled in Math but excludes those also enrolled in Science using NOT EXISTS.
Fill all three blanks to write a query that lists all employees who are in the sales department but not in the marketing department using a subquery with NOT IN.
SELECT employee_id FROM employees WHERE department = [1] AND employee_id NOT IN (SELECT employee_id FROM employees WHERE department = [2] AND status = [3]);
This query selects employees in Sales who are not in Marketing and have active status in the subquery.