What are MySQL Operators?

MySQL Operators

MySQL Operators are your friend in fact. In MySQL, just as in all programming languages, we have these nifty little things called operators. Operators are pretty darn important, as they allow us to perform meaningful operations on our data. Some of the MySQL operators include LIKE, LIKE%…%, NOT LIKE, =, !=, REGEXP, REGEXP^…$, NOT REGEXP, =”, !=”, IN(…), NOT IN(…), BETWEEN, NOT BETWEEN, IS NULL, and IS NOT NULL. Wow, that’s a lot of operators. Yep, it sure is. You might be wondering, so how the heck am I going to use all of these operators and what are they good for? Those are great thoughts, and we’ll jump right into some of the more common use cases now.

>= and <=

Using the greater than or equal to along with the less than or equal to operators is pretty cool. Let’s remember our friends table that we’ve been working with throughout this MySQL series. What if we wanted to check to see who was born during the 1980’s, how could we do that? Let’s check out a query of how to accomplish this:

Pretty Slick! By including these operators in our select statement, we found that Jim Jones was the only person from our friends that was born in the Awesome 80’s. Be honest, you love Duran Duran.


It turns out using a combination of greater than or equal to and less than or equal to is a pretty common scenario. MySQL gives you a dedicated operator to do this same thing with much less typing. It works just like this:

Voila! You see, it works just the same – Jim Jones is our only Hair Band Hero.

Not Between

Now check it out. I know you like that between operator. Today is your lucky day partner, because not only does MySQL give you a between operator, they also provide you with a not between operator. Use this to filter out anything not in a range. So we know anyone else that wasn’t born in the 80’s is not so cool. So let’s check for the non cool friends now:

Jumpin Jack Flash! Did you see that?! Our not between operator just gave us a list of friends not born in the 80’s with ease, style, and grace. So cool.


This is a great operator and you may be used to using it in your other programming efforts. When using JavaScript or PHP, we often use the or operator in conditional branches. Do this action if this is true, or that is true. We can use it in MySQL as well and it is quite handy. Let’s look for anyone born in 1972 or 1973. Check it:

So we can see that using by using or, we were able to grab results for two scenarios. We can add many together as well. Let’s look for people born in the year 1967, 1970, and 1977.


This operator is also quite useful. Instead of chaining so many or operators together, you could use the in operator to fetch similar results. Here is the syntax for this approach:

Just like any other programming language or approach, there are many ways to skin the cat so to speak. We’re simply looking at some common approaches, then you get to choose which works best for you.

Like and Not Like

The like and not like operators are very useful, especially if used with wildcard characters. So first up, we can try to find everyone in our last name column where they name is like Penblanc.

Attention Grasshopper! Did you notice that we just did two queries right there? The first one returned an empty set, why is that? You would think that Penblan is like Penblanc. We were looking for all the Penblancs in our table, searching for where something is like Penblan seems reasonable enough, no? Well, it turns out with like, you either have to have an exact match or use a wild card. The second query uses a wildcard in the last character position of the Penblanc last name. You can see that it successfully returns the two entries for Penblanc in the column when we search this way.

Another scenario with the like operator is in conjunction with the multi-character wildcard, which in MySQL is the percent % symbol. Imagine we had no clue how to spell Emmet’s last name of Brickowski. If we at least know that the first part of his name starts with Brick, then we can combine what we know with a multi-character wildcard symbol and get the data we need. Let’s try it out:

I see an Emmet right there, do you?! Man, that Lego Movie is awesome. If you haven’t seen it, you absolutely must! If you get nothing else out of this tutorial – make sure to watch Emmet save the world in the Lego Movie. You’ll have a better day because of it.

The other side of the coin with like is not like. In other words, we can do negation. Let’s practice. Show me everyone who was not born in 1972.

That’s pretty slick. In the first query, we return all friends in our friends table and we can see that Jacob, Mary, and Sofia were all born in 1972. With the second query we use the not like operator to perform negation and show all people who were not born in 1972. Very cool!

A few more examples that include use of wildcard characters is in order. Let’s say that we want to see all friends that have the letter e within their first name. Let’s try it out:

The query above says to use as many wildcard characters as you need until you hit one letter e, then include all remaining characters to the right of that one. If you’re familiar with regular expressions, this style of searching might ring a bell with you.

How about friends that have a last name which begins with the letter V?

Ah yes, there she is, our Sweet Sofia.

What about friends that have the letter o in their last name? Let’s try it out:

It looks like Emmet, Frankie, Jacob, Jim, Mary, and Sheila all have last names that contain the letter o.

Now You Know What MySQL Operators Are!

In this episode we covered a lot of ground in dealing with MySQL operators and how to apply them to your queries to get the data you want. These examples just scratch the surface of what you can do with operators. In some ways, the examples may seem trivial, but the main takeaway is that you can see how we are able to think about what we want (declarative nature), and construct the MySQL syntax to make that happen.