Top 3 Most Expensive Products
Easysql
Write a query returning the id, name, price of the 3 most expensive products, highest price first. Break ties by id ascending, so the result is fully deterministic.
sqlCREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, category TEXT NOT NULL, price INTEGER NOT NULL );
Sample data:
sqlINSERT INTO products (id, name, category, price) VALUES (1, 'ProLaptop', 'Electronics', 85000), (2, 'Desk Lamp', 'Home', 900), (3, 'ProPhone', 'Electronics', 45000), (4, 'Python Basics', 'Books', 350), (5, 'Novel', 'Books', 220), (6, 'Headphones', 'Electronics', 3000), (7, 'Rug', 'Home', 4500);
Example 1
Input
(none)
Output
id name price 1 ProLaptop 85000 3 ProPhone 45000 7 Rug 4500
LIMIT after ORDER BY gives a Top-N query; a secondary tiebreaker key (id) makes ties deterministic. Reference: SELECT id, name, price FROM products ORDER BY price DESC, id ASC LIMIT 3;