Site icon JD Bots

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

Syntax

SELECT columns FROM table_name WHERE condition;

See The WHERE Clause 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:

Terminal

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=#

CREATE DATABASE atlas;

Output

CREATE DATABASE

postgres=#

\c atlas

Output

You are now connected to database "atlas" as user "postgres".

Create the table and add three cities:

atlas=#

CREATE TABLE cities (name text, country text);

Output

CREATE TABLE

atlas=#

INSERT INTO cities (name, country) VALUES ('Lyon', 'France');

Output

INSERT 0 1

atlas=#

INSERT INTO cities (name, country) VALUES ('Porto', 'Portugal');

Output

INSERT 0 1

atlas=#

INSERT INTO cities (name, country) VALUES ('Nice', 'France');

Output

INSERT 0 1

Step 2: Filter the Rows

atlas=#

SELECT * FROM cities WHERE country = 'France';

Output

 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=#

SELECT * FROM cities WHERE country = 'Spain';

Output

 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=#

\c postgres

Output

You are now connected to database "postgres" as user "postgres".

postgres=#

DROP DATABASE atlas;

Output

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.

Exit mobile version