Pull rows out, then filter them

INPUT · Slides

Line the columns up

01 / 06

When one column is not enough

A column of prices on its own does not tell you whose price is whose. You want the name and the price together.

When you do, separate the column names with a , (comma).

SELECT title, pages FROM books;

Result

The Star Map | 210
The Lost Kitten | 96
Wonders of Science | 320
The Town Under the Sea | 180
Morning at the Bakery | 128

02 / 06

As many as you like

Join them with commas and you can take three, or four. The rule is that the last column name gets no comma after it.

Leave one there and it errors, so watch out when you add another one on.

SELECT title, author, shelf FROM books;

03 / 06

The columns come out in the order you wrote

The columns of the result line up from the left in the order you wrote them in SELECT. The order inside the table has nothing to do with it.

Which means you get to decide how it reads.

SELECT pages, title FROM books;

Result

210 | The Star Map
96 | The Lost Kitten
320 | Wonders of Science
180 | The Town Under the Sea
128 | Morning at the Bakery

04 / 06

You may write the same column twice

It may sound odd, but naming the same column twice is not an error. You get as many columns as you wrote.

You would never do it on purpose, but it shows nicely how the thing works: "SELECT lines up the columns you asked for and hands them back", and that is all.

SELECT title, title FROM books;

05 / 06

Break the line when it gets long

More columns means a longer line, and a long line is hard to read. SQL lets you break the line anywhere, so fold it at a natural break.

As long as the ; is at the end, line breaks and spaces along the way change nothing. Laying it out for a human to read is a favour to your future self.

SELECT  title,  author,  pagesFROM books;

06 / 06

* is the shortcut for "all of them"

When you want every column, write * instead of listing the names. That is the one from the first lesson.

  • Checking what is in there … pour it all out with *
  • Making a table for someone to read … list only the columns you need

Pick by what you are doing. Let us write some.

SELECT * FROM books;