Skip to the content

Choosing columns

SQL · Unit 1: First queries

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

nameprice
Keyboard45.5
Mouse19.9
Monitor189
Cable5.25
Laptop899
Tablet320
Headphones59.9
Speaker74

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

agename
34Ann
27Luke
41Chloë
19Mason
52Valerie
27Dylan
NULLHannah
38Brian

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

namecityage
AnnDenver34
LukeAustin27
ChloëNULL41
MasonTampa19
ValerieDenver52
DylanBoston27
HannahAustinNULL
BrianDenver38

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. 1. Complete the code

    Which cities does the store send packages to? Fill in the blank to see only that.

    SELECT ___
    FROM customers;
  2. 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. 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. 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. 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. 6. Multiple choice

    In the products table, the name column comes before stock. In this query's result, which column ends up on the left?

    SELECT stock, name
    FROM products;
  7. 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. 8. Multiple choice

    The store's weekly report uses SELECT * FROM customers;. One day someone adds a phone column to the table. What happens to the report?

  9. 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. 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. 11. Write the code

    The shelf is being rearranged by section. Write a query that shows two columns for each product, first its category and then its name.

    SELECT
    FROM products;
Do this lesson

It opens in your browser. It is free and you do not need an account to start.

See all lessons →