Boolean column filtering patterns in PostgreSQL - Time & Space Complexity
When we filter rows in a database using a true/false column, the time it takes can change as the table grows.
We want to know how the filtering time changes when the number of rows increases.
Analyze the time complexity of the following code snippet.
SELECT *
FROM orders
WHERE is_paid = true;
-- or
SELECT *
FROM orders
WHERE is_paid;
This code selects all rows where the boolean column is_paid is true.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row to check the boolean column.
- How many times: Once for every row in the table.
As the number of rows grows, the database checks more rows one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the table gets bigger.
[X] Wrong: "Filtering by a boolean column is instant no matter the table size."
[OK] Correct: The database still checks each row unless there is a special index, so time grows with the number of rows.
Understanding how filtering by boolean columns scales helps you explain query performance clearly and shows you know how databases handle data.
"What if we add an index on the boolean column? How would the time complexity change?"
Practice
is_active is true?Solution
Step 1: Understand boolean filtering for true values
To filter rows where a boolean column is true, you can useWHERE column = TRUEor simplyWHERE column.Step 2: Evaluate each option
WHERE is_active = TRUEusesWHERE is_active = TRUE, which is correct.WHERE is_active = FALSEfilters false values.WHERE NOT is_activefilters false values by negation.WHERE is_active = NULLcompares to NULL, which is invalid for boolean filtering.Final Answer:
WHERE is_active = TRUE -> Option DQuick Check:
Boolean true filter = WHERE column = TRUE [OK]
- Using WHERE column = FALSE to find true rows
- Using WHERE NOT column to find true rows
- Comparing boolean to NULL
is_verified is false in PostgreSQL?Solution
Step 1: Recall boolean false filtering syntax
To filter false boolean values, useWHERE column = FALSEorWHERE NOT column.Step 2: Check each option's syntax
WHERE NOT is_verifiedusesWHERE NOT is_verified, which is valid and filters false values.WHERE is_verified = TRUEfilters true values.WHERE is_verified == FALSEuses double equals which is invalid in SQL.WHERE is_verified = NULLcompares to NULL, which is incorrect.Final Answer:
WHERE NOT is_verified -> Option CQuick Check:
Boolean false filter = WHERE NOT column [OK]
- Using == instead of = in SQL
- Comparing boolean to NULL
- Using WHERE column = TRUE to find false rows
users with a boolean column is_admin, what will the following query return?SELECT username FROM users WHERE is_admin;
Solution
Step 1: Understand filtering by boolean column directly
In PostgreSQL, usingWHERE is_adminfilters rows whereis_adminis true.Step 2: Analyze the query result
The query selects usernames only whereis_adminis true, so it returns those usernames.Final Answer:
All usernames where is_admin is true -> Option AQuick Check:
WHERE boolean_column filters true rows [OK]
- Thinking WHERE boolean_column filters false rows
- Expecting syntax error without explicit comparison
- Assuming it returns all rows
is_active is false:SELECT * FROM accounts WHERE is_active = FALSE;
Solution
Step 1: Check boolean comparison syntax
In PostgreSQL, comparing a boolean column with FALSE using= FALSEis valid syntax.Step 2: Confirm query correctness
The query correctly filters rows whereis_activeis false. UsingWHERE NOT is_activeis an alternative but not required.Final Answer:
No error, query is correct -> Option AQuick Check:
Boolean = FALSE is valid syntax [OK]
- Thinking boolean cannot be compared with FALSE
- Believing parentheses are needed around FALSE
- Confusing NOT with = FALSE as mandatory
orders where the boolean column shipped is false or NULL. Which query correctly achieves this?Solution
Step 1: Understand boolean and NULL filtering
Boolean columns can be true, false, or NULL. To include false or NULL, both conditions must be checked.Step 2: Analyze each option
SELECT * FROM orders WHERE shipped = FALSE OR shipped IS NULL;explicitly checks for false or NULL, correctly including both.SELECT * FROM orders WHERE NOT shipped;filters only false but excludes NULL.SELECT * FROM orders WHERE shipped = FALSE;excludes NULL.SELECT * FROM orders WHERE shipped IS NULL;filters only NULL, missing false rows.Final Answer:
SELECT * FROM orders WHERE shipped = FALSE OR shipped IS NULL; -> Option BQuick Check:
Include false and NULL with OR condition [OK]
- Using NOT shipped excludes NULL rows
- Ignoring NULL values in boolean filtering
- Assuming shipped = FALSE includes NULL
Ready to prove it?
Pass the test and this pattern moves unknown
