Understanding the SQL WHERE Clause: Basics and Examples
Introduction
In this article we break down the fundamentals of the SQL WHERE clause, a powerful tool for filtering query results. By the end you’ll be able to write simple and complex conditions without needing to watch the original video.
Simple Equality and Inequality
- Equality:
WHERE first_name = 'Jim'returns rows where the first name is exactly Jim. - Inequality:
WHERE first_name <> 'Jim'(or!=) returns every row except those where the first name is Jim.
Comparison Operators (Numeric Data)
- Greater than:
WHERE age > 30selects people older than 30. - Greater than or equal:
WHERE age >= 30includes 30‑year‑olds. - Less than:
WHERE age < 32selects people younger than 32. - Less than or equal:
WHERE age <= 32includes 32‑year‑olds.
Combining Conditions
- AND: All conditions must be true.
sql WHERE age <= 32 AND gender = 'Male'Returns only male rows whose age is 32 or less. - OR: At least one condition must be true.
sql WHERE age <= 32 OR gender = 'Male'Includes anyone under 32 or anyone male, so a 35‑year‑old male appears in the result.
Pattern Matching with LIKE
%is a wildcard representing any sequence of characters.- Starts with:
WHERE last_name LIKE 'S%'finds last names beginning with S (e.g., Schrute, Scott). - Contains:
WHERE last_name LIKE '%S%'finds any last name that contains S anywhere. - Multiple patterns: Combine conditions for more specific searches.
sql WHERE last_name LIKE 'S%' AND last_name LIKE '%O%'Returns names that start with S and contain an O (e.g., Scott). - Patterns can be chained to mimic complex string checks.
Checking for NULL Values
WHERE first_name IS NULLreturns rows where the column has no value.WHERE first_name IS NOT NULLreturns all rows where the column contains a value.- In the demo dataset no
first_namevalues were NULL, soIS NOT NULLreturned the whole table.
Using IN for Multiple Equality Checks
INprovides a concise way to test a column against several values.sql WHERE first_name IN ('Jim', 'Michael')Equivalent tofirst_name = 'Jim' OR first_name = 'Michael'but much shorter and easier to read.
Summary of Key Operators
=,<>(or!=) – equality / inequality>,>=,<,<=– numeric comparisonsAND,OR– logical combinationLIKEwith%– pattern matching for textIS NULL,IS NOT NULL– null checksIN– multiple value matching
Next Steps
After mastering these basics you can move on to more advanced clauses such as GROUP BY and ORDER BY, which will be covered in the next tutorial.
Mastering the WHERE clause lets you precisely filter data using equality, inequality, range checks, logical operators, pattern matching, null handling, and the IN list—forming the foundation for all advanced SQL queries.
Frequently Asked Questions
Who is Alex The Analyst on YouTube?
Alex The Analyst is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.