WHERE Clause
WHERE clause is used to filter the records based on certain conditions. In short, WHERE clause filters the number of rows returned by the Query
Suppose, I want to get the BusinessEntityId, NationalIdNumber, BirthDate and MaritalStatus where Employees are Single, then you can code –
SELECT BusinessEntityID, NationalIDNumber, BirthDate, MaritalStatus FROM HumanResources.Employee WHERE MaritalStatus = 'S';
Order Of execution in SQL
Order in which SQL server will execute –
FROM -> WHERE -> SELECT
You can also have something like this –
SELECT BusinessEntityID, NationalIDNumber, BirthDate FROM HumanResources.Employee WHERE MaritalStatus = 'S';
This time, I want to know all the Employees whose BirthDate > ‘1985-01-20’, then you can code –
SELECT * FROM HumanResources.Employee WHERE BirthDate > '1985-01-20';
We will master the UPDATE, INSERT, UPDATE and a lot of other SQL statements as we move further in this tutorial.
Similarly, if I ask you to get the BusinessEntityId, NationalIdNumber and Birth Date where Birth year is greater than 1985, then you can code like –
SELECT BusinessEntityID, NationalIDNumber, BirthDate FROM HumanResources.Employee WHERE year(BirthDate) > '1985';
Year() function is used to extract the year from a date. Like this, there are so many functions which we are going to master in this tutorial.
Till now, we have used = and > operators and in the same way, there are so many operators which can be used in the SELECT clause. You are going to master all those from the next article.