0
0
PostgreSQLquery~5 mins

Boolean column filtering patterns in PostgreSQL

Choose your learning style9 modes available
Introduction

Boolean columns store true or false values. Filtering by these helps find rows that match a yes/no condition.

Finding all active users where active = true
Selecting orders that are marked as shipped (shipped = true)
Filtering products that are on sale (on_sale = true)
Getting records where a feature is disabled (enabled = false)
Checking if a task is completed (completed = true)
Syntax
PostgreSQL
SELECT * FROM table_name WHERE boolean_column = TRUE;

-- or

SELECT * FROM table_name WHERE boolean_column = FALSE;

You can write WHERE boolean_column to filter for true values.

Use WHERE NOT boolean_column to filter for false values.

Examples
Get all users where the active column is true.
PostgreSQL
SELECT * FROM users WHERE active = TRUE;
Get all orders that are not shipped.
PostgreSQL
SELECT * FROM orders WHERE shipped = FALSE;
Shortcut to get products where on_sale is true.
PostgreSQL
SELECT * FROM products WHERE on_sale;
Get tasks that are not completed (completed = false).
PostgreSQL
SELECT * FROM tasks WHERE NOT completed;
Sample Program

This creates a temporary tasks table, inserts three tasks with true/false completed status, then selects only the completed tasks.

PostgreSQL
CREATE TEMP TABLE tasks (id SERIAL PRIMARY KEY, description TEXT, completed BOOLEAN);

INSERT INTO tasks (description, completed) VALUES
('Wash dishes', TRUE),
('Do homework', FALSE),
('Read book', TRUE);

SELECT id, description FROM tasks WHERE completed = TRUE ORDER BY id;
OutputSuccess
Important Notes

Boolean columns only store TRUE, FALSE, or NULL (unknown).

Be careful with NULL values; filtering with = TRUE excludes NULLs.

Use IS TRUE or IS FALSE to handle NULLs explicitly.

Summary

Boolean filtering helps select rows based on yes/no conditions.

Use WHERE column = TRUE or simply WHERE column for true values.

Use WHERE column = FALSE or WHERE NOT column for false values.