Final challenge: the class list
After this: Write, predict and fix queries that choose, sort and cut rows of the school tables.
The school office has five last requests, and each one is a query. This challenge has five steps, each scored on its own. You will write a WHERE line for a class list, predict the best marks outside Maths, pick the query that finds the unmarked enrolment, predict a list sorted by two columns, and fix a query that compares with NULL. You can try as many times as you like, and hints work as they do in the lessons. To try a query before you answer, paste it into the playground.
Step 1 of 5The class list
The class list
Students: Ana (year 2, Leeds), Ben (1, York), Chloe (2, Leeds), Dev (3, Hull), Eve (1, York), Femi (3, Leeds). Write the WHERE line so that the query lists every student who is in year 3 or lives in York, by name: Ben, Dev, Eve, Femi.
SELECT nameFROM students________________________ -- ← the missing lineORDER BY name;Best marks outside Maths
Course 1 is Maths. The enrolments are (student, course, 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). What does this query give back?
SELECT student_id, course_id, markFROM enrolmentsWHERE course_id != 1ORDER BY mark DESCLIMIT 2;The unmarked enrolment
Which query gives back exactly one row, (3, 3): the enrolment that has no mark yet?
Outside Leeds, by year
Students: Ana (year 2, Leeds), Ben (1, York), Chloe (2, Leeds), Dev (3, Hull), Eve (1, York), Femi (3, Leeds). What does this query give back?
SELECT name, cityFROM studentsWHERE city != 'Leeds'ORDER BY year, name;Fix the query
A colleague wrote WHERE mark = NULL to find the unmarked enrolment, and got no rows. Write the WHERE line that gives (3, 3).
SELECT student_id, course_idFROM enrolments________________________ -- ← was: WHERE mark = NULL- Write a WHERE line that joins two tests with OR, and sort the result by name.
- Predict the rows of a query that filters, sorts by one or two columns and cuts with LIMIT, by working through its clauses in order.
- Find the unmarked row with IS NULL, and fix a query that compares with = NULL.