Filtering Records with WHERE

Your dataset contains millions of rows, but you only need a handful of specific records. Which SQL clause precisely narrows down this vast amount of data to just the relevant few?
The WHERE clause is your answer, allowing you to specify conditions that filter rows from your result set.

Why SELECT Isn't Always Enough

When querying a database, you often don't need every single row. Retrieving an entire table, especially a large one, can be inefficient and slow. It also clutters your results with irrelevant information.

For instance, you might only be interested in sales data from a specific region, customer orders above a certain value, or records created within a particular date range. The WHERE clause provides the mechanism to precisely define these subsets, ensuring you only retrieve the data that truly matters for your analysis.

WHERE Clause
A SQL clause used to filter records based on specified conditions, returning only the rows that satisfy those conditions.
Example: To select all customers from 'New York', you would write: SELECT FROM Customers WHERE city = 'New York';

Filtering with Simple Conditions

sqlFiltering Orders by Country

The WHERE clause works by evaluating a condition for each row in the table. A condition is a logical expression that returns either TRUE, FALSE, or UNKNOWN.

In the example, country = 'USA' is the condition. For every row, the database checks if the value in the country column matches 'USA'. If it does, the row is included in the result set; otherwise, it is excluded. This precise evaluation ensures only relevant data is returned.

Using Comparison Operators

Conditions in a WHERE clause rely on comparison operators to define how values are evaluated. These operators allow you to compare a column's value against another value or expression.

Here are the most common comparison operators:
- = (Equals): Checks if two values are identical.
- != or <> (Not Equals): Checks if two values are different.
- > (Greater Than): Checks if the left value is larger than the right.
- < (Less Than): Checks if the left value is smaller than the right.
- >= (Greater Than or Equals): Checks if the left value is larger than or equal to the right.
- <= (Less Than or Equals): Checks if the left value is smaller than or equal to the right.

sqlFiltering with Various Comparison Operators
Check Your Understanding
To find all products with a price of $50 or more, which operator would you use?

Combining Multiple Conditions with Logical Operators

Often, your filtering needs are more complex than a single condition. You might need to find orders from a specific country and above a certain amount, or customers from one country or another. This is where logical operators become indispensable.

SQL provides AND, OR, and NOT to combine or negate conditions, allowing you to build sophisticated filtering logic. These operators enable you to express intricate data retrieval requirements precisely.

sqlUsing the `AND` Operator

The AND operator combines two or more conditions. For a row to be included in the result set, all conditions connected by AND must evaluate to TRUE.

If even one condition linked by AND is FALSE, the entire combined condition becomes FALSE for that row, and the row is excluded. This operator is useful for narrowing down results based on multiple strict criteria.

sqlUsing the `OR` Operator

The OR operator also combines two or more conditions. However, for a row to be included in the result set, at least one of the conditions connected by OR must evaluate to TRUE.

If all conditions linked by OR are FALSE, then the entire combined condition becomes FALSE. This operator is ideal for broadening your results to include data that meets any of several possible criteria.

sqlUsing the `NOT` Operator
Check Your Understanding
Which logical operator would you use to find customers who live in 'London' AND have placed more than 5 orders?
Try It Yourself
Write a query to find all orders placed in 2023 with a total amount greater than $100, OR orders placed in 2024 with a total amount less than $100.
sql
Key Takeaways
  • The WHERE clause is essential for filtering rows based on specific conditions, retrieving only relevant data.

  • Comparison operators (=, !=, >, <, >=, <=) define how values are evaluated against criteria.

  • Logical operators (AND, OR, NOT) combine multiple conditions for more complex filtering requirements.

  • AND requires all connected conditions to be true, OR requires at least one to be true, and NOT negates a condition.

  • Mastering the WHERE clause allows you to efficiently extract precisely the data you need, avoiding the overhead of processing irrelevant information.

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free