Skip to main content
0 reactions

How to Filter Rows with WHERE in PostgreSQL

Featured card for How to Filter Rows with WHERE in PostgreSQL, with a terminal window listing WHERE country =, ‘France’: 2 rows, ‘Spain’: 0 rows and an error for France without quotes.
How to filter rows with WHERE in PostgreSQL.

WHERE keeps only the rows that match a condition: SELECT * FROM cities WHERE country = 'France'; returns the French cities. Here’s how to filter rows with WHERE in PostgreSQL.

Every command and output on this page is real, from PostgreSQL 19 on macOS.

Syntax

SyntaxSQL
SELECT columns FROM table_name WHERE condition;
  • condition: a test for each row, such as country = 'France'. Only the rows where it’s true come back.
  • Text in a condition goes in single quotes; numbers don’t.

See The WHERE Clause (opens in a new tab) in the PostgreSQL documentation.

The Example

A table of cities and the countries they’re in.

Step 1: Create a Test Database

Open Terminal and connect to your PostgreSQL server with psql as the postgres user:

TerminalBash
psql -U postgres

Type the user’s password when psql asks for it. psql connects you to the postgres database, and its prompt is postgres=#.

Create a database for the example and connect to it:

postgres=#SQL
CREATE DATABASE atlas;
OutputPlain text
CREATE DATABASE
postgres=#Plain text
\c atlas
OutputPlain text
You are now connected to database "atlas" as user "postgres".

Create the table and add three cities:

atlas=#SQL
CREATE TABLE cities (name text, country text);
OutputPlain text
CREATE TABLE
atlas=#SQL
INSERT INTO cities (name, country) VALUES ('Lyon', 'France');
OutputPlain text
INSERT 0 1
atlas=#SQL
INSERT INTO cities (name, country) VALUES ('Porto', 'Portugal');
OutputPlain text
INSERT 0 1
atlas=#SQL
INSERT INTO cities (name, country) VALUES ('Nice', 'France');
OutputPlain text
INSERT 0 1

Step 2: Filter the Rows

atlas=#SQL
SELECT * FROM cities WHERE country = 'France';
OutputPlain text
 name | country
------+---------
 Lyon | France
 Nice | France
(2 rows)

Porto is left out, because its country isn’t France.

Step 3: A Condition No Row Matches

atlas=#SQL
SELECT * FROM cities WHERE country = 'Spain';
OutputPlain text
 name | country
------+---------
(0 rows)

No row matches, so you get (0 rows). That’s an answer, not an error.

Clean Up

To remove the example, connect to another database and drop the test database:

atlas=#Plain text
\c postgres
OutputPlain text
You are now connected to database "postgres" as user "postgres".
postgres=#SQL
DROP DATABASE atlas;
OutputPlain text
DROP DATABASE

Warning: DROP DATABASE deletes the database and everything in it, and it can’t be undone.

Common Mistakes

MistakeWhat happensDo this instead
Text without quotes: WHERE country = FranceERROR: column "france" does not existPut text in single quotes: 'France'. Without them, PostgreSQL reads a column name.

FAQ

Why does the error say france, in lowercase?

Because PostgreSQL read France as a column name, and it turns names typed without quotes into lowercase.

Conclusion

Add WHERE and a condition to a SELECT to get only the rows you want, with text values in single quotes.

Additional Resources

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

0 comments