Skip to content
Laviyx Learn

Try

SQL Filtering and Sorting Practice

Practise DISTINCT, ORDER BY, LIMIT, AND/OR/NOT, NULL checks and column aliases on a product catalogue.

Beginner8 problemsUpdated

Review first: DISTINCT, ORDER BY, LIMIT, AND / OR / NOT, NULL Values, Aliases

All problems use this products table:

product_idnamecategorypricestocksupplier
1LaptopElectronics5500012TechWorld
2HeadphonesElectronics250040SoundCo
3Desk ChairFurniture70008NULL
4NotebookStationery60500PaperPlus
5MonitorElectronics120000TechWorld
6BookshelfFurniture45005NULL
7Pen SetStationery250120PaperPlus
8KeyboardElectronics180025TechWorld
Try your answers on these tables in the SQL Playground
  1. 1.List the categories

    Beginner

    Return each product category once, in alphabetical order.

    Show hint

    DISTINCT removes duplicate rows; ORDER BY sorts them.

    Show solution
    SQLRun
    SELECT DISTINCT category
    FROM products
    ORDER BY category;
    Output
    category
    -----------
    Electronics
    Furniture
    Stationery
  2. 2.Three most expensive products

    Beginner

    Return the name and price of the three most expensive products, most expensive first.

    Show hint

    Sort by price from high to low, then keep only the first three rows.

    Show solution
    SQLRun
    SELECT name, price
    FROM products
    ORDER BY price DESC
    LIMIT 3;
    Output
    name       | price
    -----------+------
    Laptop     | 55000
    Monitor    | 12000
    Desk Chair | 7000

    LIMIT works in PostgreSQL, MySQL and SQLite. SQL Server writes SELECT TOP 3 name, price ..., and Oracle (and standard SQL) adds FETCH FIRST 3 ROWS ONLY after ORDER BY. Without ORDER BY, "the first three rows" could be any three rows.

  3. 3.The second page of results

    Beginner

    A shop shows 3 products per page, sorted by price from high to low. Return the name and price of the products on page 2.

    Show hint

    Page 2 skips the first 3 rows. Combine LIMIT with OFFSET.

    Show solution
    SQLRun
    SELECT name, price
    FROM products
    ORDER BY price DESC
    LIMIT 3 OFFSET 3;
    Output
    name       | price
    -----------+------
    Bookshelf  | 4500
    Headphones | 2500
    Keyboard   | 1800

    For page n with k rows per page, use LIMIT k OFFSET (n - 1) * k. The ORDER BY is essential: without a fixed order, pages can overlap or skip rows.

  4. 4.Affordable electronics in stock

    Beginner

    Return the name, price and stock of Electronics products that are in stock and cost less than 20000. Sort by price.

    Show hint

    All three conditions must be true, so join them with AND.

    Show solution
    SQLRun
    SELECT name, price, stock
    FROM products
    WHERE category = 'Electronics'
      AND stock > 0
      AND price < 20000
    ORDER BY price;
    Output
    name       | price | stock
    -----------+-------+------
    Keyboard   | 1800  | 25
    Headphones | 2500  | 40

    The Monitor costs less than 20000 but has a stock of 0, so the stock > 0 condition removes it.

  5. 5.AND, OR and parentheses

    Beginner

    Return the name, category and price of products that are Electronics or Furniture and cost under 5000. Sort by price.

    Show hint

    AND is evaluated before OR. Use parentheses around the two category checks.

    Show solution
    SQLRun
    SELECT name, category, price
    FROM products
    WHERE (category = 'Electronics' OR category = 'Furniture')
      AND price < 5000
    ORDER BY price;
    Output
    name       | category    | price
    -----------+-------------+------
    Keyboard   | Electronics | 1800
    Headphones | Electronics | 2500
    Bookshelf  | Furniture   | 4500

    Without the parentheses, the condition is read as category = 'Electronics' OR (category = 'Furniture' AND price < 5000), which wrongly returns every Electronics product, including the Laptop and the Monitor. category IN ('Electronics', 'Furniture') is a shorter way to write the same check.

  6. 6.Products with no supplier

    Beginner

    Return the name of every product whose supplier is unknown (NULL).

    Show hint

    = NULL is never true. There is a special operator for NULL checks.

    Show solution
    SQLRun
    SELECT name
    FROM products
    WHERE supplier IS NULL
    ORDER BY name;
    Output
    name
    ----------
    Bookshelf
    Desk Chair

    Comparing anything with NULL using = gives unknown, not true, so WHERE supplier = NULL returns no rows at all. Always use IS NULL and IS NOT NULL.

  7. 7.Replace NULL with a label

    Beginner

    Return every product's name and its supplier as a column called supplier_name, showing Unknown where the supplier is NULL. Sort by product name.

    Show hint

    COALESCE returns its first argument that is not NULL.

    Show solution
    SQLRun
    SELECT name, COALESCE(supplier, 'Unknown') AS supplier_name
    FROM products
    ORDER BY name;
    Output
    name       | supplier_name
    -----------+--------------
    Bookshelf  | Unknown
    Desk Chair | Unknown
    Headphones | SoundCo
    Keyboard   | TechWorld
    Laptop     | TechWorld
    Monitor    | TechWorld
    Notebook   | PaperPlus
    Pen Set    | PaperPlus
  8. 8.Most valuable stock

    Beginner

    The value of a product's stock is price * stock. Return the product name as product and that value as stock_value for the three products with the highest stock value.

    Show hint

    Give the calculation an alias with AS, then sort by the alias.

    Show solution
    SQLRun
    SELECT name AS product, price * stock AS stock_value
    FROM products
    ORDER BY stock_value DESC
    LIMIT 3;
    Output
    product    | stock_value
    -----------+------------
    Laptop     | 660000
    Headphones | 100000
    Desk Chair | 56000

    A column alias can be used in ORDER BY because ORDER BY runs after SELECT. It cannot be used in WHERE, which runs before SELECT.