×

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement.

WHERE clause condition is an optional part of the SQL Data Manipulation Language query

WHERE clause condition is used to filter the records in the SQL query, the WHERE clause condition return only those records, which fulfill the specific Condition in the SQL query.

We can use WHERE Condition in the SELECT statement, UPDATE statement, and DELETE statement. We can even use Logical operators and comparison operators with the SQL queries WHERE conditions.

Let’s take deep dive and understand WHERE conditions in SQL concept with the help of examples.

The syntax of WHERE Condition is as follows:

SELECT * FROM Table_Name [WHERE conditions];

WHERE clause is optional in the SQL query, you want selected rows to be fetched using WHERE Condition in the SQL query.

The syntax of WHERE Condition of selected columns from the table is as follows:

SELECT Column_Name1, Column_Name2 FROM Table_Name [WHERE conditions]; 

Consider the already existing table:

Table 1: Emp

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE1F
1002VAIBHAVSHARMA60000NOIDAORACLE1M
1003NIKHILVANI50000JAIPURFMW2M
2001PRACHISHARMA55500CHANDIGARHORACLE1F
2002BHAVESHJAIN65500PUNEFMW2M
2003RUCHIKAJAIN50000MUMBAITESTING4F
3001PRANOTISHENDE55500PUNEJAVA3F
3002ANUJAWANRE50500JAIPURFMW2F
3003DEEPAMJAUHARI58500MUMBAIJAVA3M
4001RAJESHGOUD60500MUMBAITESTING4M

Table 2: Manager.

ManageridManager_NameManager_department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING

1. WHERE Condition with SELECT statement

The SELECT statement is used to fetch the records from the SQL table. Assume you want to fetch those records from the Order table whose order is placed between 20 September to 25 September. In this situation, we have to use the WHERE clause in the SELECT statement and also BETWEEN operator with the WHERE clause. The WHERE clause is used when we want to retrieve records of specific conditions from the table.

Syntax of WHERE Condition with the SELECT statement:

SELECT * FROM Table_Name [WHERE conditions];

Example 1: Execute a query to display employee information from the Emp Table, only that Employee whose City is 'Pune’.

SELECT Employeeid, First_Name, Last_Name, City, Salary, Gender from Emp WHERE City = 'Pune';

In the above query, we display employee information from the Emp Table of employees in Pune city. In the WHERE condition, we used the Equal comparison operator, which will compare Pune city in the Condition.

The Following output for the above query:

Where Condition in SQL

Example 2:  Execute a query to display employee information from the Emp Table, only that Employee whose Salary is greater than 55000.

SELECT Employeeid, First_Name, Last_Name, Department, Salary, Gender from Emp WHERE Salary > 55000;

In the above query, we display the employee's information from the Emp Table of that Employee whose Salary is greater than 55000. In the WHERE condition, we used the GREATER THAN comparison operator, which will use to compare SalarySalary in the Condition.

The Following output for the above query:

Where Condition in SQL

Example 3: Execute a query to display employee information from the Emp Table, only those employees whose Salary is greater than 55000 or whose City is 'Pune’.

SELECT Employeeid, First_Name, Last_Name, Department, Salary, City, Gender from Emp WHERE Salary > 55000 OR City = ’Pune’;

In the above query, we display employee information from the Emp Table of those employees whose Salary is greater than 55000 OR City is 'Pune'. If an employee's salary is greater than 55000, it will return that Employee in the result set, OR if the employee's city is Pune, it will return that Employee in the result set. In the WHERE condition, we used OR operator, which will return only the true value of the Condition.

The Following output for the above query:

Where Condition in SQL

Example 4: Execute a query to display employee information from the Emp Table only for that Employee whose Salary is between 50000 and 60000.

SELECT Employeeid, First_Name, Last_Name, Department, Salary, City, Gender from Emp WHERE Salary BETWEEN 50000 AND 60000;

In the above query, we display employee information from the Emp Table of those employees whose Salary is between 55000 and 60000. In the WHERE condition, we used BETWEEN operators to return those employees whose Salary is between 50000 and 60000 in the result set.

The Following output for the above query:

Where Condition in SQL

Consider the following tables along with the given records.

Table 1: Emp

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE1F
1002VAIBHAVSHARMA60000NOIDAORACLE1M
1003NIKHILVANI50000JAIPURFMW2M
2001PRACHISHARMA55500CHANDIGARHORACLE1F
2002BHAVESHJAIN65500PUNEFMW2M
2003RUCHIKAJAIN50000MUMBAITESTING4F
3001PRANOTISHENDE55500PUNEJAVA3F
3002ANUJAWANRE50500JAIPURFMW2F
3003DEEPAMJAUHARI58500MUMBAIJAVA3M
4001RAJESHGOUD60500MUMBAITESTING4M

Table 2: Manager.

ManageridManager_NameManager_Department
1Snehdeep KaurORACLE
2Kirti KirtaneFMW
3Abhishek ManishJAVA
4Anupam MishraTESTING

2. WHERE Condition with an UPDATE statement

The UPDATE statement in SQL is used to modify the SQL Table records. We used the WHERE clause with the UPDATE statement to modify the specific records of the SQL Table.

The syntax of WHERE Condition with the UPDATE statement is as follows:

UPDATE Table_Name SET Column_Name = Value WHERE conditions;

Example 1: Execute a query to modify the Employee's first name as Harsha, whose employee id is 3002.

UPDATE Emp SET First_Name = 'HARSHA' WHERE Employeeid = 3002;

In the above query, we modify the Employee First_ Name as HARSHA from the Emp Table, but whose employee id is 3002.

We will cross-check the query using the SELECT statement to check whether the employee name is modified or not.

SELECT * FROM Emp WHERE EmployeeId = 3002; 
Where Condition in SQL

Example 2:  Execute a query to modify the Employee City as Hyderabad and Salary increased by 1.2% whose employee Department is Testing and Employee is male.

UPDATE Emp SET City = 'HYDERABAD', Salary = Salary * 1.2 WHERE Department = 'TESTING' AND Gender = 'M';

In the above query, we modify the Employee City as Hyderabad and increment Salary by 1.2%, but whose employee Department is testing and Employee must be male.

We will cross-check the query using the SELECT statement to check whether the employee name is modified or not.

SELECT * FROM Emp WHERE Department = 'TESTING' AND Gender = 'M';
Where Condition in SQL

3. WHERE Condition with DELETE statement

The DELETE statement in SQL is used to delete the records from the SQL Table. We used the WHERE clause with the DELETE statement to delete the specific records from the SQL Table.

The syntax of WHERE Condition with the DELETE statement is as follows:

DELETE FROM Table_Name WHERE conditions;

Example 1: Execute a query to delete the Employee from the Emp table whose employee city is Pune and Employee must be male.

DELETE FROM Emp WHERE City = 'Pune' AND Gender =' M';

In the above query, we removed the employee details from Emp Table, whose City is Pune, and Employee must be male.

We will cross-check the query using the SELECT statement to check whether the employee name is deleted or not.

SELECT * FROM Emp;
Where Condition in SQL

Example 2: Execute a query to delete the Employee from the Emp table whose employee city is Mumbai or whose Salary is greater than 50000.

DELETE FROM Emp WHERE City = 'Pune' AND Gender =' M';

In the above query, we deleted the employee details from Emp Table, whose City is Mumbai, and the employee Salary is greater than 50000.

We will cross-check the query using the SELECT statement to check whether the employee name is deleted or not.

SELECT * FROM Emp;
Where Condition in SQL

Related Topics

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.