For working professionals
For fresh graduates
More
A relational database might comprise billions of data rows. In a practical scenario, you might want to work with only some of the set of records in a single go. For instance, if we have a table that records all of the customer's orders, we might want to display only some of the data available in an application since we created the table. The structured query language, i.e., SQL uses WHERE clause to filter data based on the various applied conditions.
In this blog, we will learn everything about the SQL WHERE clause, its syntax, usage for a single condition or SQL WHERE multiple conditions, including and excluding data based on various query expressions, and so on.
The SQL WHERE clause is an essential component of SQL that is used for database management. It is employed within a SQL statement, specifically to SELECT, UPDATE, and DELETE or filter rows based on a specific condition. Now, let's understand the syntax of the SQL WHERE clause.
The basic syntax of the WHERE clause is as follows:
SELECT column names
FROM table name
WHERE conditions;
Now, let's look at a few examples to understand the WHERE clause more easily.
Imagine you have a list of employees with details like their ID, name, gender, department, education, when they joined, and how much they earned. The SQL WHERE clause is like a filter that helps you pick out specific employees based on certain conditions.
For instance, if you only want to see employees who work in the marketing department, you would use the WHERE clause to tell the database to show you just those employees. It is like wearing a pair of glasses that only enable one to see what one is looking for in the data.
Let's understand this in tabular form:
Employee ID | Name | Gender | Department | Education | Month of Joining | CTC |
---|---|---|---|---|---|---|
1001 | Ajay | M | HR | UG | January | 25 |
1002 | Babloo | M | Marketing | PG | February | 23 |
1003 | Chhavi | F | Marketing | UG | March | 21 |
1004 | Dheeraj | M | HR | PG | April | 17 |
1005 | Evina | F | Sales | PG | March | 14 |
1006 | Fred | M | Sales | PG | January | 13 |
1007 | Gautam | M | Accounts | UG | August | 12 |
1008 | Hari | M | Admin | UG | March | 9 |
1009 | Isha | F | Admin | Intermediate | February | 8 |
1010 | Jay | M | Peon | December | 6 |
Example 1: Find complete details of the Employee Named “Chhavi”.
Query:
SELECT *
FROM Employee
WHERE Name = ‘Chhavi’;
Output:
1003 | Chhavi | F | Marketing | UG | March | 21 |
---|
Example 2: Select all employee IDs, Names, and Departments that are ‘Male’ using the employee table.
Query:
SELECT Employee ID, Name, Department
FROM Employee
WHERE Gender = ‘M’;
Output:
Employee ID | Name | Gender | Department | Education | Month of Joining | CTC |
---|---|---|---|---|---|---|
1001 | Ajay | M | HR | UG | January | 25 |
1002 | Babloo | M | Marketing | PG | February | 23 |
1004 | Dheeraj | M | HR | PG | April | 17 |
1006 | Fred | M | Sales | PG | January | 13 |
1007 | Gautam | M | Accounts | UG | August | 12 |
1008 | Hari | M | Admin | UG | March | 9 |
1010 | Jay | M | Peon | December | 6 |
Example 3: Find the employees who are working in marketing and undergraduates.
Query:
SELECT *
FROM Employee
WHERE department = ‘Marketing’ AND Education = ‘UG’;
Output:
1003 | Chhavi | F | Marketing | UG | March | 21 |
---|
There are six types of operators in the SQL WHERE clause. These operators are explained with examples.
For example, ‘salary > 50000’ selects rows where the salary is greater than 50000.
For example, ‘department = 'Marketing' AND gender = 'Female'’ selects rows where the department is Marketing and the gender is Female.
For example, ‘department IN ('Marketing', 'Sales')’ selects rows where the department is either Marketing or Sales.
For example, ‘age BETWEEN 25 AND 35’ selects rows where the age is between 25 and 35.
For example, ‘name LIKE 'John%'’ selects rows where the name starts with "John".
For example, ‘education IS NULL’ selects rows where the education column contains NULL values.
There are various practices for efficient filtering, such as indexing, optimization, readability and maintainability, and testing and performance monitoring. These are explained below.
Imagine your database is like a library, and indexing is like having a well-organized catalog. When you index specific columns, it is like creating an easy-to-use index at the library that tells you exactly where to find each book.
This helps the database quickly locate the data you are looking for, making your searches much faster. There are different types of indexes, like the "super-organized" clustered index, which sorts the data physically on disk, and the "flexible" non-clustered index, which keeps a separate list of pointers to the data.
Think of optimization as making your queries smarter and more efficient. It is like fine-tuning a car to get the best performance. You can do things like rewrite your queries to be more straightforward and faster, optimize how different parts of your database connect (that is called join optimization), and make sure any more minor queries inside your main one are as efficient as possible (that is subquery optimization).
When you optimize queries, it does not involve only writing them in a way that the database can execute them faster but also ensuring that you can use indexes effectively. For example, you should use appropriate strategies, and avoiding unnecessary subqueries can significantly enhance query performance.
Moreover, optimizing the WHERE clause by referencing an indexed column and avoiding complex expressions can improve efficiency.
This is all about writing your queries in a way that's easy to understand and maintain, like keeping your room tidy so you can find things quickly. Use clear names for your tables and columns, and add comments to explain what your query is doing.
This helps not only you but also your teammates who might need to work with your code later on.
Imagine you are a coach preparing your team for a big game. You want to make sure they are in top shape and prepared to perform. Similarly, before putting your queries into action, you need to test them to make sure they work smoothly and quickly.
Tools like query execution plans and profiling help you see how your queries are performing and where you need to make improvements. It is like giving your queries a check-up to keep them running smoothly.
Properly indexing columns that are referenced in the WHERE condition is crucial to improving query execution speed and efficiency. Indexing allows the database to quickly locate the data you are searching for, making your queries run faster. Without proper indexing, the database may have to scan through the entire dataset, leading to slower performance. Avoiding complex expressions in WHERE conditions and optimizing queries to reference indexed columns can further enhance efficiency and speed up query execution.
Various real-world applications use the SQL WHERE clause. Here are some of them:
Below, we provide common mistakes and pitfalls in a SQL WHERE clause:
A SQL WHERE clause is a filter that allows you to pick out precisely what you need from a bunch of data. When you understand how to use it, you can find important information quickly and make smarter decisions. So, as you learn more about SQL, remember that the WHERE clause is your key to finding success with data.
1. What is the WHERE clause in SQL?
The SQL WHERE clause is used to filter records from a database table based on specified conditions. It allows you to retrieve only the rows that meet specific criteria.
2. How to use 2 conditions in a WHERE clause in SQL?
You can use multiple conditions in the WHERE clause by combining them with logical operators, such as AND, OR, and NOT. For example:
SQL:
SELECT * FROM table_name WHERE condition1 AND condition2;
3. What are the 4 clauses of a SQL expression?
The four clauses of a SQL expression are:
4. WHERE to use with clauses in SQL?
The SQL WHERE clause is typically used in SELECT, UPDATE, DELETE, and sometimes in INSERT statements to filter records from a table.
5. What is the syntax of the WHERE clause?
The basic syntax of the WHERE clause is:
SQL
SELECT column1, column2, …
FROM table_name
WHERE condition;
6. What is an example of a WHERE clause?
An example of a WHERE clause is:
SQLSELECT * FROM employees WHERE department = 'Sales';
7. WHERE contains SQL Server?
The WHERE clause is a fundamental part of SQL Server, as it is in all SQL-based database systems. It is used to filter records in SELECT, UPDATE, DELETE, and INSERT statements.
8. What are clauses in SQL?
Clauses in SQL are components of SQL statements that define various aspects of the query, such as filtering conditions, sorting order, and grouping criteria.
Kechit Goyal
Experienced Developer, Team Player and a Leader with a demonstrated history of working in startups. Strong engineering professional with a Bachel…Read More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.