Lesson 1 · Unit 01
Rows and filters
SELECT, WHERE, ORDER BY, and the trap called NULL
What you practise
- Tables, rows, columns, types and NULL
SELECT ... FROM ... WHERE,ORDER BY,LIMIT,DISTINCT,IS NULL,LIKE,IN- Three-valued logic bites
Try a question from this lesson
Pick one
Four pets are stored in a table. Which names does the SELECT print?
CREATE TABLE pets(name, kind, legs, age);
INSERT INTO pets VALUES
('Rex', 'dog', 4, 7),
('Tweety', 'bird', 2, 3),
('Tom', 'cat', 4, 5),
('Polly', 'bird', 2, 9);
SELECT name FROM pets WHERE legs = 4;Correct answer
Rex and Tom
Why
WHERE tests every row and keeps the ones where the test is true: the two pets with 4 legs. SELECT name then shows only that column, so you get names, not kinds.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Rows, columns, questions
A table is a grid. Each row is one thing; each column is one fact about it.
SELECT names the columns you want, FROM names the table, and WHERE keeps only the rows that pass a test. You say what you want. The database decides how to find it.
CREATE TABLE pets(name, kind, legs, age);
INSERT INTO pets VALUES
('Rex', 'dog', 4, 7),
('Tweety', 'bird', 2, 3),
('Tom', 'cat', 4, 5),
('Polly', 'bird', 2, 9);
SELECT name, age FROM pets
WHERE kind = 'bird';Concept
NULL means unknown
NULL is not zero and not an empty string. It means the value is unknown.
Any comparison with NULL (=, >, <>) is neither true nor false. It is unknown, and WHERE only keeps rows where the test is true.
To test for it, use IS NULL or IS NOT NULL.
CREATE TABLE users(id, name, age);
INSERT INTO users VALUES
(1, 'Ana', 31), (2, 'Bo', 25),
(3, 'Cy', NULL);
SELECT name FROM users
WHERE age IS NULL;10 more exercises in the app
- 2× type the answer
- 2× fill the blanks
- 3× multiple choice
- 1× true or false
- 1× match the pairs
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.