🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Indexes and WHERE in SQL & Databases

Learn about Indexes and WHERE in this comprehensive SQL & Databases development tutorial. Performance secrets.

Total XP: 0|💻 sql XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

1The Full Table Scan

If you write WHERE last_name = 'Smith', the database has to read every single row on the hard drive to find the Smiths. This is a 'Full Table Scan' and it is very slow. If you create an 'Index' on the last_name column, the database creates a B-Tree, allowing it to instantly jump to the Smiths without scanning the whole table.

2Step-by-Step Breakdown

Filtering Data. If you have 10 million users, you never want to fetch all of them. The WHERE clause filters the rows returned by the FROM clause based on a specific condition.

Basic Equality. To find a specific user, use the equals sign. 'SELECT * FROM users WHERE id = 5;'. Note that SQL uses a single '=' for equality, not '==' like JavaScript.

String Matching. When filtering by text, you must use single quotes. 'WHERE country = 'Spain';'. Many databases are case-sensitive here, so 'spain' will not match 'Spain'.

Inequality. You can use standard math operators: > (greater than), < (less than), >=, <=. For 'not equal', SQL standard uses '<>'. (Though '!=' also works in most modern engines).

Knowledge Check. What is the standard SQL operator for 'Not Equal To'?

  • !==
  • <>

Working with NULL. NULL means 'missing or unknown data'. You CANNOT write 'WHERE phone = NULL'. It will fail. You must use the special syntax 'WHERE phone IS NULL' or 'IS NOT NULL'.

The IN Operator. If you want users from 3 specific countries, don't write 3 conditions. Use IN. 'WHERE country IN ('Spain', 'France', 'Italy');'.

The BETWEEN Operator. Perfect for dates and numbers. 'WHERE age BETWEEN 20 AND 30;'. This is inclusive (includes 20 and 30).

Pattern Matching (LIKE). To search for partial text, use LIKE with the '%' wildcard. 'WHERE name LIKE 'A%';' finds all names starting with A. '%son' finds names ending in son.

Summary. The WHERE clause is your primary tool for reducing data payloads.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Expose Active Filter Conditions as Visible, Screen-Reader-Announced Text

When a search UI applies a WHERE-clause-backed filter (e.g. price BETWEEN 10 AND 50), display the active filter as removable, readable text like 'Price: $10 to $50' with an accessible remove button, rather than only reflecting it via a visual checkbox state that assistive tech users can't easily audit.

SEO Implications

  • 1

    Slow, Unindexed WHERE Clauses Directly Hurt Time to First Byte

    A WHERE clause filtering on a column without an index forces a full table scan, which can add hundreds of milliseconds or more to a page's server response time on large tables. Since Time to First Byte is a foundational input to Core Web Vitals, unindexed filters on database-backed pages can measurably hurt SEO.

Best Practices

Always Use IS NULL / IS NOT NULL, Never = NULL or != NULL

NULL represents 'unknown', so comparing it with = or != always evaluates to unknown, meaning rows are silently excluded rather than raising an error. Use the special IS NULL and IS NOT NULL predicates whenever checking for missing data.

Index Columns That Are Frequently Filtered With WHERE

A WHERE clause on an unindexed column forces a full table scan, reading every row to check the condition. Adding a B-Tree index on frequently filtered columns lets the engine jump directly to matching rows instead.

Frequent Bugs

THE BUG

A query like WHERE phone = NULL always returns zero rows, even for records that clearly have a missing phone number.

THE FIX

NULL represents unknown data, and no value, including NULL itself, is considered equal to NULL using the = operator; the comparison evaluates to unknown, not true. Use WHERE phone IS NULL instead, which is SQL's dedicated syntax for checking for missing values.

THE BUG

A LIKE pattern search (e.g. WHERE name LIKE '%son') runs extremely slowly on a large table even though the name column is indexed.

THE FIX

A leading wildcard (%son) prevents a standard B-Tree index from being used efficiently, because the index can't narrow down rows by a prefix it doesn't know. If suffix or substring search is a core requirement, consider a specialized index type like a trigram (pg_trgm) or full-text search index instead of relying on a plain B-Tree.

Real-World Examples

Finding Active Users in a Specific Age Range with an Indexed Filter

An analytics query needed to find all users between 20 and 30 years old with a verified email, filtering on columns that were indexed to keep the query fast even on a multi-million row table.

SELECT id, name, email
FROM users
WHERE age BETWEEN 20 AND 30
  AND email IS NOT NULL;

Interview Prep

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Using = NULL or != NULL instead of IS NULL / IS NOT NULL

-- Wrong: never matches any row SELECT * FROM users WHERE phone = NULL; -- Correct SELECT * FROM users WHERE phone IS NULL;

The Solution //

NULL represents unknown data, so comparing anything to NULL with = or != always evaluates to unknown, silently excluding rows rather than matching them. Use the dedicated IS NULL and IS NOT NULL predicates instead.

The Error //

A leading-wildcard LIKE pattern (e.g. '%son') failing to use a standard index, causing a full table scan

-- Slow: leading wildcard can't use a standard B-Tree index SELECT * FROM users WHERE name LIKE '%son'; -- Faster: prefix search can use the index SELECT * FROM users WHERE name LIKE 'John%';

The Solution //

A B-Tree index can only be used efficiently when the search pattern has a known prefix. A leading '%' means the engine can't narrow the search using the index and falls back to scanning every row. Use a trigram or full-text index if suffix/substring search is required.

Lesson Glossary

[01]NULL

Absence of data.

Code Preview
// NULL context

[02]Wildcard (%)

Matches any sequence of characters.

Code Preview
// Wildcard (%) context

Continue Learning