Your first query
After this: Run SELECT, FROM and WHERE in real SQLite, and step through the query clause by clause.
On this page · 5 sections · 3 checks
So far you have read the tables by eye. With six students that works. With sixty thousand you would ask the database instead, by sending it a query. This lesson writes your first two.
The smallest query
Here is a query that asks for the whole students table:
SELECT * FROM students;Read it as "select everything from students". It has three parts:
SELECTsays which columns you want.*means every column.FROM studentsnames the table to read.- The semicolon
;ends the query.
SELECT and FROM are : words with a fixed meaning in SQL. They are written in capitals by habit, so that they stand out from the names of tables and columns. SQL would also accept select * from students;.
The block below is real SQLite, running inside this page. Press Run, or Ctrl+Enter (Cmd+Enter on a Mac).
The rows the database gives back are called the . Here the result is the whole table: 6 rows and 4 columns. Change students to teachers and run it again to see the teachers.
Asking for one row
Now ask for one student's name, by her key:
SELECT name FROM students WHERE id = 3;WHERE id = 3 is a test. The database keeps only the rows that pass it. SELECT name keeps only the name column. Work it through by hand, in the order the database does it:
FROM students: start with all 6 rows.WHERE id = 3: test each row. Only Chloe's row passes, so 1 row is left.SELECT name: from that row, keep thenamecolumn. The result is one row: Chloe.
Notice that the database starts with FROM, although SELECT is written first. Step through the same query in the card below. It uses SELECT *, so the last step keeps every column of Chloe's row. Press Run it, then step forward, and watch WHERE strike out five rows.
SELECT *FROM studentsWHERE id = 3;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
Now run the version that asks for the name only:
Because id is the key, WHERE id = 3 can never give more than one row. It can give none: WHERE id = 9 gives an empty result, because no student has the id 9. That is not an error; the answer to the question is "nobody".
Try your own
A test can use any column. Text goes in single quotes, so that SQL can tell it from the name of a column:
Change 'Hull' to 'York', or name to *, and run it again. Try SELECT title FROM courses WHERE teacher_id = 1; to find Mrs Okafor's courses, as you did by eye in lesson 3.
Check yourself
The teachers table has three teachers, each with an id, a name and a room. What does this query give back?
SELECT * FROM teachers;What does this query give back?
SELECT name FROM students WHERE id = 5;The school has six students, numbered 1 to 6. What happens when this runs?
SELECT name FROM students WHERE id = 9;Honesty notes
- Write SELECT * FROM students; and say what SELECT, *, FROM and the semicolon each do.
- Add a test with WHERE, such as WHERE id = 3, and keep only the name column with SELECT name.
- Say the order the database works in: FROM, then WHERE, then SELECT, although SELECT is written first.
- Say that a test on the key gives one row or none, and that an empty result is an answer, not an error.