Learn SQL: Microsoft SQL Server - Episode 3: Where Clause - Part 1
Tags: sql, database, beginners
Published: 2021-10-26 18:05:42

![goran-where?](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 <u>where</u> a **WHERE** clause can help us.

We will be discussing the **Where** clause and the syntax to use it.

![skid-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a78lf54g6wn5mk8x85o8.png)

####Where Clause Statements
**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.

![skid-09](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 <abbr title="SQL Server Management Studio">SSMS</abbr>.

![skid-6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/578jfjdw5crthvnpv3um.png)
####Operator
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.

![skid-09](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c0dsqw7oxs224eqzsxy.gif)

In <abbr title="SQL Server Management Studio">SSMS</abbr> lets start using the _Where clause_ in our query statements.

![where-query-statement](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.

![skid-11](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.

![where-query-and-statement](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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azl3r9dtgs4doi1bnv0h.gif)

In this case we use the **OR** operator:

![where-query-or-statement](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.

![where-query-or-and-many-statement](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.

![skid-09](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c0dsqw7oxs224eqzsxy.gif)

Back in <abbr title="SQL Server Management Studio">SSMS</abbr>, 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. 

![where-query-more-than](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**_:

![where-query-more-than-or-equal](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:

![where-query-less-than](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**_:

![where-query-less-than-or-equal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arqubfzogsptegzdjswk.gif)

![skid-4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a78lf54g6wn5mk8x85o8.png)


####Between

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 <abbr title="SQL Server Management Studio">SSMS</abbr>.

![where-query-between](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.

![where-query-between-with-And](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.

![skid-6](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.

![meditation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aoqwm21nl1ngbppep0tt.gif)