AI Hints
Home›PostgreSQL Tutorial›Querying Rows
PostgreSQL · Lesson 7 of 120

SELECT and WHERE

Filter the rows.

⏱ 2 min read▶ Video at 5:17🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

A shop with five hundred customers doesn't want all of them at once. Where keeps only the rows that pass a test, like customers from Canada.

  • 500 customers
  • Only Canada
  • WHERE

Filter with WHERE

Where keeps rows where country equals Canada. And adds a second test, joined after twenty twenty four started. Text values go in single quotes. Run it. Canadian customers who joined recently.

query.sqlsql
SELECT first_name, last_name, city, joined
FROM customers
WHERE country = 'Canada'
  AND joined >= '2024-01-01'
LIMIT 4;
Output
 first_name | last_name |   city    |   joined
------------+-----------+-----------+------------
 Tara       | Brown     | Toronto   | 2024-07-02
 Finn       | Haddad    | Toronto   | 2024-03-10
 Vera       | Evans     | Vancouver | 2025-05-10
 Finn       | Rossi     | Vancouver | 2025-01-18
(4 rows)

Comparisons

Equals and not equals compare values. Less than and greater than work on numbers, dates and text. In checks a list. Between checks a range. And i like matches text patterns, ignoring case.

keyvalue
= / <>equal / not equal
< > <= >=numbers, dates, text
IN (...)in a list
ILIKEpattern, any case

Summary

Where filters rows with tests, joined with and or or. Next, sorting and limiting.

Key takeaways
  • WHERE
  • AND / OR
  • ILIKE
Next up: ORDER BY and LIMIT — sort, then cut