Yogesh Chauhan's Blog

Some Useful Operators in The SQL WHERE Clause

in SQL/MySQL on August 24, 2019

There are many operators in WHERE clause in SQL but we are going to take a look the 4 difficult ones only. Let's take a look at the syntax of WHERE first.


SELECT column1, column2, ...
FROM table_name
WHERE condition;
  1. Not equal

Now we always use operators like CustomerID=1 or country=USA etc. etc. But what if you want to get all the records except CustomerID=1 or country=USA? That's when the Not equal operator comes into picture. Let's take a look at the example. 

Let's apply the following queries and see what results we get.


SELECT * FROM city LIMIT 10;
SELECT * FROM city WHERE Population <> 1780000 LIMIT 10;

The first one gives the whole database table without any condition but the second one gives us data without the Population=1780000. Take a look at the results in the demo link provided at the end of the article. [I am using the database from MySQL classic DB. Click here to read more]

Note: In some versions of SQL this operator may be written as !=

  1. IN

This operator gives us results within the given value set. Take a look at the following queries.


SELECT * FROM city WHERE CountryCode IN ('AFG','ANT');

As you can see in the demo, it will give us the results with CountryCode AFG and ANT only and will omit other results. 

  1. LIKE

This operator, as per the name, gives results which are alike to the query, sort of. Let's take a look at the example,


SELECT * FROM city WHERE CountryCode LIKE 'r%' LIMIT 10;

As you can see the results in the demo, the query returns the rows with CountryCode starting with "r" only. 

  1. BETWEEN

As per the name suggests, BETWEEN operator gives us results between certain values for example price or population. Take a look at the following query.


SELECT * FROM city WHERE Population BETWEEN 10000 AND 20000 LIMIT 10;

As you can see the query and the results in the demo link, the BETWEEN operator helps us to find out the results for a specific range, in this example population between 10000 to 20000.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to create a flip pricing table using CSS and JavaScript?CSSJSON.stringify() in JavaScriptJavaScriptWhat is the difference between float and double?MiscellaneousHow to Use the EXISTS and NOT EXISTS Operator with a Subquery in SQL and MySQL?SQL/MySQLA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSSWhat are components in Angular?Angular