Bird
Raised Fist0
PostgreSQLquery~10 mins

Boolean type behavior in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Boolean type behavior
Input Boolean Value
↓
Evaluate Boolean Expression
↓
Result is TRUE, FALSE, or NULL
↓
Use Result in Query Condition or Output
Boolean values in PostgreSQL are evaluated as TRUE, FALSE, or NULL and control query logic or output accordingly.
Execution Sample
PostgreSQL
SELECT TRUE AS val_true, FALSE AS val_false, NOT TRUE AS not_true;
This query shows basic Boolean values and the NOT operator result.
Execution Table
StepExpressionEvaluationResult
1TRUEBoolean literal TRUEtrue
2FALSEBoolean literal FALSEfalse
3NOT TRUENegation of TRUEfalse
4End of evaluationAll expressions evaluatedQuery returns one row with three columns
💡 All Boolean expressions evaluated and returned as query output.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
val_trueundefinedtruetruetruetrue
val_falseundefinedundefinedfalsefalsefalse
not_trueundefinedundefinedundefinedfalsefalse
Key Moments - 2 Insights
Why does NOT TRUE become false instead of true?
NOT reverses the Boolean value. Since TRUE is true, NOT TRUE becomes false as shown in step 3 of the execution_table.
Can Boolean values be NULL in PostgreSQL?
Yes, Boolean can be NULL representing unknown. This is different from TRUE or FALSE and affects conditions in queries.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of NOT TRUE at step 3?
Atrue
Bfalse
CNULL
DError
💡 Hint
Check the 'Result' column for step 3 in the execution_table.
At which step does the query return all Boolean values?
AStep 1
BStep 3
CStep 4
DStep 2
💡 Hint
Look for the step where the note says 'Query returns one row with three columns'.
If we add NOT FALSE to the query, what would be the result?
Atrue
Bfalse
CNULL
DError
💡 Hint
NOT reverses the Boolean value; FALSE becomes true.
Concept Snapshot
Boolean type in PostgreSQL stores TRUE, FALSE, or NULL.
Use TRUE and FALSE literals directly.
NOT operator reverses Boolean values.
NULL means unknown, different from FALSE.
Boolean values control query logic and conditions.
Full Transcript
This visual execution shows how PostgreSQL handles Boolean values. The query selects TRUE, FALSE, and NOT TRUE. Step by step, TRUE evaluates to true, FALSE to false, and NOT TRUE to false. The final output is a single row with these Boolean results. Key points include that NOT reverses Boolean values and that Boolean can also be NULL, representing unknown. This affects how conditions work in queries.

Practice

(1/5)
1. In PostgreSQL, which of the following is a valid boolean value stored in a boolean column?
easy
A. TRUE
B. 2
C. 'foo'
D. 'NULL'

Solution

  1. Step 1: Understand boolean data type values

    PostgreSQL boolean columns accept TRUE, FALSE, or NULL (unknown).
  2. Step 2: Identify valid boolean literals

    TRUE and FALSE are valid boolean literals; 2, 'foo', and 'NULL' are not valid boolean literals in PostgreSQL.
  3. Final Answer:

    TRUE -> Option A
  4. Quick Check:

    Boolean values = TRUE or FALSE [OK]
Hint: Boolean columns accept TRUE, FALSE, or NULL only [OK]
Common Mistakes:
  • Using 1 or 0 as boolean values directly
  • Using strings like 'yes' or 'no' as boolean
  • Confusing NULL with a boolean TRUE or FALSE
2. Which of the following is the correct way to declare a boolean column named is_active in a PostgreSQL table?
easy
A. CREATE TABLE users (is_active BOOLEAN(1));
B. CREATE TABLE users (is_active BOOLEAN);
C. CREATE TABLE users (is_active BOOL(1));
D. CREATE TABLE users (is_active BOOLIAN);

Solution

  1. Step 1: Check correct boolean type syntax

    PostgreSQL uses BOOLEAN or BOOL as valid types; BOOLEAN(1) is invalid.
  2. Step 2: Identify syntax errors

    A uses invalid BOOLEAN(1) syntax; B has a typo 'BOOLIAN'; C uses invalid BOOL(1) syntax.
  3. Final Answer:

    CREATE TABLE users (is_active BOOLEAN); -> Option B
  4. Quick Check:

    BOOLEAN keyword is correct type [OK]
Hint: Use BOOLEAN or BOOL, no size or typo allowed [OK]
Common Mistakes:
  • Typo in BOOLEAN keyword
  • Adding size like BOOLEAN(1)
  • Using unsupported types like BOOLIAN
3. Given the table settings with a boolean column enabled, what will this query return?
SELECT enabled FROM settings WHERE enabled = FALSE;
medium
A. All rows where enabled is TRUE
B. All rows where enabled is NULL
C. All rows where enabled is FALSE
D. Syntax error

Solution

  1. Step 1: Understand the WHERE clause condition

    The query filters rows where the boolean column enabled equals FALSE.
  2. Step 2: Determine which rows match

    Only rows with enabled = FALSE will be returned; TRUE or NULL rows are excluded.
  3. Final Answer:

    All rows where enabled is FALSE -> Option C
  4. Quick Check:

    WHERE enabled = FALSE returns FALSE rows [OK]
Hint: WHERE boolean_column = FALSE returns only FALSE rows [OK]
Common Mistakes:
  • Assuming NULL rows are included
  • Confusing TRUE and FALSE values
  • Thinking it returns all rows
4. You wrote this query but it returns no rows:
SELECT * FROM users WHERE is_admin = TRUE;

The is_admin column is boolean but some rows have NULL. What is the likely issue?
medium
A. TRUE is not a valid boolean value in PostgreSQL
B. The query syntax is incorrect
C. The column should be text, not boolean
D. NULL values cause the condition to fail, so no rows with NULL appear

Solution

  1. Step 1: Understand boolean NULL behavior in WHERE clause

    Comparing boolean column to TRUE excludes NULL rows because NULL means unknown, not TRUE.
  2. Step 2: Explain why no rows returned

    Rows with NULL in is_admin do not satisfy is_admin = TRUE, so they are excluded.
  3. Final Answer:

    NULL values cause the condition to fail, so no rows with NULL appear -> Option D
  4. Quick Check:

    NULL in boolean = TRUE returns no rows [OK]
Hint: NULL in boolean columns excludes rows in equality checks [OK]
Common Mistakes:
  • Thinking NULL equals TRUE
  • Assuming TRUE is invalid boolean
  • Believing syntax error caused no rows
5. You want to select all rows from tasks where the boolean column completed is either FALSE or NULL. Which query correctly does this?
hard
A. SELECT * FROM tasks WHERE completed = FALSE OR completed IS NULL;
B. SELECT * FROM tasks WHERE completed = FALSE AND completed IS NULL;
C. SELECT * FROM tasks WHERE NOT completed;
D. SELECT * FROM tasks WHERE completed != TRUE;

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    SELECT * FROM tasks WHERE completed = FALSE OR completed IS NULL; -> Option A
  4. Quick Check:

    Use OR to include FALSE or NULL rows [OK]
Hint: Use OR with IS NULL to include unknown boolean values [OK]
Common Mistakes:
  • Using AND instead of OR
  • Ignoring NULL rows in boolean filters
  • Using NOT completed excludes NULL rows
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard