Boolean type behavior in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to work with Boolean values changes as we use more data.
Specifically, how does checking or using Boolean values in queries grow when the data grows?
Analyze the time complexity of the following code snippet.
SELECT id, is_active
FROM users
WHERE is_active = TRUE;
This query selects user IDs where the Boolean column is_active is true.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the
userstable to check theis_activevalue. - How many times: Once for every row in the table.
As the number of users grows, the database checks more rows one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks of is_active |
| 100 | 100 checks of is_active |
| 1000 | 1000 checks of is_active |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Checking a Boolean column is instant no matter how big the table is."
[OK] Correct: Even though Booleans are simple, the database still looks at each row, so more rows mean more work.
Understanding how simple Boolean checks scale helps you explain query performance clearly and confidently.
"What if the is_active column had an index? How would the time complexity change?"
Practice
Solution
Step 1: Understand boolean data type values
PostgreSQL boolean columns accept TRUE, FALSE, or NULL (unknown).Step 2: Identify valid boolean literals
TRUE and FALSE are valid boolean literals; 2, 'foo', and 'NULL' are not valid boolean literals in PostgreSQL.Final Answer:
TRUE -> Option AQuick Check:
Boolean values = TRUE or FALSE [OK]
- Using 1 or 0 as boolean values directly
- Using strings like 'yes' or 'no' as boolean
- Confusing NULL with a boolean TRUE or FALSE
is_active in a PostgreSQL table?Solution
Step 1: Check correct boolean type syntax
PostgreSQL uses BOOLEAN or BOOL as valid types; BOOLEAN(1) is invalid.Step 2: Identify syntax errors
A uses invalid BOOLEAN(1) syntax; B has a typo 'BOOLIAN'; C uses invalid BOOL(1) syntax.Final Answer:
CREATE TABLE users (is_active BOOLEAN); -> Option BQuick Check:
BOOLEAN keyword is correct type [OK]
- Typo in BOOLEAN keyword
- Adding size like BOOLEAN(1)
- Using unsupported types like BOOLIAN
settings with a boolean column enabled, what will this query return?SELECT enabled FROM settings WHERE enabled = FALSE;
Solution
Step 1: Understand the WHERE clause condition
The query filters rows where the boolean column enabled equals FALSE.Step 2: Determine which rows match
Only rows with enabled = FALSE will be returned; TRUE or NULL rows are excluded.Final Answer:
All rows where enabled is FALSE -> Option CQuick Check:
WHERE enabled = FALSE returns FALSE rows [OK]
- Assuming NULL rows are included
- Confusing TRUE and FALSE values
- Thinking it returns all rows
SELECT * FROM users WHERE is_admin = TRUE;
The
is_admin column is boolean but some rows have NULL. What is the likely issue?Solution
Step 1: Understand boolean NULL behavior in WHERE clause
Comparing boolean column to TRUE excludes NULL rows because NULL means unknown, not TRUE.Step 2: Explain why no rows returned
Rows with NULL in is_admin do not satisfy is_admin = TRUE, so they are excluded.Final Answer:
NULL values cause the condition to fail, so no rows with NULL appear -> Option DQuick Check:
NULL in boolean = TRUE returns no rows [OK]
- Thinking NULL equals TRUE
- Assuming TRUE is invalid boolean
- Believing syntax error caused no rows
tasks where the boolean column completed is either FALSE or NULL. Which query correctly does this?Solution
Step 1: Understand boolean NULL and FALSE filtering
To get rows where completed is FALSE or NULL, use OR with completed = FALSE and completed IS NULL.Step 2: Analyze each option
SELECT * FROM tasks WHERE completed = FALSE AND completed IS NULL; uses AND, which cannot be both FALSE and NULL simultaneously. SELECT * FROM tasks WHERE NOT completed; excludes NULL rows. SELECT * FROM tasks WHERE completed != TRUE; excludes TRUE and NULL (since NULL != TRUE is unknown), includes only FALSE.Final Answer:
SELECT * FROM tasks WHERE completed = FALSE OR completed IS NULL; -> Option AQuick Check:
Use OR to include FALSE or NULL rows [OK]
- Using AND instead of OR
- Ignoring NULL rows in boolean filters
- Using NOT completed excludes NULL rows
Ready to prove it?
Pass the test and this pattern moves unknown
