Top 5 Most Expensive Gadgets
A tech store wants to highlight its most premium products. Given a table of electronics, identify the top 5 most expensive gadgets.
Table: electronics
id(integer): Unique identifier for the electronic.name(varchar(100)): Name of the electronic.price(numeric(10,2)): Price of the electronic.
Return the id, name, and price of the top 5 most expensive gadgets, ordered by price in descending order. If there are fewer than 5 gadgets, return all of them. Ties in price should be handled by their id in ascending order. NULL prices should be ignored.
Tables
CREATE TABLE electronics (
id integer PRIMARY KEY,
name varchar(100) NOT NULL,
price numeric(10,2)
);Example data
INSERT INTO electronics (id, name, price) VALUES (1, 'Laptop', 1200.50), (2, 'Smartphone', 800.00), (3, 'Tablet', 450.75), (4, 'Smartwatch', 300.00), (5, 'Headphones', 150.00), (6, 'Bluetooth Speaker', 100.00), (7, 'Gaming PC', 2500.99), (8, 'Monitor', 500.00);
Your query is also checked on hidden datasets with different data.
Hints
-- Select the top 5 most expensive gadgets
SELECT
Your query runs on the example tables shown with the problem, then on hidden datasets when you submit.
Write standard SQL. MySQL habits work too: IFNULL, IF, YEAR(), DATE_FORMAT, GROUP_CONCAT, `backticks` and LIMIT 5, 10. It runs on PostgreSQL, so integer division keeps whole numbers (5 / 2 = 2): write 5.0 / 2 for 2.5.