I want to return 3 rows from a table called cars and each car should have a distinct dealer_id , and I want them ordered by ID desc (so that the latest three cars added to the database are the ones returned)
SELECT id,dealer_id,name,model_year
FROM cars
GROUP by dealer_id
ORDER BY id DESC
LIMIT 3;
But this query is not returning the latest 3 cars from each distinct dealer_id
If you want the most recent records then you can use something like the following:
See SQL Fiddle with Demo