List Unique Product Categories
EasySQLPractice library · +10 XPdistinct valuessqlproducts
A retail store wants to understand the variety of products it offers.
Consider the following table:
products(product_id integer, category varchar(50), price numeric(10,2))
Return a list of all unique product categories. The results should be ordered alphabetically by category name. NULL categories should be ignored. Empty results are possible if no products exist.
Tables
CREATE TABLE products ( product_id integer PRIMARY KEY, category varchar(50), price numeric(10,2) );
Example data
INSERT INTO products (product_id, category, price) VALUES (1, 'Electronics', 199.99), (2, 'Clothing', 49.50), (3, 'Electronics', 299.00), (4, 'Home Goods', 75.20), (5, 'Clothing', 25.00), (6, 'Books', 15.99), (7, 'Electronics', 99.99), (8, 'Home Goods', 120.00);
Your query is also checked on hidden datasets with different data.
Hints
SELECT DISTINCT category
FROM products
WHERE category IS NOT NULL
ORDER BY category;
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.