SQL query-writing questions
Questions where you write the query aloud or on a whiteboard, with model answers.
SELECT c.* FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL;
SELECT DATE_FORMAT(created_at, '%Y-%m') AS month, SUM(total) FROM orders WHERE YEAR(created_at) = YEAR(CURDATE()) GROUP BY month ORDER BY month; (function names vary by database).
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); or use DENSE_RANK() in databases that support window functions.
First: SELECT COUNT(*) FROM orders WHERE created_at < DATE_SUB(CURDATE(), INTERVAL 1 YEAR); check the count; then run DELETE with the same WHERE inside a transaction.
SELECT c.name, SUM(o.total) AS spent FROM customers c JOIN orders o ON o.customer_id = c.id GROUP BY c.name ORDER BY spent DESC LIMIT 3;
Start from a statuses table (or a VALUES list) and LEFT JOIN orders, then GROUP BY status and COUNT(o.id). COUNT(*) would wrongly count the empty join as 1.
Practise out loud. Say each answer once as written, then again in your own words with your own number or story.
Have a number ready. "My SQL score is at benchmark on a timed test" is a stronger answer than "I am good at it". Take the test and quote the result.
More in Accounts and data