Final challenge: read the school
After this: Count rows, find keys, follow a teacher to her courses, and predict a refused row.
This challenge has five steps, and each step is scored on its own. You will count the rows of a table, find a table's key, follow a course to its teacher, predict the rows of a query, and predict what happens to a second student numbered 3. Each step shows the tables or the query it is about. You can try as many times as you like, and hints work as they do in the lessons.
If you want to look around first, the block below runs real SQLite on the school tables. Change students to teachers, courses or enrolments to see each table.
Step 1 of 5Count the rows
Count the rows
How many rows does the enrolments table have?
SELECT * FROM enrolments;Output10 rows
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | NULL |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
Find the key
The courses table has a primary key. Which column is it?
SELECT * FROM courses;Output5 rows
| id | title | credits | teacher_id |
|---|---|---|---|
| 1 | Maths | 4 | 1 |
| 2 | Physics | 4 | 1 |
| 3 | History | 3 | 2 |
| 4 | Art | 2 | 2 |
| 5 | Music | 2 | 3 |
Who teaches Music
Who teaches Music?
SELECT * FROM courses;Output5 rows
| id | title | credits | teacher_id |
|---|---|---|---|
| 1 | Maths | 4 | 1 |
| 2 | Physics | 4 | 1 |
| 3 | History | 3 | 2 |
| 4 | Art | 2 | 2 |
| 5 | Music | 2 | 3 |
SELECT * FROM teachers;Output3 rows
| id | name | room |
|---|---|---|
| 1 | Mrs Okafor | R1 |
| 2 | Mr Silva | R2 |
| 3 | Ms Chen | R3 |
Mrs Okafor's courses
Mrs Okafor's id is 1. What does this query give back?
SELECT title FROM courses WHERE teacher_id = 1;A second student 3
What happens when this runs on the school tables?
INSERT INTO students (id, name, year, city)VALUES (3, 'Zed', 1, 'York');- Count the rows of a table, find its key, follow a course to its teacher, predict the rows of a query, and predict the error for a repeated key.