The MySQL WHERE Clause is used to restrict the number of records (or rows) returned by the SELECT Statement. It means, SELECT Statement will return the records only If the condition specified after the WHERE keyword is TRUE.
Let me take on example to demonstrate the functionality of MySQL where clause in real-time. For example, we want to order a shirt from Amazon store. When we type shirt in search bar it will show thousands of shirts. And to customize the search results we have to use filters such as: Gender, Price range between $50 to $100, and brand = Levis, or River Island etc. Once you applied the above filters, it will only display the shirts that are matching the above requirements.
Let me show you, what happens internally (mean MySQL query against the Amazon). It will go something like this:
SELECT Product_Name, Size, Brand, Price, Discount FROM Products_table WHERE Product_Name = 'Shirts' AND Gender = 'Male' AND Brand = 'Levis' OR 'River Island' AND Price BETWEEN 50 AND 100
MySQL WHERE Clause Syntax
The SELECT Statement with WHERE Clause in MySQL can be written as:
SELECT [Column Name 1], [Column Name 2], ...., [Column Name N] FROM Source_Table WHERE Conditions
- Column Names: Please choose the number of columns from the tables. It may be One or more.
- Source_Table: One or more tables present in the Database. Use Joins to join multiple tables.
- Conditions: Here we have to provide the filters, or conditions. If the condition is TRUE then only SELECT Statement will return the records.
MySQL WHERE Clause Examples
In this article we will show you, How to use the MySQL WHERE Clause to filter the extracting data. For this, we are going to use the below shown data
MySQL WHERE Clause Single Condition
In this example we are going to use single condition in MySQL WHERE Clause.
USE world; SELECT Code, Name, Continent, Region, SurfaceArea, IndepYear, Population, LocalName FROM country WHERE Continent = 'Asia';
Above statement will retrieve above mentioned records where the Continent is exactly equal to Asia
MySQL WHERE Clause Multiple Condition
In this example we are going to use Multiple Conditions in MySQL WHERE Clause.
USE world; SELECT Code, Name, Continent, Region, SurfaceArea, IndepYear, Population, LocalName FROM country WHERE Continent = 'Asia' AND Region = 'Southern and Central Asia';
Above statement will retrieve above specified records where Continent is equal to Asia, and Region is equal to Southern and Central Asia.
Let me show you another explain to explain the multiple conditions. In this example we are going to use less than, and greater than operator in the WHERE Clause.
USE world; SELECT Code, Name, Continent, Region, SurfaceArea, IndepYear, Population, LocalName FROM country WHERE Population > 20000000 AND Population < 32164700;
MySQL Where Clause – Command Prompt Example
In this example we will write a query in Command prompt to demonstrate the MySQL where clause along with Multiple Conditions.
USE world; SELECT Code, Name, Continent, Region, SurfaceArea, IndepYear, Population, LocalName FROM country WHERE Continent = 'Asia' AND Population > 40000000;
Above statement will retrieve the above mentioned records where continent is Asia, and population is greater than 40000000.
NOTE: Don’t use ALIAS Column Names in the WHERE Condition. Because WHERE Clause will execute first, and then SELECT Statement will select the Columns. So, MySQL WHERE Clause does not understand the ALIAS Column name declared in the SELECT Statement.
Thank You for Visiting Our Blog