Reading rows: SELECT, WHERE, ORDER BY, LIMIT

Most read queries use SELECT, WHERE, ORDER BY, and LIMIT. This lesson looks at what each clause does and how they work together.

Your first query

A query answers one question: which rows, and which of their columns, do I want? The shortest way to ask is SELECT * — every column — FROM one table.

1

-- Every column of every product. Run it with F9 or Ctrl/Cmd + Enter.
SELECT *
FROM products
LIMIT 20;
Run in Planche

Result — first 5 of 20 rows

idcategory_idnamedescriptionskupricecoststock_quantityweight_kgattributesis_activecreated_atupdated_at
116Glass Orchard (Paperback ed.)Glass Orchard (Paperback ed.) — built to last.SKU-00001913.72443.951796.18{"color":"green","warranty_months":12,"material":"fabric"}12024-01-13 07:13:522024-01-13 07:13:52
217Luma The GeneLuma The Gene — built to last.SKU-00002332.64161.72835.893{"color":"silver","warranty_months":24,"material":"steel"}12022-06-16 08:45:202022-06-16 08:45:20
321Quill HelmetQuill Helmet — designed for daily use.SKU-00003196.26118.253998.261{"color":"silver","warranty_months":24}12022-08-26 20:06:032022-08-26 20:06:03
416Kite Glass Orchard 3Kite Glass Orchard 3 — built to last.SKU-000041719.68845.912435.263{"color":"silver","warranty_months":24}02022-07-19 11:34:272022-07-19 11:34:27
517The Gene (3rd ed.)The Gene (3rd ed.) — a dependable everyday choice.SKU-00005122.869.53571.898{"color":"red","warranty_months":36,"material":"aluminium"}12022-05-21 10:53:402022-05-21 10:53:40

Opens Planche in a separate browser tab, with the sample database loaded and this query ready. Every example on the page reuses that tab, and nothing runs until you press F9.

The * means “all columns,” and LIMIT 20 means “return at most twenty rows.” When you only need to inspect a table, avoid loading every row: products can contain thousands, all of which would need to be transferred and rendered.

Planche also caps result sets (Settings › Safety › Row limit). Still, writing the limit yourself is useful because the app-level cap protects the browser, not the database server doing the work.

Choosing columns

SELECT * is for exploring. Once you know what you are after, name the columns: the result is narrower, easier to read, and the database moves less data.

2

SELECT name, sku, price, stock_quantity
FROM products
LIMIT 20;
Run in Planche

Result — first 5 of 20 rows

nameskupricestock_quantity
Glass Orchard (Paperback ed.)SKU-00001913.72179
Luma The GeneSKU-00002332.6483
Quill HelmetSKU-00003196.26399
Kite Glass Orchard 3SKU-000041719.68243
The Gene (3rd ed.)SKU-00005122.8357

The order in the SELECT list becomes the order of the result columns; the table's own column order does not matter. Use AS to rename a result column.

Filtering rows with WHERE

SELECT chooses columns; WHERE chooses rows. It takes a condition and keeps only the rows the condition is true for.

3

SELECT name, price, stock_quantity
FROM products
WHERE price > 100
LIMIT 20;
Run in Planche

Result — first 5 of 20 rows

namepricestock_quantity
Lean Startup (Deluxe ed.)107.3784
Pixel Sofa108.187
Terra Pillow Pro113.670
Pulse LED Strip114.86480
Brief Answers (3rd ed.)117.96194

The comparison operators are the ones you would guess: =, <> (or !=), <, <=, >, >=. Two of them behave differently from other languages and catch everyone once:

  • Equality is a single =, not ==.
  • Nothing equals NULL, not even NULL. WHERE weight_kg = NULL matches no rows at all, however many products have no weight recorded. The test you want is IS NULL / IS NOT NULL. The next lesson covers this properly.

Sorting with ORDER BY

Without an ORDER BY, row order is not defined. If the order matters, specify it; the order you happen to see can change when the data or an index changes.

4

SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 20;
Run in Planche

Result — first 5 of 20 rows

