...

/

The WHERE Clause

The WHERE Clause

Learn to use WHERE clause and logical operators in queries.

Imagine our online store is offering a 70% discount on some shoes during the mega annual sale. A customer, who has been eagerly waiting for this sale, now wants to buy white shoes for a themed party. Instead of searching through all the shoes in the database, it would be great if they could quickly find those white shoes that are 70% off and still in stock. This is where SQL's WHERE clause comes in—it helps retrieve only the data that meets specific criteria.

Let's explore the WHERE clause in SQL to confidently produce queries that return only the rows we need, making our data analysis more efficient and targeted. We'll go step by step to:

  • Understand the purpose and importance of the WHERE clause.

  • Learn how to use the WHERE clause to filter records.

  • Explore logical operators such as AND, OR, and NOT to create complex conditions.

  • Understand how to combine multiple conditions.

  • Learn the order of operations when multiple logical operators are involved.

The WHERE clause

Analyzing the entire dataset provides a bigger picture of our online store. However, at times, it's crucial to focus on specific data to identify trends or answer particular business questions. The WHERE clause in SQL is used to filter rows in a table based on specific conditions such as matching specific values, ranges, or patterns. It is used along with the SELECT query. Without it, a SELECT query retrieves all rows from the table.

The basic syntax for the WHERE clause is shown below:

Press + to interact
SELECT Column1, Column2, ...
FROM TableName
WHERE condition;

In the code above, the condition specifies the criteria rows must meet to be included in the query results. There are various ways to use conditions with the WHERE clause. Let's explore them ...