CodingNic

Querying Data

WHERE and Comparison Operators

Querying Data 10 min read

WHERE and Comparison Operators

Objectives

By the end of this lesson, you should be able to:

  • Filter rows with WHERE
  • Use every standard comparison operator
  • Explain how PostgreSQL evaluates a WHERE clause

💡 Why this matters: Every SELECT so far has returned an entire table. Real questions are almost always narrower, “which employees earn over 80000,” this lesson is where SELECT becomes genuinely precise.

⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database.

The Sample Table

The rest of this module queries this employees table:

sql
CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    email VARCHAR(150) UNIQUE NOT NULL,
    department VARCHAR(50),
    salary NUMERIC(10,2) NOT NULL,
    hire_date DATE NOT NULL
);

INSERT INTO employees (first_name, last_name, email, department, salary, hire_date) VALUES
    ('Erin', 'Castillo', 'erin.castillo@example.com', 'Engineering', 74000, '2021-03-15'),
    ('Jordan', 'Blake', 'jordan.blake@example.com', 'Sales', 61000, '2022-06-01'),
    ('Maya', 'Fischer', 'maya.fischer@example.com', 'Marketing', 67000, '2020-11-20'),
    ('Priya', 'Desai', 'priya.desai@example.com', 'Engineering', 89000, '2019-01-10'),
    ('Sam', 'Whitfield', 'sam.whitfield@example.com', 'Sales', 63000, '2023-02-14'),
    ('Devon', 'Ellery', 'devon.ellery@example.com', NULL, 58000, '2023-08-30'),
    ('Taylor', 'Nakamura', 'taylor.nakamura@example.com', 'Engineering', 95000, '2018-05-05'),
    ('Alexis', 'Moreno', 'alexis.moreno@example.com', 'Marketing', 71000, '2021-09-12');

WHERE and =

sql
SELECT first_name, department FROM employees WHERE department = 'Engineering';
text
 first_name | department
------------+-------------
 Erin       | Engineering
 Priya      | Engineering
 Taylor     | Engineering

WHERE is evaluated once per row, only rows where the condition is true make it into the result. Every other lesson in this module is a variation on this same idea, different ways to write the condition.

Comparison Operators

text
=    equal to
!=  or  <>    not equal to
<    less than
>    greater than
<=   less than or equal to
>=   greater than or equal to
sql
SELECT first_name, salary FROM employees WHERE salary > 70000;
text
 first_name | salary
------------+---------
 Erin       | 74000.00
 Priya      | 89000.00
 Taylor     | 95000.00
 Alexis     | 71000.00
sql
SELECT first_name, department FROM employees WHERE department != 'Engineering';
text
 first_name | department
------------+-------------
 Jordan     | Sales
 Maya       | Marketing
 Sam        | Sales
 Alexis     | Marketing

Notice Devon, whose department is NULL, doesn’t appear in either the = 'Engineering' result or the != 'Engineering' result. NULL never satisfies an ordinary comparison, not even !=, that’s covered fully in this module’s IS NULL lesson.

Try It

  1. Write a query that returns every employee earning less than 65000.
  2. Write a query that returns every employee hired on or after '2021-01-01' (hire_date >= '2021-01-01').
  3. Write a query that returns every employee whose department is not 'Sales'.
  4. Run SELECT * FROM employees WHERE department != 'Engineering'; and confirm Devon (whose department is NULL) is missing from the result, then explain why in your own words.

Recap

  • WHERE condition filters which rows a query returns, evaluated once per row.
  • =, !=/<>, <, >, <=, >= are the standard comparison operators.
  • A NULL value never satisfies an ordinary comparison, including !=, it needs its own check, covered later in this module.

Next lesson: combining several conditions together with AND, OR, and NOT.