Before you start, you need
Choose columns and their table
To build a catalogue you need each product's name and price. The table also stores stock and categories, but there is no need to show everything. Choose the columns that answer the question.
SELECT goes before the names of those columns. FROM goes before the table. So SELECT name, price FROM products means “read the name and price of every product”.
Separate columns with commas and use their names exactly as they appear in the table. Without the comma, SELECT name price can be read as a single column with a different name. In the exercises you can open “View tables and data” to check the column headings.
Choosing fewer columns does not delete rows: we still see every product. SELECT * shows every column and is useful for exploring; once you know what you need, write the column names.
SELECT column_1, column_2
FROM table_name;See it in action
We want a simple catalogue with the name and price of each product.
SELECT name, price
FROM products;| name | price |
|---|---|
| Mouse | 24.9 |
| Keyboard | 49.5 |
| Monitor 24 | 179 |
| Pro Laptop | 999 |
| Headphones | 69.9 |
| Monitor 27 | 249 |
| SSD Drive | 89 |
| Cable USB-C | 12 |
- FROM products reads the nine rows of the catalogue. SELECT picks two properties of each product.
- The result has nine rows and two columns. It does not include stock because we did not ask for it.
- Separate name and price with a comma. Without it, name price could be interpreted as a renamed column rather than two columns.
- To explore every property you can use SELECT *. Then go back to a specific selection that answers the question.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Prepare a product list
Write a query that returns the name, category and stock of every product.
Expected result: 9 rows with the columns name, category, stock, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
You need three properties of each product: name, category and stock.
After SELECT, write name, category and stock, separated by commas.
Name the source with FROM products; you do not need to filter any rows.
Solution explained
SELECT name, category, stock FROM products;The query asks for exactly the three properties requested.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Build a catalogue
Show the id and name of every category. Keep that column order; any row order is fine.
Expected result: 5 rows with the columns id, name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Each row will still represent one category.
Write both properties in the select list.
The source is categories and the list starts with id.
Solution explained
SELECT id, name FROM categories;Two properties per category give two columns and five rows. SELECT does not reduce the number of categories.
Key takeaways
- SELECT picks the columns.
- FROM names the table.
- Separate columns with commas.