DEV Community

Cover image for Learn SQL: Microsoft SQL Server - Episode 3: Where Clause - Part 2
Goran Kortjie
Goran Kortjie

Posted on • Updated on

Learn SQL: Microsoft SQL Server - Episode 3: Where Clause - Part 2

goran-where-2

WHERE is an incredible method to narrow down results from a database based on our specified criteria. But what if we wanted to select records based on a pattern?

In that case we can use the LIKE operator in the WHERE clause.

like

Like Operator

The LIKE operator in WHERE allows you to match a pattern.

The syntax is as follows:

Select [Column Names | *]
Where [Column Name] Like [Criteria]
Enter fullscreen mode Exit fullscreen mode

We can add additional options as well.

Select [Column Names | *]
Where [Column Name] Like [Criteria]
AND | OR [Column Name] [Operator] [Criteria]
Enter fullscreen mode Exit fullscreen mode

We can see this in action inside SSMS.

skid-7

Let's start off with a New Query. Remember to make sure the AdventureWorks database is selected on the dropdown menu, otherwise the query statements will not work as expected.

This time we will be using a different table than before, instead of the Person.Person table, we will be using the Production.Product table.

skid-7

Let's see what kind of records are available to us in the Production.Product table. We can use a simple select all to get all the data from this table.

select-all-production-product

We get the result set of this table as expected. But let's say we want to see all the products that start with the letter A. In this case we can use a LIKE statement.

skid-19

We construct this query by specifying the Column name in the Where clause and the A in the LIKE clause, since we want to match all the products that start with the letter A.

But we also want to match all the words that follow after A, to do this we put in a Wild-Card. A Wild-Card matches anything. How it will look is as follows:

For example

Select *
Where name Like 'A%'
Enter fullscreen mode Exit fullscreen mode

The % is the Wild-Card character and it will match everything after A.

where-in-like

skid-19

If we wanted, for instance to find all the products names that ended with an A, then we can move the % sign to the front and the A to the back.

where-in-like-percent

What if we wanted to find a product name that has the word 'paint' in it and we do not care where in the product name 'paint' resides.

skid-7

We could do this by adding the Wild-Card at the start of our criteria and at the end of our criteria.

where-in-like-percent-on-both-sides

This means that no matter what a paritcular word starts with and no matter what it ends with, it should have the word 'paint' in it.

Remember the query window is case-incensitive

skid-7

Multiple values in Where Clause

We have discussed how to select values from a table, using a Where clause and a specifed value from a column.

For example: If we wanted to select a value in a table where the ID is 123.

Select *
From [Table Name]
Where ID = 123
Enter fullscreen mode Exit fullscreen mode

If we wanted to select values where ID is 123 or 234 we would construct it as follows.

Select *
From [Table Name]
Where ID = 123
Or ID = 234
Enter fullscreen mode Exit fullscreen mode

When we need to add in more OR statements then we just need to specify it as we did before and we can do this as many times as we prefer.

skid-19

This is generally fine in a situation where we have a limited amount of values, two or maybe even ten values. But if we have hundreds of values for ID then it becomes cumbersome to manage and SQL becomes difficult to read and understand.

This is where another SQL clause comes into play, this clause is called IN. The syntax for using an IN query statement is similar and simple to what we are used to.

Select [Column Names | *]
From [Table Name]
Where ID in (criteria, criteria, criteria...)
Enter fullscreen mode Exit fullscreen mode

For example:

Select [Column Names | *]
From [Table Name]
Where ID in (123, 434, 234, 233, 112, 431, 332...)
Enter fullscreen mode Exit fullscreen mode

We enclose the criteria in parenthesis and separate them by a comma. We can see how this works in practice inside SSMS.

skid-7

We are going to use the Person.Person table to demonstate IN.

Let's assume that we were given values from the sales department and they wanted to know the first name and last name of a particular group of people. The values they have provided are ID's.

The cumbersome way of performing this type of query would be as follows.

where-or-statement

As we discussed we can perform the same query by using the IN query method.

where-query-with-in

As we can see the same result set is returned if we run both queries simultaneously.

skid-19

If for instance you have to use sting values, then you need to enclose them in single quotes. For example we need to find the values for firstName. We would construct the query statement as follows:

where-query-with-in-string

If we want to use Date values, then we would also use single quotes.

skid-2

Thats it! Well done on making it this far, we have seen how we can narrow down our result sets even further with Where and Like, so I hope you Liked this article. See you next time...

Image description

Top comments (0)