Choosing columns
After this: Choose the columns of a result, use the star, and name a worked-out column with AS.
On this page · 6 sections · 3 checks
The school office is sending a letter to every student, and it needs two things for each one: the name and the city. The students table holds four things for each student: an id, a name, a year and a city. The office does not want the whole table. It wants two of its four columns.
This tutorial is about asking a table for exactly what you want: which columns, which rows, in what order, and how many. This first lesson starts with the columns.
A question that gets a table back
In the first tutorial you ran SELECT * FROM students; and got the whole table back. That was a : a question you send to the database. Every query gives back a table, called the . The result is a new table made for you. The students table itself does not change.
Here is the whole students table again, as the star gives it:
SELECT * FROM students;| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
Two columns, by hand
To get only names and cities, you name those two columns after SELECT:
SELECT name, cityFROM students;The database answers in two steps:
FROM studentsstarts with the whole table: 6 rows, 4 columns.SELECT name, citykeeps two of the columns, name and then city. The other two are left out of the result. All 6 rows are still there.
Each part of a query that starts with a keyword, such as FROM students, is called a . Press Run it and step through the two clauses.
SELECT name, cityFROM students;students · 6 rows
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
The rule
After SELECT, write the columns you want, with commas between them, in the order you want them. The result has those columns in that order. The star, *, means every column, in the order the table lists them.
Choosing columns never removes a row. A query with only SELECT and FROM gives back every row of the table.
SQL does not care about capital letters in keywords, or about line breaks: select name from students works too. This tutorial writes keywords in capitals and puts each clause on a line of its own, so you can see where one part ends and the next begins.
A new column, with a name
A column in the result does not have to be a column of the table. It can be worked out from one. This query gives each student a score of ten times their year:
SELECT name, year * 10 AS scoreFROM studentsWHERE id <= 2;| name | score |
|---|---|
| Ana | 20 |
| Ben | 10 |
FROM studentsstarts with all 6 rows.WHERE id <= 2keeps the rows whose id is at most 2: Ana and Ben. You metWHEREin the first tutorial; lesson 2 covers it fully.SELECTkeeps name, and works outyear * 10for each row: 2 × 10 = 20 for Ana, 1 × 10 = 10 for Ben.
The word AS gives the new column a name, score. A name given with AS is called an . Without one, SQLite here names the column after what you wrote. Other databases may choose another name, so write AS whenever the name matters:
SELECT name, year * 10FROM studentsWHERE id <= 2;| name | year * 10 |
|---|---|
| Ana | 20 |
| Ben | 10 |
Run it
The block below runs real SQLite inside this page, on the school tables. Press Run. Then change the columns: try SELECT city, name, or add year, or put back the star.
Every press of Run starts from a fresh copy of the tables, so nothing you type can break them.
Check yourself
The students table has 6 rows and 4 columns. What shape is the result of this query?
SELECT name, yearFROM students;What does the star mean in SELECT * FROM courses;?
What are the names of the two columns in this result?
SELECT title, credits * 2 AS hoursFROM coursesWHERE id <= 2;- Say what a query is: a question sent to the database, whose answer is always a table, the result.
- Choose columns by naming them after SELECT, in the order you want; the star means every column.
- Say that choosing columns never removes a row: SELECT with FROM alone gives every row of the table.
- Work out a new column, such as year * 10, and give it a name with AS.