Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
WHERE Clause.docx58.7 KB WHERE Clause - Solution.docx
58.9 KB
Quick reference
WHERE Clause
The Where clause allows us to pull out more specific information from our database.
When to use
Anytime you want to pull out specific information, use the where clause.
Instructions
Where clauses go at the end of your SQL statement and allow you to pull out specific information using comparison operators.
For example: WHERE [Age] > 21
Comparison operators
= Equal
<> Not Equal
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Less Than or Equal To
Like
Between
Hints & tips
- Use comparison operators with your WHERE clause to pull out specific information.
- 00:05 In the last video, we talked about the select statement.
- 00:07 In this video, I want to expand on the select statement with the where clause.
- 00:12 And the where allows us to create more specific information,
- 00:15 to pull out more specific details.
- 00:17 And it's very easy, we just come down here and type in WHERE, and
- 00:21 then we give it some specific detail that we want.
- 00:23 So let's come up here and change this from all records to, let's go First Name.
- 00:33 And as I start to type, I can tab in order to do that.
- 00:37 And let's go Age, so we want to display the First Name, the Last Name,
- 00:42 and the Age of everybody in our database table,
- 00:46 where First Name is like John.
- 00:51 So if we execute this, we're gonna get an error, cuz we want single quotation marks.
- 00:57 Here we have John Elder and John Smith.
- 01:00 That's as easy as this is.
- 01:03 What can we do here?
- 01:04 I use this like, let me pull up this list.
- 01:07 You can also use these conditional operators, equal, not equal, greater than,
- 01:12 less than, greater than or equal to, less than or equal to, between and like.
- 01:16 Now, between is usually a range,
- 01:18 we wanna say give us everybody with ages between 12 and 25, or whatever.
- 01:23 Like is a pattern search, First Name like John, right.
- 01:28 And you might think why don't I use equal?
- 01:30 So I want the First Name to equal John.
- 01:34 Well if we execute this we get this error, the data types text and
- 01:38 var char are incompatible with equal.
- 01:41 Now if we changed First Name to Age, and
- 01:46 we wanted everyone whose age is 18, John Smith is 18.
- 01:51 If we want everybody whose age is greater than 18, we could get all these.
- 01:58 You'll see, John Smith isn't on this list, because he's only 18.
- 02:02 If we changed this to less than I don't know, let's say 20.
- 02:08 Execute, then get John Smith again.
- 02:11 We could do between, where age between, let's say 30 and 40.
- 02:17 We execute that, John Alder is 39, Steve Jansen is 32.
- 02:24 That's the where clause, very, very useful.
- 02:28 And like I said, you use these equal, not equal, greater than, less than.
- 02:33 If you've taken basic math classes in elementary school, you're
- 02:36 familiar with these equal to and greater than or equal to signs and the between.
- 02:42 You just write it out like this between two number ranges or
- 02:46 the like, as we did earlier.
- 02:48 So, that's the where clause.
- 02:50 In the next video, we'll talk about the and and the or clause.
Lesson notes are only available for subscribers.