Skip to playerSkip to main content
  • 31 minutes ago
The WHERE clause in MySQL is the key to filtering rows, and this beginner lesson breaks it down with clear examples you can follow step by step. Instead of selecting every record, you learn how to return only the rows that meet a specific condition, whether that means matching a name, comparing salaries, or filtering by gender and birth date.

The lesson walks through comparison operators like equal to, greater than, less than, and not equal to, then shows how greater than or equal to and less than or equal to change the results. It also explains logical operators such as AND, OR, and NOT, including how parentheses affect the order of conditions. The final section covers the LIKE statement for pattern matching, using percent signs and underscores to search names and dates by sequence rather than exact match.

If you are learning SQL for data analytics, this is a practical MySQL tutorial for beginners that builds a strong foundation in filtering data, writing precise queries, and understanding conditional logic.

Ideal for anyone searching for MySQL WHERE clause tutorial, SQL beginner lessons, comparison operators in SQL, logical operators in MySQL, and LIKE statement examples. A useful watch for data analyst training, SQL practice, and anyone who wants to learn how to filter rows in MySQL with confidence.

Category

📚
Learning
Transcript
00:00Hello, everybody. In this lesson, we're going to be taking a look at the WHERE clause.
00:04The WHERE clause is used to help filter our records or our rows of data,
00:08whereas the SELECT statement is used to help filter or select our actual columns.
00:13So when we're using the WHERE clause, we're only going to return the rows
00:17that fulfill a specific condition. Let's take a look at exactly how this works.
00:22Let's say we come right up here. We're going to say WHERE.
00:25And let's go down with that one. Let's say WHERE.
00:28And now we need to specify what column we're about to create this condition for.
00:32So we're going to say FIRST underscore NAME.
00:35So we're saying WHERE the FIRSTNAME, we'll say is equal to,
00:38and let's do quotes, and let's say Leslie.
00:42So we're saying the FIRSTNAME has to be equal to this value right here,
00:47which is Leslie for Leslie, nope.
00:49If we run this, there's only going to be one row that's returned
00:53because Leslie is the only Leslie in this entire table.
00:57Now we just used an equal sign, and that's actually called a comparison operator.
01:02And there's a few other comparison operators that you can use.
01:06Let's take a look at some of these other ones.
01:08Let's pull this down right down here,
01:11and let's actually highlight the SELECT FROM,
01:14and we're going to run it with this one right here.
01:16It's going to only select everything from the whole table.
01:19So we didn't select that WHERE clause.
01:21Let's go right down here, and let's look at this SALARY field.
01:26So I'm going to say WHERE the SALARY,
01:29and I'm going to do a different comparison operator called GREATER THAN.
01:33So WHEN the salary is greater than $50,000.
01:36Now one thing I want to note before we actually run this
01:39is that right down here we have Tom Haverford,
01:42who makes exactly $50,000.
01:44And I think there's one more, Jerry Gergich,
01:47which also makes exactly $50,000.
01:49If we run this,
01:51you'll notice that both Tom and Jerry are not in this output.
01:55But in the SALARY field, everything is greater than $50,000.
01:59The reason for that is that Tom and Jerry made exactly $50,000.
02:04What we're saying right here is where the SALARY is only GREATER THAN.
02:08If we want to include Tom and Jerry,
02:11we have to say GREATER THAN OR EQUAL TO.
02:13And now we'll select $50,000 OR ABOVE,
02:16whereas right here, before when you're doing just this,
02:19it was GREATER THAN $50,000.
02:21It didn't include the $50,000.
02:23Let's go ahead and include it and run this.
02:26And now you'll notice that Tom and Jerry were both included
02:30because they had exactly $50,000,
02:32and we said GREATER THAN OR EQUAL TO.
02:34Now we can do the exact same thing, but with LESS THAN.
02:38So we have LESS THAN $50,000.
02:41And now we only have two people who make LESS THAN $50,000.
02:44That's April and Andy.
02:45And if we say LESS THAN OR EQUAL TO, and we run that,
02:50now we include both Tom and Jerry who make exactly $50,000.
02:54So it's LESS THAN OR EQUAL TO $50,000.
02:56Now what we're going to do is head on over to a different table.
03:01We're going to do the DEMOGRAPHICS TABLE.
03:05Make sure I spell that right.
03:07And let's add our semicolon.
03:08Let's run this.
03:10And what we want to look at is the GENDER really quick.
03:14So we're going to say where the GENDER is equal to,
03:19we'll do in quotes, FEMALE.
03:22And if we run this, we get all the GENDERS that are equal to FEMALE.
03:26But we do have something called the NOT EQUAL TO, and it looks like this.
03:32It's an exclamation point and an equal sign.
03:35This is going to say where the GENDER is not equal to FEMALE.
03:38So if we run this, you'll notice that the GENDER is all male now.
03:42Now so far, we've worked with things like integers, which are numbers.
03:46We've worked with characters or strings, like names.
03:50But there's a different type of data type as well in here.
03:53We have a DATE column for these BIRTHDATES.
03:56Now in the WHERE clause, we can also filter on BIRTHDATES.
03:59Let's come over here, and we'll say BIRTH underscore DATE.
04:04Let's say it's greater than, and within quotes, we'll say 1985-01-01.
04:11This is kind of the standard default date format within MySQL,
04:16which is year, month, and day.
04:18If we go ahead and run this, we can also take all the people
04:21who are greater than or were born greater than 1985.
04:24So all of these dates are greater than 1985.
04:27Now the next thing that I want to take a look at
04:29is logical operators in the WHERE clause.
04:32So logical operators are things like AND, OR, and NOT.
04:38Now these are called, and let's add this, logical operators.
04:42So logical operators allow us to have different logic.
04:46Now let's take a look at how this works exactly.
04:48Let's copy this down, because we already have this one written out.
04:51We're saying where the BIRTHDATE is greater than 1985.
04:55We can also say where the GENDER is equal to male.
04:58We can say AND the GENDER is equal, and then we'll say male.
05:04So we're adding a different complexity,
05:06or an additional conditional statement within our WHERE clause.
05:11Let's go ahead and run this.
05:12So now we're only selecting BIRTHDATES that are greater than 1985,
05:16AND where the GENDER is equal to male.
05:19Only the rows that fulfill both of those are returned.
05:23Now the AND says both this AND this have to be true.
05:28But we could change this.
05:30We could say OR.
05:32What this means is either this one has to be true,
05:35OR this one has to be true in order for it to be returned.
05:39So let's go ahead and run this.
05:41You'll notice that Jerry Gergich was born much before 1985,
05:45but since he has a male gender, he is in our output.
05:49And we could also use the NOT operator by saying OR,
05:53NOT gender equal to male.
05:56So now what this is saying is the BIRTHDATE could be greater than 1985,
06:00OR it could NOT be equal to male, which is female.
06:04So if we look at Leslie Knope, she was born before 1985,
06:09but because she is female, she is in the output.
06:11Now like we talked about in the last lesson,
06:13there is something called PEMDOS,
06:15and that actually applies to these logical operators as well.
06:19So if we run this entire table, let's go ahead and run this.
06:23If we're looking at this entire table,
06:26let's say we want to get someone very, very specific.
06:28Let's say we're going to do where the first underscore name is equal to Leslie,
06:37and their age has to be equal to 44.
06:42That's extremely specific, and we can actually just do it like this.
06:44We don't need quotes for integers.
06:47We could just do the number if we'd like to.
06:49This is very specific.
06:51This is only one person.
06:52But if we put this in parentheses,
06:55we can add an OR over here.
06:58We could say OR the age is greater than, let's just do 55.
07:03Let's go ahead and run this, and then we'll take a look at it.
07:05So within these parentheses, we have an AND operator.
07:09What that means is both this condition has to be met,
07:12AND this condition has to be met.
07:14And that's only one person.
07:15That's Leslie Knope.
07:16But then outside of these parentheses,
07:18we have another conditional statement.
07:20OR the age is greater than 55.
07:22So what we're saying within these parentheses is that
07:25this is an isolated conditional statement.
07:27Within these parentheses,
07:29if this is true, then in our output, it'll be returned.
07:33But then we have an OR condition, which says,
07:35OR someone with the age of greater than 55 can also be in the output.
07:39So these parentheses can be really helpful
07:41when you're actually using it in the WHERE clause
07:43with these AND, ORs, and NOTs.
07:46Now I want to take a look at just one more thing.
07:48And let's bring this down here.
07:52And let's get rid of this entire thing.
07:56Now, the last thing that we're going to take a look at
07:59is a LIKE statement.
08:01Now the LIKE statement is super unique
08:04because we can look for specific patterns.
08:07We're not necessarily looking for an exact match.
08:10Like here, if we said WHERE first underscore name is equal to Jerry.
08:18If we're looking for Jerry, it has to be exactly Jerry.
08:22But if we take this out, say J-E-R, and then we run it,
08:26we get no output.
08:28It has to be an exact match.
08:29But here's where the LIKE statement comes in.
08:32Because we can actually say LIKE J-E-R,
08:36and we can add two special sequences or special characters
08:40within our LIKE statement.
08:42So those special characters are the PERCENT sign and the UNDERSCORE.
08:49The PERCENT sign means ANYTHING,
08:52and the UNDERSCORE means a specific value.
08:55Let's see how that actually works.
08:57So what we're going to do is we're going to say LIKE J-E-R, PERCENT sign.
09:00That's the first one in this LIKE statement.
09:04What this says is the first name is LIKE starting with J-E-R,
09:08but then has anything after it.
09:11It doesn't matter what it is.
09:12As long as it has J-E-R at the very beginning, it will be returned.
09:17Let's go ahead and run this.
09:18Now the only person who starts with J-E-R is Jerry.
09:21But what if I took the J out of here?
09:25Now it's saying it starts with E-R.
09:27And that's not anybody.
09:29What we can do is we can add another PERCENT at the beginning.
09:32This is going to say anything comes before, anything comes after.
09:36All we're looking for is E-R somewhere in their name.
09:40Let's go ahead and run this.
09:42There still is only one person, and that's Jerry.
09:45Now let's come up here, and let's get rid of this.
09:48And let's say we're looking for everyone's name who starts with A.
09:51We can do that really easily by saying A, PERCENT sign.
09:55All that says is it starts with A.
09:57We don't have a PERCENT sign before it, which would say this string just has to have an A somewhere
10:02in it.
10:03If we have it like this, this means an A has to come at the beginning.
10:07Let's go ahead and run this.
10:09In our output, we have April, Ann, and Andy.
10:12Now let's take a look at the underscore.
10:15If we get rid of this PERCENT sign, and we do two underscores, one, two.
10:22This is going to say it starts with an A, and then it has two characters after it.
10:26No more, no less.
10:28So if we run this, Ann is going to be the only person who's returned because she has an A,
10:34and then two characters after it.
10:36Now if we want Andy, we can specify that by doing another underscore.
10:40That's one, two, three.
10:42And now Andy is the only one in our output.
10:45Now there was also April in there, but she had more than three characters.
10:49But we can actually get her in our output by doing a PERCENT sign.
10:53So we can combine both the underscore and the PERCENT sign, and this is going to say it
10:59starts with an A, has one, two, three characters, and then it can have anything after that.
11:06So it just has to have at least an A, and have one, two, three characters after it.
11:10So let's run it.
11:12Now you can see April comes into here because she does have A.
11:16The P, R, and I are the three next characters, but then we have a PERCENT sign that allows
11:22that L to be in the output as well.
11:24Now we don't just have to do this with strings or text like April and Andy.
11:29We could also do this with birthdates.
11:30For example, Andy's birthdate is 1989.
11:33We could say where the birth underscore date is like, let's say we want to look at everyone
11:40who is 1989 or born in 1989.
11:44Let's go ahead and run this.
11:46And Andy's the only person born in 1989, but again, we looked at the year at the very beginning.
11:52So that is how the like statement works.
11:54It looks for a specific sequence within that column that you can search for.
11:58So it doesn't have to be an exact match, as long as it has that specified sequence that
12:03you've put in there anywhere within that cell or that column.
12:06So that is everything that we're going to look at for the where clause.
12:09In the next lesson, we're going to take a look at the group by and the order by within MySQL.

Recommended

  1. RareGear
    4 months ago