beginner

Filtering with WHERE clause

8 min readLast updated: 2026-07-05

Overview

Learn how to narrow query results by applying condition filters using comparative operators.

Learning Objectives

  • Apply filters to numerical and string fields.
  • Use comparison operators (=, <>, >, <, >=, <=).
  • Understand how database engines evaluate filters.

Detailed Concept Explanation

The WHERE clause filters rows before they are grouped or returned. The database engine scans the table (or uses index structures) and keeps only the rows where the condition evaluates to TRUE.


Code Examples

SQL

sql
-- Filter active users in engineering department
SELECT username, email 
FROM users 
WHERE department = 'Engineering' 
  AND status = 'Active';
Execution Plan Diagram
Scan users table
Evaluate WHERE conditions
Filter non-engineering rows
SELECT fields

Best Practices

  • Index Scanned Columns: Ensure columns frequently queried in WHERE filters are indexed (e.g. using B-Trees).
  • Match Types: Keep datatypes matching in comparison expressions (e.g. do not compare integer columns with string literals).

Interview Perspective

Note

Interview Question: Does the WHERE clause execute before or after the SELECT clause?

The WHERE clause executes before the SELECT clause in the query execution order. This means you cannot use aliases defined in the SELECT list inside the WHERE clause condition.


Interactive Challenges

Challenge 1: Filter salaries (Beginner)

Write an SQL query to retrieve all columns from 'employees' where the 'salary' is greater than or equal to 75000.


Summary

The WHERE clause filters rows using comparison conditions and runs before the SELECT clause in the database execution path.