Top 5 Most Expensive GadgetsEasy

Top 5 Most Expensive Gadgets

EasySQLPractice library · +10 XPsorting and limitssqlgadgets

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