The SQL WHERE clause is used to filter records during selecting records from one or more tables in SQL. If the condition specified in the WHERE clause matches then only the SQL query returns the specific value. You can use WHERE clause only to filter the necessary records while rest of the unwanted records will be discarded.
The WHERE clause not only used in the SELECT statement but also used in the UPDATE and DELETE statement.
SQL WHERE Clause Syntax
The basic syntax of WHERE clause along with the SELECT statement is as below.
SELECT column1, column2, columnN FROM table_name WHERE [condition]
In this syntax,
- column1, column2, columnN – The names of the column from table.
- table_name – The name of the table from which data need to be fetched.
- WHERE [condition] – This is used to specify some conditions while selecting data. In case you are not using WHERE clause all the rows available will be selected.
You can specify a condition in the WHERE clause using the comparison or logical operators like >
, <
, =
, LIKE
, NOT
, etc.
SQL WHERE Clause Example
Consider a table of Employee
with the following records.
emp_id | emp_name | emp_gender | emp_salary | dept_code | emp_location |
1510 | Avinash | M | 33000 | 101 | Mumbai |
1511 | Ramjan | M | 56000 | 105 | Kolkata |
1512 | Piyanka | F | 35000 | 103 | Kolkata |
1513 | Piyam | M | 42000 | 101 | Kolkata |
1514 | Jitu | M | 43000 | 104 | Mumbai |
1515 | Raman | M | 49000 | 105 | Delhi |
1516 | Lata | F | 27000 | 104 | Mumbai |
1517 | Prakash | M | 45000 | 101 | Delhi |
1518 | Nitu | F | 45000 | 103 | Mumbai |
1519 | Pallab | M | 42000 | 103 | Kolkata |
1520 | Pankaj | M | 55000 | 101 | Bangalore |
The following example fetch the emp_id
, emp_name
, emp_salary
from the Employee
table whose salary is 45000 or more:
SELECT emp_id, emp_name, emp_salary FROM Employee WHERE emp_salary >= 45000 ORDER BY emp_id;
emp_id | emp_name | emp_salary |
1511 | Ramjan | 56000 |
1515 | Raman | 49000 |
1517 | Prakash | 45000 |
1518 | Nitu | 45000 |
1520 | Pankaj | 55000 |
In the following example, the SQL query will fetch emp_id
, emp_name
and emp_salary
from the Employee table where emp_name
is "Prakash"
.
One thing you have to remember that if you are providing a string value in the WHERE clause it should be enclosed with single quotes(”). Whereas for numeric should not be inside any quotes as seen the above example.
SELECT emp_id, emp_name, emp_salary FROM Employee WHERE emp_salary = 'Prakash';
emp_id | emp_name | emp_salary |
1517 | Prakash | 45000 |
Summary: In this tutorial, you have learned how to use SQL WHERE clause to filter out the records while select data from one more tables.