Menu

SQL WHERE Clause – A Comprehensive Guide

Let’s dive into the heart of SQL (Structured Query Language) with a detailed look at the SQL WHERE clause.

You will explore its syntax, usage, and some real-life examples to give you a robust understanding of this essential command.

What is the SQL WHERE Clause?

Let’s start at the very beginning. SQL is a standard language used for managing data held in a relational database management system (RDBMS) or a relational data stream management system (RDSMS).

The SQL WHERE clause is the filtering mechanism in SQL. It specifies a condition while fetching data from a single table or by joining with multiple tables. If the given condition is satisfied, only then it returns a specific value from the table. You could say the WHERE clause is the gatekeeper of data – only letting through the data you specifically request.

Syntax of SQL WHERE Clause

The syntax for the WHERE clause in SQL is

SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • SELECT column1, column2, … : This is the portion of the statement where you specify the columns you want to see in the result.

  • FROM table_name : Here, you replace ‘table_name’ with the name of the table from which you want to fetch the data.

  • WHERE condition : This is where the magic happens! Replace ‘condition’ with the condition you’re trying to meet.

Using the SQL WHERE Clause

The WHERE clause is versatile, supporting several operators to filter queries:

1) Comparison operators : such as =, >, <, >=, <=, <> or !=.

2) Logical operators : such as AND, OR, NOT.

3) IN operator : to specify multiple values in a WHERE clause.

4) BETWEEN operator : to filter values within a certain range.

5) LIKE operator: for pattern matching.

Practical Examples

Let’s use the ‘Customers’ table to illustrate

-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 2  | Sarah | LA   | 80      |
| 3  | Bob   | SF   | 120     |
| 4  | Alice | NYC  | 75      |
-------------------------------

Example 1: Using Comparison Operator

Suppose we want to fetch all the customers from NYC, we can use the WHERE clause as follows:

SELECT * 
FROM Customers 
WHERE City = 'NYC';

This query will return

-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 4  | Alice | NYC  | 75      |

Example 2: Using Logical Operators

Now, let’s fetch the customers who are from either NYC or LA

SELECT * 
FROM Customers 
WHERE City = 'NYC' OR City = 'LA';

The output will be

-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 2  | Sarah | LA   | 80      |
| 4  | Alice | NYC  | 75      |

Example 3: Using IN Operator

The IN operator allows you to specify multiple values. If you want to fetch data for John and Alice, we can do this

SELECT * 
FROM Customers 
WHERE Name IN ('John', 'Alice');

This will return

-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 1  | John  | NYC  | 45      |
| 4  | Alice | NYC  | 75      |

Example 4: Using BETWEEN operator

SELECT * 
FROM Orders
WHERE Amount BETWEEN 50 AND 100;

This query will output

-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 2  | Sarah | LA   | 80      |
| 4  | Alice | NYC  | 75      |

Example 5: Using LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specific pattern in a column.

Let’s consider our ‘Customers’ table again, and we want to find all customers whose names start with ‘A’:

SELECT * 
FROM Customers 
WHERE Name LIKE 'A%';

Here, % is a wildcard character that matches any sequence of characters.

This query will output

-------------------------------
| ID | Name  | City | Amount  |
-------------------------------
| 4  | Alice | NYC  | 75      |

Conclusion

The SQL WHERE clause is an essential part of SQL. Its versatility enables us to filter data to meet our exact needs, offering an efficient and streamlined way to interact with our databases. .

Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science