Lesson 2 of 5

Progress: 0/4 exercises solved (0%)

← Previous

Solve all exercises below to unlock the next lesson.

WHERE: filtering rows

Lesson Description

Keep only rows that match a condition.

Easy Project

Mini project: Create a hiring shortlist query that filters by role, salary range, and hire date with deterministic ordering.

WHERE — filter rows

WHERE keeps only the rows that satisfy a condition. Without WHERE, every row in the table is returned.

Basic syntax

SELECT columns
FROM table_name
WHERE condition;

Comparison operators

OperatorMeaning
=Equal
!= or <>Not equal
<, >Less / greater than
<=, >=Less / greater than or equal

Examples

-- Employees earning above 10 000
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 10000;

-- Only the IT department (id = 60)
SELECT first_name, last_name
FROM employees
WHERE department_id = 60;

-- A specific employee by name
SELECT *
FROM employees
WHERE last_name = 'King';

String comparisons

String comparisons are case-sensitive by default in PostgreSQL. Use ILIKE for case-insensitive matching (covered in the next course).

Date comparisons

Dates can be compared directly using ISO format:

SELECT first_name, hire_date
FROM employees
WHERE hire_date > '2005-01-01';

Tips

  • WHERE is evaluated before SELECT, so you can filter on columns you are not selecting.
  • One WHERE clause per query; combine multiple conditions with AND / OR.

Learning context

This lesson is part of SQL Fundamentals. Focus on the core idea in WHERE: filtering rows, then validate with deliberate practice.

What to master

  • Understand clause order and intent
  • Write deterministic result queries
  • Use clear aliases and readable structure

Common mistakes

  • Skipping ORDER BY in final outputs
  • Using joins before validating base rows
  • Not checking row counts after changes

High-level strategy

  • Start simple, then layer complexity
  • Run often and compare expected shape
  • Refactor with CTEs when logic grows

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

Logical reasoning for commands

WHERE

Why: Limits rows to only the business-relevant subset.

Memory cue: Filter early to reduce noise.

ORDER BY

Why: Makes output deterministic and reviewable.

Memory cue: No ORDER BY means no guaranteed row order.

AS alias

Why: Makes output columns readable for teams and reports.

Memory cue: If the name is clear, the query is easier to trust.

Concept check

Quick check: WHERE: filtering rows

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Employees earning more than $10,000

Enterprise Workforce Analyticseasy

List the first_name, last_name, and salary of every employee with a salary above 10000.

Run a query to see results here.
Exercise 2

Everyone in IT

Enterprise Workforce Analyticseasy

Return the first_name and last_name of employees in department 60 (IT).

Run a query to see results here.
Exercise 3

Employees hired after 2005

Enterprise Workforce Analyticseasy

Return first_name, last_name, and hire_date for employees hired after 2005-01-01, ordered by hire_date ascending.

Run a query to see results here.
Exercise 4

In-stock products under $50

Commerce Operations Intelligenceeasy

From the store sandbox, return name, price, and stock for products with price < 50 AND stock > 0, ordered by price ascending.

Run a query to see results here.

Lesson 2 of 5

0/4 solved (0%)

← Previous

Finish this lesson to unlock next.