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;
Result — first 5 of 20 rows
| id | category_id | name | description | sku | price | cost | stock_quantity | weight_kg | attributes | is_active | created_at | updated_at |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 16 | Glass Orchard (Paperback ed.) | Glass Orchard (Paperback ed.) — built to last. | SKU-00001 | 913.72 | 443.95 | 179 | 6.18 | {"color":"green","warranty_months":12,"material":"fabric"} | 1 | 2024-01-13 07:13:52 | 2024-01-13 07:13:52 |
| 2 | 17 | Luma The Gene | Luma The Gene — built to last. | SKU-00002 | 332.64 | 161.72 | 83 | 5.893 | {"color":"silver","warranty_months":24,"material":"steel"} | 1 | 2022-06-16 08:45:20 | 2022-06-16 08:45:20 |
| 3 | 21 | Quill Helmet | Quill Helmet — designed for daily use. | SKU-00003 | 196.26 | 118.25 | 399 | 8.261 | {"color":"silver","warranty_months":24} | 1 | 2022-08-26 20:06:03 | 2022-08-26 20:06:03 |
| 4 | 16 | Kite Glass Orchard 3 | Kite Glass Orchard 3 — built to last. | SKU-00004 | 1719.68 | 845.91 | 243 | 5.263 | {"color":"silver","warranty_months":24} | 0 | 2022-07-19 11:34:27 | 2022-07-19 11:34:27 |
| 5 | 17 | The Gene (3rd ed.) | The Gene (3rd ed.) — a dependable everyday choice. | SKU-00005 | 122.8 | 69.5 | 357 | 1.898 | {"color":"red","warranty_months":36,"material":"aluminium"} | 1 | 2022-05-21 10:53:40 | 2022-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;
Result — first 5 of 20 rows
| name | sku | price | stock_quantity |
|---|---|---|---|
| Glass Orchard (Paperback ed.) | SKU-00001 | 913.72 | 179 |
| Luma The Gene | SKU-00002 | 332.64 | 83 |
| Quill Helmet | SKU-00003 | 196.26 | 399 |
| Kite Glass Orchard 3 | SKU-00004 | 1719.68 | 243 |
| The Gene (3rd ed.) | SKU-00005 | 122.8 | 357 |
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;
Result — first 5 of 20 rows
| name | price | stock_quantity |
|---|---|---|
| Lean Startup (Deluxe ed.) | 107.37 | 84 |
| Pixel Sofa | 108.18 | 7 |
| Terra Pillow Pro | 113.67 | 0 |
| Pulse LED Strip | 114.86 | 480 |
| Brief Answers (3rd ed.) | 117.96 | 194 |
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 = NULLmatches no rows at all, however many products have no weight recorded. The test you want isIS 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;
Result — first 5 of 20 rows
| name | price |
|---|---|
| Glide GPS Watch Pro | 2484.4 |
| Terra 3D Puzzle | 2481.77 |
| Aria Castle Kit | 2479.41 |
| Aria LED Strip Pro | 2463.41 |
| Nova Brief Answers | 2445.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;
Result — first 5 of 30 rows
| category_id | name | price |
|---|---|---|
| 2 | Aria Notebook | 2116.4 |
| 2 | Ember Laptop Plus | 2087.98 |
| 2 | Sol Notebook | 1982.34 |
| 2 | Sol Ultrabook | 1942.47 |
| 2 | Nimbus Ultrabook | 1933.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;
Result — first 5 of 15 rows
| name | price | cost | margin |
|---|---|---|---|
| Ember Tab Plus | 2412.93 | 984.94 | 1427.99 |
| Aria LED Strip Pro | 2463.41 | 1098.69 | 1364.72 |
| Luma Denim Jacket Pro | 2357.59 | 993.24 | 1364.35 |
| Drift A Winter Letter | 2422.5 | 1082.43 | 1340.07 |
| Nova Brief Answers | 2445.49 | 1108.09 | 1337.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.