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 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. 


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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Safe Practice to add Links to cross-origin destinationUI/UXHow to add transparent text on top of an image using CSS?CSSDifferent Types of Functions in JavaScriptJavaScriptHow to convert an object from API to JSON array in Angular 9?AngularJavaScript Number MethodsJavaScriptDid you get an error while displaying Special Characters in JavaScript?JavaScript