Pull rows out, then filter them

INPUT · Slides

Just the column you want

01 / 06

One instruction is called a query

A single instruction you send to a database is called a query. SELECT * FROM books; is a query, and a perfectly good one.

A query is shaped a lot like an English sentence. "From books, select everything" — you could already read it.

02 / 06

A column name instead of *

Put a column name where you were writing * and only that column comes out.

"From books, select title" — same way of reading it.

SELECT title FROM books;

Result

The Star Map
The Lost Kitten
Wonders of Science
The Town Under the Sea
Morning at the Bakery

03 / 06

Every row still comes out

Narrowing the columns does not touch the rows — all of them still come back. Only the columns get fewer.

Picking just some of the five books is something you will learn a little later on.

SELECT pages FROM books;

Result

210
96
320
180
128

04 / 06

The same value can appear twice

Take out one column and the same value may show up more than once. Look at the authors above: each name appears twice.

That is not a mistake. It is the shape of the table — one row per book — coming straight through. Tidying the repeats into a list is something for a later chapter.

05 / 06

Get the column name wrong and it stops

Write a column name that is not in the table and it errors on the spot. That means a typo shows up straight away.

The error says there is no such column, so check your spelling. Being stopped is far kinder than getting a strange answer.

06 / 06

Why pick out the columns you want

Pour everything out with * and the number you wanted is buried in the rest. Take only the columns you need and the result gets much easier to read.

The more data you handle, the bigger that difference gets. Against a table with millions of rows it is faster too, because the extra columns never get read. Let us write some.

SELECT author FROM books;

Result

Minato Asahi
Yui Kusunoki
Ren Todo
Minato Asahi
Yui Kusunoki