NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle+0 views

Question 2.1. What query would return the number of cars priced under one million for each dealer with Active status? Question 2.2. Keeping the condition from question 2.1, return only dealers with fewer than 10 such cars. Sort the dealers with the highest number of cars first.

Practice

Asked at

AI answer

Suppose the tables are named dealers(id, status) and cars(id, dealer_id, price). LEFT JOIN includes active dealers who have no matching cars.

2.1.

SELECT d.id, COUNT(c.id) AS car_count
FROM dealers AS d
LEFT JOIN cars AS c
  ON c.dealer_id = d.id
 AND c.price < 1000000
WHERE d.status = 'Active'
GROUP BY d.id;

2.2.

SELECT d.id, COUNT(c.id) AS car_count
FROM dealers AS d
LEFT JOIN cars AS c
  ON c.dealer_id = d.id
 AND c.price < 1000000
WHERE d.status = 'Active'
GROUP BY d.id
HAVING COUNT(c.id) < 10
ORDER BY car_count DESC;

If you need to display the dealer’s name, add it to SELECT and GROUP BY.