Choosing columns
SQL · Unit 1: First queries
- Free
- SQL
- 11 exercises
The store is sending out a notice and only needs the names. With *, each person's city and age would come along too, and nobody asked for those.
Instead of *, write the column you want. You get the same rows, with that one value.
SELECT name
FROM customers;Returns
| name |
|---|
| Ann |
| Luke |
| Chloë |
| Mason |
| Valerie |
| Dylan |
| Hannah |
| Brian |
A price without its product's name is no use to anyone. To see several columns, separate them with commas.
No comma after the last one: what comes next is the FROM.
SELECT name, price
FROM products;Returns
| name | price |
|---|---|
| Keyboard | 45.5 |
| Mouse | 19.9 |
| Monitor | 189 |
| Cable | 5.25 |
| Laptop | 899 |
| Tablet | 320 |
| Headphones | 59.9 |
| Speaker | 74 |
Columns come out in the order you ask for them, not the order the table has them in. You design the result.
In customers, name comes before age. Here they come out the other way around because that's how they were asked for.
SELECT age, name
FROM customers;Returns
| age | name |
|---|---|
| 34 | Ann |
| 27 | Luke |
| 41 | Chloë |
| 19 | Mason |
| 52 | Valerie |
| 27 | Dylan |
| NULL | Hannah |
| 38 | Brian |
SELECT * is for taking a peek at a table. In a query that's going to stick around, like the one behind a report, name the columns.
The reason is that tables change. If someone adds a column tomorrow, your * starts returning it too, and nobody decided that.
When there are a lot of columns, put one per line, with two spaces in front. It reads from top to bottom, and removing or adding one means touching a single line.
SQL also doesn't care whether you write SELECT or select. The uppercase is for whoever reads it: at a glance it separates SQL's words from your names.
select name,
city,
age
from customers;Returns
| name | city | age |
|---|---|---|
| Ann | Denver | 34 |
| Luke | Austin | 27 |
| Chloë | NULL | 41 |
| Mason | Tampa | 19 |
| Valerie | Denver | 52 |
| Dylan | Boston | 27 |
| Hannah | Austin | NULL |
| Brian | Denver | 38 |
If you ask for a column the table doesn't have, the query won't run. The message is no such column, with the name SQL couldn't find.
This error does point at the exact word. It's almost always one letter too many or a column from another table.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Complete the code
Which cities does the store send packages to? Fill in the blank to see only that.
SELECT ___ FROM customers;2. Multiple choice
The store has 8 products and someone asks for only the prices. What does this query return?
SELECT price FROM products;3. Complete the code
In the warehouse, every box is marked with its product's number. Fill in the blank to see that number next to the name.
SELECT id___ name FROM products;4. Put the lines in order
The store wants to know how old its customers are. Put the query in order to see each one's name and age. Two lines don't belong.
5. Find the bug
The shelf signs need each product's name and category. SQL complains about the word FROM. Which line is the real error on?
SELECT name, category, FROM products;6. Multiple choice
In the
productstable, thenamecolumn comes beforestock. In this query's result, which column ends up on the left?SELECT stock, name FROM products;7. Complete the code
The warehouse sorts outgoing packages by city, so its list reads better with the city first and the name after. Fill in the blanks.
SELECT ___, ___ FROM customers;8. Multiple choice
The store's weekly report uses
SELECT * FROM customers;. One day someone adds aphonecolumn to the table. What happens to the report?9. Put the lines in order
The catalog lists each product's name, category, and price, in that order. Put the query in order, with one column per line. One line doesn't belong.
10. Find the bug
Someone wants to compare prices by section, with each product's category and price. Which line has the error?
SELECT category, prices FROM products;11. Write the code
The shelf is being rearranged by section. Write a query that shows two columns for each product, first its
categoryand then itsname.SELECT FROM products;
It opens in your browser. It is free and you do not need an account to start.