A WHERE clause is used in a Select statement to filter the results based on conditions specified in the ‘WHERE’ clause.
Consider an employee table as shown below:
id | first_name | last_name | department | salary |
101 | bryan | walker | sales | 50,000 |
102 | alex | carpenter | accounts | 55,000 |
103 | sam | white | sales | 60,000 |
104 | john | barry | sales | 54,000 |
105 | bill | wood | accounts | 65,000 |
106 | robert | hopkins | support | 70,000 |
107 | david | lee | support | 72,000 |
108 | jack | sears | support | 65,000 |
109 | tom | young | sales | 55,000 |
Below is the SQL query to return all rows from the employee table.
SELECT * FROM employee; id first_name last_name department salary --- --------- --------- ---------- ------- 101 bryan walker sales 50000 102 alex carpenter accounts 55000 . . .
Let’s assume you need to view the details of only the employees who work in the ‘sales’ department. To filter the rows, you would use the ‘WHERE’ clause as shown below:
SELECT * FROM employee WHERE department='sales'; id first_name last_name department salary --- --------- --------- ---------- ------- 101 bryan walker sales 50000 103 sam white sales 60000 104 john barry sales 54000