text to sql syntaxTue, 08 Nov 2022

Write an SQL query that, for each city finds the 3 cheapest flats located in each city. In case of a tie, the query may return any three flats with the cheapest prices. The result table should contain three columns: id, city and price and should be sorted by id column. If the city has less than three flats for sale, the result table should contain all of them. Please use an OVER clause to simplify your solution.

SELECT id, city, price FROM flats WHERE price = (SELECT MIN(price) OVER(PARTITION BY city) FROM flats) ORDER BY id;

Questions about programming?Chat with your personal AI assistant