nameprice
Glide GPS Watch Pro2484.4
Terra 3D Puzzle2481.77
Aria Castle Kit2479.41
Aria LED Strip Pro2463.41
Nova Brief Answers2445.49

DESC sorts high to low; ASC, the default, sorts low to high. ORDER BY price DESC with LIMIT 20 returns the 20 most expensive products: the database sorts first, then takes the first 20 rows.

In Planche, clicking a result-column header cycles through ascending, descending, and unsorted order. The table is sorted on the server, and the generated SQL is available to inspect.

Sorting by more than one column

When you list several sort keys, each one orders rows tied on the preceding key.

5

SELECT category_id, name, price
FROM products
ORDER BY category_id, price DESC
LIMIT 30;
Run in Planche

Result — first 5 of 30 rows

category_idnameprice
2Aria Notebook2116.4
2Ember Laptop Plus2087.98
2Sol Notebook1982.34
2Sol Ultrabook1942.47
2Nimbus Ultrabook1933.06

This orders products by category and then, within each category, by price from high to low.

Expressions and aliases

The SELECT list can contain arithmetic, function calls, or any other expression that produces a value for each row. AS gives that result a name.

6

SELECT name,
       price,
       cost,
       ROUND(price - cost, 2) AS margin
FROM products
WHERE is_active = 1
ORDER BY margin DESC
LIMIT 15;
Run in Planche

Result — first 5 of 15 rows

namepricecostmargin
Ember Tab Plus2412.93984.941427.99
Aria LED Strip Pro2463.411098.691364.72
Luma Denim Jacket Pro2357.59993.241364.35
Drift A Winter Letter2422.51082.431340.07
Nova Brief Answers2445.491108.091337.4

ROUND(price - cost, 2) AS margin computes a column that exists nowhere in the table, and the name margin is then usable in ORDER BY. It is not usable in WHERE, which trips people up regularly. The reason is the order the database evaluates the clauses in:

FROM  ->  WHERE  ->  SELECT  ->  ORDER BY  ->  LIMIT

By the time ORDER BY runs, SELECT has produced margin. WHERE runs earlier, so WHERE margin > 50 is an error. Repeat the expression or wrap the query in another query. The same order explains why WHERE filters before grouping while HAVING filters afterward.

What you can do now

These clauses cover many one-table questions: the 20 most expensive products, items out of stock, or the widest margins in an active catalogue. Questions spanning two tables, such as which customer placed an order, need joins.

Try changing numbers and column names in the examples, then run them again to see how each clause changes the result.

Try it yourself

  1. The ten cheapest products that are still on sale, showing only the name and the price. (is_active is 1 when a product is on sale.)

    Show the answer

    Show the answer

    SELECT name, price
    FROM products
    WHERE is_active = 1
    ORDER BY price
    LIMIT 10;
    Run in Planche

    Result — first 5 of 10 rows

    nameprice
    Clean Code (2nd ed.)6.89
    Nimbus Desk Max7.21
    Drift Castle Kit Max17.35
    Pixel Laptop18.46
    Terra Dumbbell Set19.8

    Three clauses doing one job each: WHERE drops the products that are not on sale, ORDER BY price puts the cheapest first — ascending is the default, so no keyword is needed — and LIMIT 10 stops there.

  2. What is the stock value of each product? Show the name, the quantity, and price × quantity rounded to two decimals, with the biggest first.

    Show the answer

    Show the answer

    SELECT name,
           stock_quantity,
           ROUND(price * stock_quantity, 2) AS stock_value
    FROM products
    ORDER BY stock_value DESC
    LIMIT 10;
    Run in Planche

    Result — first 5 of 10 rows

    namestock_quantitystock_value
    Luma Boots4921042454.52
    Glide GPS Watch Pro4141028541.6
    Brief Answers (Deluxe ed.)434988448.02
    Brief Answers (Hardcover ed.)413980879.13
    Vega Field Watch464975221.28

    The third column exists nowhere in the table; AS stock_value names it, and because ORDER BY runs after SELECT, that name can be used to sort.