Let’s see SQL Where Clause. In SQL, WHERE clause is used to filter records. WHERE clause Restrict the rows that are returned by the SQL query.
We use the WHERE with SELECT, INSERT, UPDATE, or DELETE statement to apply the conditions.
SQL Syntax :
WHERE Conditions ;
The conditions that must be met for records to be selected.
Single Condition in SQL Where Clause
We have seen the Syntax of the SQL WHERE clause. Now we will see the example. Suppose we want to filter the number or employees from Employees table who have the salary greater than 2000 $.
SQL> select count(*) from employees where salary > 2000;
You can see, We are using count keyword to get the Number of records. And We mentioned Salary>2000 in the condition after WHERE clause. So in Employees table, there are 107 records whose salary is greater than 2000 $.
Multiple Condition in SQL Where Clause
We have seen the use of WHERE clause with the single condition but conditions may be multiple. Now we will see how can we handle the Multiple conditions.
AND Keyword – We use AND keyword with conditions after WHERE clause if all conditions must be required to satisfied.
OR Keyword – We use OR keyword with conditions after WHERE clause if only any condition is satisfied among all.
Use of AND with WHERE – We discussed, we use AND if all conditions need to be satisfied.
Query – We need to filter those employees only who does belong from the department 110 and have the salary greater than 2000 $;
SQL> Select count(*) from Employees where salary>2000 AND Department_ID = 110;
Use of OR with SQL WHERE Clause
We discussed, we use OR if any condition needs to be satisfied.
Query – We need to filter those employees only who does belong from the department 110 or have the salary greater than 2000 $;
SQL> Select count(*) from Employees where salary>2000 OR Department_ID = 110;
Both of the above queries produce the different result as in the first query both conditions must be fulfilled and in Second query any condition needs to be fulfilled to produce the result.
COMBINING AND & OR CONDITIONS in SQL Where Clause
We can also use the both of the conditions together. Let’s see how can we use these.
Query – We need to filter the details of employees whose job id is ST_Clerk and salary greater than 3000 $ or the employees whose department id is 50.
SQL> select JOB_ID, DEPARTMENT_ID , SALARY from Employees
WHERE (Job_id = ‘ST_clerk’ AND Salary> 3000)
OR Department_id = 50;
So we have seen the uses of WHERE clause in SQL, We have seen the application of WHERE clause as well. Try some other scenarios on your local machine, WHERE is an important clause in SQL.