Locked lesson.
About this lesson
We can select data based on multiple conditions using the AND & OR clause
Exercise files
Download this lesson’s related exercise files.
AND & OR Clause.docx58.8 KB AND & OR Clause - Solution.docx
59 KB
Quick reference
AND & OR Clause
We can search for multiple conditions in our Where clause using "And" & "Or".
When to use
Anytime you want to search for more than one thing, use And/Or.
Instructions
To use AND/OR, just add them to your Where clause:
Where [Age] > 21 AND [First Name] LIKE 'John'
or...
Where [Age] > 21 OR [First Name] LIKE 'John'
Hints & tips
- Anytime you want to search for more than one thing, use And/Or.
- AND means everything has to be true
- OR means at least one of the things has to be true
- 00:05 In this video I wanna talk about the and and the or clause.
- 00:08 So these things that we're doing here are called conditionals, they're conditions.
- 00:12 Where age is greater than 20.
- 00:15 That's a condition.
- 00:16 When that condition is met we get output, right?
- 00:20 Well, we can have multiple conditions, we can have many conditional statements on
- 00:25 here and we string them together with ands or ors depending on what we want to do.
- 00:29 So let me just create one really quickly where age greater
- 00:35 than 20 and let's say first name is like John.
- 00:40 Execute this.
- 00:41 So where age is greater than 20 and the first name is like John,
- 00:45 we get one record.
- 00:46 John Edler 39.
- 00:48 So what's the difference between AND and OR?
- 00:51 It's a pretty big difference.
- 00:52 And means everything has to be true, so
- 00:56 it's true that the age is greater than 20 and the name is like John.
- 01:02 Both of these things have to be correct in order to return something.
- 01:06 If we change this to Tracy,
- 01:08 we would get nothing here because we don't have any people named Tracy.
- 01:13 So both of these things have to exist basically in order for
- 01:16 this to return something under and not so for or.
- 01:21 For or, only one of them has to be correct.
- 01:24 So if we go age greater than 20 or name like John, we execute this,
- 01:28 we get a whole bunch of results, because there's a whole bunch of people with
- 01:33 ages greater than 20, even though there's only two people with names like John.
- 01:39 And only one of those is over 20 years old, even though that's true,
- 01:44 we get all of these responses.
- 01:46 So for or, only one thing has to exist.
- 01:50 So we're gonna return everybody with an age over 20 or
- 01:54 everybody with first name John.
- 01:55 So if we change this around a little bit.
- 01:57 If we go less than 20 or name like John, and
- 02:01 execute this, we get two returns right?
- 02:03 Because we have two people named John, so it returns both of them, and
- 02:07 we have one person age below 20, John Smith, so it returns that.
- 02:12 So very, very subtle distinction, so if we change this to and
- 02:17 boom, we go back to just having one, cuz both of these have to exist.
- 02:21 Has to be less than 20 years old and
- 02:23 have a name like John, unlike before where we just have or, we get both of them.
- 02:28 So play around with this and and or.
- 02:30 It's very, very useful.
- 02:32 You're gonna use this a lot because you're often gonna wanna search for
- 02:37 many different things.
- 02:38 I want somebody who's older than 40 years old who lives in the Seattle area
- 02:43 who bought our blue widget but not our red widget, go.
- 02:47 Right? So you can string this together ands and
- 02:49 ors to get exactly the data that you want.
- 02:52 Very easy, just one line of code here and
- 02:55 we can specify exactly what we want at any given time.
- 02:58 So that's and and or used with the where clause.
- 03:02 So in the next video we are going to look at ordering and
- 03:04 grouping, order by and group by.
Lesson notes are only available for subscribers.