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

Comparison operators

Comparison operators test equality and ordering between values.

In this page:

  1. Comparison operators
Syntax
sql
WHERE column = value
WHERE column <> value
WHERE column BETWEEN a AND b
WHERE column IN (value1, value2)
WHERE column IS NULL

Comparison operators

Use =, <> (or !=), <, <=, >, >=, plus BETWEEN a AND b (inclusive), IN (list) and IS NULL. NOT can invert BETWEEN, IN and LIKE. Strings compare according to the database collation, so upper and lower case may sort differently than you expect.

Note: BETWEEN includes both boundary values.

Example: Comparison operators

sql
CREATE TABLE scores (id INTEGER PRIMARY KEY, name TEXT, points INTEGER);
INSERT INTO scores VALUES (1, 'Ada', 70), (2, 'Bob', 90), (3, 'Cy', 85), (4, 'Di', NULL);
SELECT name FROM scores WHERE points >= 85;
SELECT name FROM scores WHERE points BETWEEN 70 AND 85;
SELECT name FROM scores WHERE name IN ('Ada', 'Di');
SELECT name FROM scores WHERE points IS NULL;

-- Output:
-- name
-- Bob
-- Cy
-- name
-- Ada
-- Cy
-- name
-- Ada
-- Di
-- name
-- Di
Related Topics
Common Mistakes
  1. Using == instead of =
  2. Forgetting BETWEEN is inclusive
  3. Using = NULL instead of IS NULL
Chapter Summary
  • = <> < <= > >= compare values
  • BETWEEN is inclusive
  • IN tests a list
  • IS NULL tests missing values
🔒

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.