Full answer
Each filter is written so that a missing value makes its condition true for every row:
(:city IS NULL OR city = :city)If the user leaves city empty, :city IS NULL is true and the filter switches itself off. If they choose a city, only matching rows pass.
To show it without an application, the parameters come from a one-row CTE here. First with only the year set:
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);WITH params(p_city, p_year) AS (SELECT NULL, 2)
SELECT s.name, s.city, s.year
FROM students s, params
WHERE (p_city IS NULL OR s.city = p_city)
AND (p_year IS NULL OR s.year = p_year)
ORDER BY s.name;| name | city | year |
|---|---|---|
| Ben | Leeds | 2 |
| Eli | Leeds | 2 |
Then with both filters set:
WITH params(p_city, p_year) AS (SELECT 'Pune', 1)
SELECT s.name, s.city, s.year
FROM students s, params
WHERE (p_city IS NULL OR s.city = p_city)
AND (p_year IS NULL OR s.year = p_year)
ORDER BY s.name;| name | city | year |
|---|---|---|
| Asha | Pune | 1 |
| Chen | Pune | 1 |
Then with neither, which returns everybody:
WITH params(p_city, p_year) AS (SELECT NULL, NULL)
SELECT s.name, s.city, s.year
FROM students s, params
WHERE (p_city IS NULL OR s.city = p_city)
AND (p_year IS NULL OR s.year = p_year)
ORDER BY s.name;| name | city | year |
|---|---|---|
| Asha | Pune | 1 |
| Ben | Leeds | 2 |
| Chen | Pune | 1 |
| Dara | NULL | 3 |
| Eli | Leeds | 2 |
The pattern has a cost worth raising in an interview. The database sees one query text for every combination of filters, so it may reuse one plan. That plan cannot assume the city filter is active, so it may not use an index on city even when a city is given. On small tables it does not matter. On large ones, build the WHERE clause in application code, adding AND city = ? only when a city is supplied. Each combination gets its own query text and its own plan, and values still go in as bound parameters, never pasted into the string.
Never build the filter by concatenating raw user text into SQL; that is how SQL injection happens.
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
WITH params(p_city, p_year) AS (SELECT NULL, 2)
SELECT s.name, s.city, s.year
FROM students s, params
WHERE (p_city IS NULL OR s.city = p_city)
AND (p_year IS NULL OR s.year = p_year)
ORDER BY s.name;
WITH params(p_city, p_year) AS (SELECT 'Pune', 1)
SELECT s.name, s.city, s.year
FROM students s, params
WHERE (p_city IS NULL OR s.city = p_city)
AND (p_year IS NULL OR s.year = p_year)
ORDER BY s.name;
WITH params(p_city, p_year) AS (SELECT NULL, NULL)
SELECT s.name, s.city, s.year
FROM students s, params
WHERE (p_city IS NULL OR s.city = p_city)
AND (p_year IS NULL OR s.year = p_year)
ORDER BY s.name;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- (:p IS NULL OR col = :p) disables a filter when the parameter is empty
- One query text can mean one plan for every combination
- Dynamic SQL with bound parameters often gives better plans
- Never concatenate user input into the SQL text
Common mistakes
- Using an empty string as 'no filter' and getting zero rows
- Building optional filters by string concatenation of user input
Follow-ups
- What is SQL injection, and how do bound parameters prevent it?
- How would you add an optional name search with LIKE?
Tips
- Mention the plan-sharing cost; it separates a good answer from a basic one