PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL WHERE

PostgreSQL WHERE

Summary: in this tutorial, you’ll learn how to use PostgreSQL WHERE clause to filter rows returned from the SELECT statement.

In the previous tutorial, you’ve learned how to use the SELECT statement to query data from a table. What if you want to query just particular rows from a table? In this case, you need to use the WHERE clause in the SELECT statement.

Let’s take a look at the syntax of the PostgreSQL WHERE clause.

PostgreSQL WHERE Clause

The syntax of the PostgreSQL WHERE clause is as follows:

1
2
3
SELECT column_1, column_2 … column_n
FROM table_name
WHERE conditions;

The WHERE clause appears right after the FROM clause of the SELECT statement.  The conditions are used to filter the rows returned from the SELECT statement. PostgreSQL provides you with various standard operators  to construct the conditions.

The following table illustrates the standard comparison operators.

OperatorDescription
=Equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
<> or !=Not equal
ANDLogical operator AND
ORLogical operator OR

Let’s practice with some examples of using the WHERE clause with conditions.

PostgreSQL WHERE examples

If you want to get all customers whose first names are Jamie, you can use the WHERE clause with the equal (=) operator as follows:

1
2
3
SELECT last_name, first_name
FROM customer
WHERE first_name = 'Jamie';

If you want to select the customer whose first name is Jamie and last names is  rice, you can use the AND logical operator that combines two conditions as the following query:

1
2
3
4
SELECT last_name, first_name
FROM customer
WHERE first_name = 'Jamie' AND
last_name = 'Rice';

If you want to know who paid the rental with amount is either less than 1USD or greater than 8USD, you can use the following query with OR operator:

1
2
3
4
5
SELECT customer_id,
amount,
payment_date
FROM payment
WHERE amount <= 1 OR amount >= 8;

There are many ways to construct conditions with standard operators. In the next tutorials, you will also learn how to filter the rows based on pattern matching or using special operators such as BETWEEN, IN and IS. So take few minutes to practice with the sample database using the WHERE clause with comparison operators.

In this tutorial, you’ve learned how to use WHERE clause together with the SELECT statement to filter rows based on conditions constructed using standard operators.

Previous Tutorial: PostgreSQL ORDER BY
Next Tutorial: PostgreSQL LIMIT

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.