SQL Tutorial SQL Advanced SQL Database SQL References

SQL - WHERE Clause



The SQL WHERE Clause is used to specify condition(s) in SQL query. It can be used to specify condition(s) while fetching data from a table, joining two tables, updating records in a table, inserting records in a table or deleting records from a table.

Syntax

The syntax for using WHERE Clause is given below:

SELECT column1, column2, ...
FROM table_name
WHERE condition(s);

To specify condition in a query, SQL comparison or logical operators like <, >, =, LIKE, IN, NOT, NULL etc. are used.

Example:

Consider a database containing a table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • To fetch Name, Age and Salary data from Employee table where Salary is greater than 2800, the SQL query is:

    SELECT Name, Age, Salary 
    FROM Employee
    WHERE Salary > 2800;
    

    This will produce the result as shown below:

    NameAgeSalary
    John253000
    Kim303100
    Ramesh283000
  • To specify multiple conditions SQL logical operators are used, for example - To fetch data from the Employee table where Salary is greater than 2800 and Age is less than 30, the SQL AND operator is used and the SQL query will be:

    SELECT * FROM Employee
    WHERE Salary > 2800 AND Age < 30;
    

    This result of the following code will be:

    EmpIDNameCityAgeSalary
    1JohnLondon253000
    5RameshNew Delhi283000