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.
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.
Also asked at AutoSpot
- Tell us about yourself as a developer — experience, stack, projects.
- What are your salary expectations?
- Do you have experience writing and working with native SQL queries or similar low-level commands to a database?
- What AI tools do you use and for what tasks?
- What do you know about indexes? How can you determine which indexes are used in a query, and how can you tell which indexes are unnecessary or missing?
Similar questions
- Select all clients who have no orders (id, name). Select the top 100 clients by number of orders, and output client_id and the number of orders. Output only clients with more than 5 orders.
- 1. Select all clients who have no orders (c.*). 2. Select the top 100 clients by number of orders (client_id, count). Output only clients with more than 5 orders. 3. Select clients with duplicate names (c1.id, c2.id). Remove mirrored pairs.
- Select all clients and their entire first order (c.*, o.*).
- Select all clients who have no orders (c.*).
- Clients and orders 1. Select all clients who have no orders (all client columns). 2. Select the top 100 clients by number of orders (client_id, count). Output only clients with more than 5 orders. 3. Remove mirrored pairs of clients with identical names. 4. Select all clients and their entire first order (all client and order columns). 5. Calculate the proportion of clients whose average order amount over the last 30 days is greater than over the previous 30 days.