← Back to PostgreSQL Course | Chapter 2: Basic Queries | Lesson 2 of 7

WHERE clause

WHERE keeps only the rows that satisfy a condition.

In this page:

  1. WHERE clause
Syntax
sql
SELECT columns
FROM table_name
WHERE condition;

WHERE clause

Conditions compare columns to values and evaluate to true, false or NULL, and only true rows are returned. Text values use single quotes. WHERE runs before grouping and before column aliases are available. Combine conditions with AND, OR and NOT.

Note: Use single quotes for strings and double quotes only for identifiers.

Example: WHERE clause

sql
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, city TEXT, age INTEGER);
INSERT INTO users VALUES (1, 'Ada', 'London', 36), (2, 'Bob', 'Oslo', 25), (3, 'Cy', 'Oslo', 41), (4, 'Di', 'Rome', 30);
SELECT * FROM users WHERE city = 'Oslo';
SELECT name FROM users WHERE age > 30;

-- Output:
-- id | name | city | age
-- 2 | Bob | Oslo | 25
-- 3 | Cy | Oslo | 41
-- name
-- Ada
-- Cy
Related Topics
Common Mistakes
  1. Using double quotes for string values
  2. Comparing to NULL with =
  3. Using aliases in WHERE
Chapter Summary
  • WHERE filters rows
  • Text needs single quotes
  • NULL comparisons need IS NULL
  • Only true rows are returned
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.