21
Learn SQL: Microsoft SQL Server - Episode 3: Where Clause - Part 1
![](https://res.cloudinary.com/practicaldev/image/fetch/s--wWQSokE2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90bk8xas75umpumnzuvb.png)
Majority of the time when selecting data from the database, we want to narrow down the result set based on certain criteria. This is where a WHERE clause can help us.
We will be discussing the Where clause and the syntax to use it.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--2VWTVKVj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a78lf54g6wn5mk8x85o8.png)
Where clause specifies the criteria to narrow down.
We use the following syntax to use a Where clause.
Select [Column Names | *]
Where [Column Name] [operator] [criteria]
We narrow down our result set with the criteria.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--1gs93nUb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c0dsqw7oxs224eqzsxy.gif)
We can use any number of criteria to further narrow down our result set by using the AND or OR condition, with the following syntax.
Select [Column Names | *]
Where [Column Name] [operator] [criteria]
AND | OR [Column Name] [operator] [criteria]
This will all make more sense when we actually make use of these query statements in SSMS.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Rg3Q7jDJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/578jfjdw5crthvnpv3um.png)
The operators that we can use are:
=
<, >, >=, <=
Between (inclusive) - for dates and numbers.
An example of how we would construct a query statement using the Where clause is as follows:
Syntax:
Select firstName
From Person.Person
Where firstName = 'John'
When we have a criteria that is a string we need to enclose it in quotation marks. This can be either double quotes or single quotes, however remember you cannot mix the two.
'John" - This will be invalid.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--1gs93nUb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c0dsqw7oxs224eqzsxy.gif)
In SSMS lets start using the Where clause in our query statements.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Jres3SfR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/857z7i6qtj9s3ikk6dgq.gif)
As we can see we get a bunch of results back with people whose first name is John. But this of course is too much data if we were looking for a specific John. We can narrow down the query statement even further by using adding more criteria.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--7bUltP5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azl3r9dtgs4doi1bnv0h.gif)
This time we are looking for a person with the first name as John and the last name as Ford. We use the AND operator to help narrow down this distinction.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--fChhbO9n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hk3eykpb0bew4u4fko6o.gif)
We have narrowed down the query to find the person with the first name as John and the last name as Ford. But what if we wanted to find the person with the first name of John or the last name of Ford.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--7bUltP5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azl3r9dtgs4doi1bnv0h.gif)
In this case we use the OR operator:
![](https://res.cloudinary.com/practicaldev/image/fetch/s--N4_GOarf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f25h3j57pdzf69f8esyb.gif)
This time we get the results back of every person with the first name of John or with the last name of Ford.
We can use as many criteria as we want.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--OChfKxcM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfbmmcqy4aiodoweelab.gif)
We can now look at the other operators available to us. Let’s start with the More Than operator.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--1gs93nUb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c0dsqw7oxs224eqzsxy.gif)
Back in SSMS, let’s create a new query window and experiment with the More Than operator. These operators are usually used with numbers and dates, however they can also be used with sting values.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--o-FQdP5W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjzh2bbp9dv9kyr3ap9z.gif)
Note that we pull all the specified data, however 2000 was not included in the result set. This is because we queried for more than 2000. If we want to include 2000 in the result set, we specify More Than or Equal to:
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Qj3P_Czj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32x389o8u59ctyyt5kb4.gif)
We can also use this syntax to see the result set that has a criteria Less Than 2000:
![](https://res.cloudinary.com/practicaldev/image/fetch/s--XtuwRWke--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c411txqga1trshlyxsjz.gif)
As we expected to see, we get the results of every data point less than 2000. If we want to include 2000 in the result set then we use Less Than or Equal to:
![](https://res.cloudinary.com/practicaldev/image/fetch/s--_HcUXZ2q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arqubfzogsptegzdjswk.gif)
![](https://res.cloudinary.com/practicaldev/image/fetch/s--2VWTVKVj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a78lf54g6wn5mk8x85o8.png)
The between criteria uses the startValue and endValue and returns the result set that is between those two values, including those two values.
For instance we want to see the data of Person.Person table with the BusinessEntityID between 2000 and 2021, we use the following syntax:
Select *
From Person.Person
Where BusinessEntityID between startValue and endValue
We can see this in action in SSMS.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--QLXziXnt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1nn9v5tds11o9b5n8o8.gif)
We could have gotten the same result set by using the AND operator as well, lets see how we could have constructed it.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--yNPnHoFD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k8mjm1i99jvz450t3uhw.gif)
Between in a sense removes the need to use AND and it simplifies the query statement a bit.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Rg3Q7jDJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/578jfjdw5crthvnpv3um.png)
Thats the basics on how we can narrow things in SQL, I hope you enjoyed this discussion, and for more on SQL you know Where to find me.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--X67kh3Vn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aoqwm21nl1ngbppep0tt.gif)
21