HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SQL (SELECT)Tables (CREATE & INSERT)Filtering (WHERE)Sorting & LimitingAggregate FunctionsUpdate & DeleteJoins (JOIN)
REFERENCE
📖 Overview
🔎 Querying (SELECT)
🗃️ Creating a Table
✏️ Inserting & Modifying Data
🧲 Filtering (WHERE)
🔢 Sorting & Limiting
📊 Aggregate Functions
🔗 Joins (JOIN)
SQL›Filtering (WHERE)

Filtering (WHERE)

Selecting rows based on conditions with WHERE, comparisons, AND/OR, and LIKE.

Simple enough for a 5-year-old to understand

Filter with WHERE

Example code
1
CREATE TABLE k(name TEXT, age INTEGER);
2
INSERT INTO k VALUES ('Alex', 36), ('Sam', 20);
3
SELECT name FROM k WHERE age >= 30;
Matching condition: Alex
Output / InputMatching condition: Alex
Output / Input
💡 Hint: Write conditions using = != < > <= >= operators and AND/OR.

About this topic

WHERE is the most used part of SQL: it selects only the ones you need from millions of rows in a table. The speed of a query is also largely determined here — a well-written WHERE condition allows the database to use an index and reduces the query from seconds to milliseconds.

Inside WHERE, alongside comparison operators, there are SQL-specific tools: ranges with BETWEEN, lists with IN, pattern matching with LIKE, and null value checks with IS NULL. Knowing these saves you from writing long and slow conditions that do the same thing.

What you will learn

  • filtering rows with WHERE
  • combining conditions with AND, OR, and NOT
  • using BETWEEN and IN
  • pattern matching with LIKE
  • null value checking with IS NULL
  • the importance of condition order and parentheses

When to use?

WHERE is in almost every query: finding a user's orders, getting records within a specific date range, listing incomplete tasks.

WHERE is vital in delete and update operations; a missing WHERE affects the ENTIRE table. Writing such queries as SELECT first and seeing how many rows are returned is a professional habit.

Common mistakes

Wrong

SELECT * FROM users WHERE city = NULL;

NULL is not equal to anything, not even itself; this query won't throw an error but will return ZERO rows. Empty value checking must be done with IS NULL.

Correct

SELECT * FROM users WHERE city IS NULL;

Wrong

SELECT * FROM orders
WHERE status = 'new' OR status = 'paid' AND total > 100;

AND is evaluated before OR; this query means "all the new ones, plus paid AND over 100" — likely not what you meant. Using parentheses makes the intention clear.

Correct

SELECT * FROM orders
WHERE (status = 'new' OR status = 'paid')
  AND total > 100;

Wrong

SELECT * FROM users WHERE LOWER(email) = '[email protected]';

Wrapping a column inside a function prevents the database from using the index on that column; the query has to scan the entire table. In large tables, this is the difference between seconds and milliseconds.

Correct

SELECT * FROM users WHERE email = '[email protected]';

Frequently asked questions

How does LIKE work?

The percent sign represents any number of characters, and the underscore represents a single character. Starting the pattern with a percent sign — meaning searching for "contains" — prevents the use of indices and is slow. If full-text search is needed, the database's search features should be used.

Is there a difference between IN and an OR chain?

The result is the same, but IN is much more readable and the database optimizes it better for long lists. Use IN if you are comparing more than five values.

Why is my query running slowly?

The three most common reasons: wrapping the column in a function inside WHERE, putting a percent sign at the start of the pattern, and lacking an index on the filtered column. The EXPLAIN command, which shows the database's query plan, tells you exactly which step is expensive.

Summary

WHERE filters rows and determines a large part of query speed. Three critical rules: NULL check is done with IS NULL, parentheses are used when mixing AND and OR, and the filtered column is not wrapped in a function.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code