Learn how to use the PostgreSQL BOOLEAN data type to store TRUE, FALSE, and NULL values, with practical examples of inserting, retrieving, and updating Boolean data.
In real life, many questions have only two possible answers:
In PostgreSQL, we use the BOOLEAN data type to store these two-state values.
PostgreSQL supports three Boolean states:
| Value | Meaning |
|---|---|
TRUE |
Yes |
FALSE |
No |
NULL |
Unknown or not provided |
Important: NULL is not the same as FALSE. It means that the value is unknown or missing.
Let’s create a student table:
CREATE TABLE students (
student_id INTEGER,
student_name VARCHAR(100),
fee_paid BOOLEAN,
is_active BOOLEAN
);
Here:
student_id stores the student’s ID.student_name stores the student’s name.fee_paid stores whether the fee has been paid.is_active stores whether the student is active.INSERT INTO students
VALUES
(1, 'Ali', TRUE, TRUE),
(2, 'Ahmed', FALSE, TRUE),
(3, 'Sara', TRUE, FALSE),
(4, 'Ayesha', NULL, TRUE);
PostgreSQL also accepts these Boolean representations:
| TRUE | FALSE |
|---|---|
TRUE |
FALSE |
't' |
'f' |
'yes' |
'no' |
'1' |
'0' |
For beginners, use TRUE and FALSE because they are clear and readable.
Find students whose fees have been paid:
SELECT *
FROM students
WHERE fee_paid = TRUE;
You can also write:
SELECT *
FROM students
WHERE fee_paid;
Find students whose fees have not been paid:
SELECT *
FROM students
WHERE fee_paid = FALSE;
Find students with unknown fee status:
SELECT *
FROM students
WHERE fee_paid IS NULL;
Find students with a known fee status:
SELECT *
FROM students
WHERE fee_paid IS NOT NULL;
Suppose a student has paid the fee. We can update the record:
UPDATE students
SET fee_paid = TRUE
WHERE student_id = 2;
To deactivate a student:
UPDATE students
SET is_active = FALSE
WHERE student_id = 1;
| Feature | BOOLEAN | INTEGER |
|---|---|---|
| Stores | True/False | Whole numbers |
| Example | TRUE |
1 |
| Typical use | Fee paid or not | Student age |
| Storage | 1 byte | 4 bytes |
| Main purpose | Logical values | Numeric values |
Use BOOLEAN when a field represents a yes/no condition. Do not use INTEGER just to store 1 or 0 for such conditions.
Create a table named courses with the following columns:
| Column | Data Type |
|---|---|
| course_id | INTEGER |
| course_name | VARCHAR(100) |
| is_active | BOOLEAN |
| is_compulsory | BOOLEAN |
Tasks:
Question 1. Which PostgreSQL data type stores TRUE or FALSE values?
A. INTEGER
B. BOOLEAN
C. VARCHAR
D. NUMERIC
Question 2. Which value represents an unknown Boolean state?
A. FALSE
B. 0
C. NULL
D. TRUE
Question 3. Which condition finds records where fee_paid is unknown?
A. fee_paid = FALSE
B. fee_paid IS NULL
C. fee_paid = TRUE
D. fee_paid = 0
0 of 3 answered
Check answers
BOOLEAN stores logical values.TRUE, FALSE, and NULL.IS NULL to check unknown values.IS NOT NULL to check known values.BOOLEAN over INTEGER for yes/no conditions.Next lecture: PostgreSQL Constraints — NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK.
Reference: PostgreSQL Official Documentation: Boolean Type