×

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause.

First, let's understand the concept of WHERE clause.

WHERE clause is used to specify a condition while retrieving records from a table.

WHERE clause is generally used with SELECT statement in SQL

The SELECT query will display only the records satisfying the condition specified in the WHERE clause

There can be one or more than one condition specified in WHERE clause condition of a SELECT query.

The AND and OR operators are used to check multiple conditions using the WHERE clause in a single query.

  • AND operator with WHERE clause

Whenever AND operator is used in between the conditions with WHERE clause to check multiple conditions in SELECT query, the results are displayed when only more than one condition given in the query is met.

Syntax:

SELECT *FROM table_name WHERE Condition 1 AND Condition 2 [AND Condition 3];

Example 1:

Consider the students table given below. We will consider the same table for all the following examples.

Student_RollNoStudent_NameStudent_GenderStudent_MobileNumberStudent_HomeTownStudent_AgeStudent_Percentage
1Rohit MoreMale9890786123Lucknow2375
2Kunal ShahMale7789056784Chandigarh2068
3Kartik GoenkaMale9908743576Ahemdabad2292
4Anupama ShahFemale8890907656Chandigarh2494
5Snehal JainFemale8657983476Surat2194

Now, let us see few examples to understand this concept practically.

mysql> SELECT *FROM students WHERE Student_Gender = "Male" AND Student_HomeTown = "Chandigarh";

Output:

SQL WHERE Multiple Conditions

There is only one record in the students table with roll number 2, which has gender as male and home town as Chandigarh. Even if any one of the specified conditions is not met, then, in that case, the output will not be the same. The record/records displayed will be different, or in some cases, the output may be an empty set if no record satisfies both conditions.

Example 2:

mysql> SELECT *FROM students WHERE Student_Age = 21 AND Student_Percentage = 94;

Output:

SQL WHERE Multiple Conditions

There is only one record with roll number ‘5’ in the students table, which has age equals to ‘21’ and percentage equal to ‘94’.

Example 3:

mysql> SELECT *FROM students WHERE Student_Gender = "Female" AND Student_HomeTown = "Chandigarh" AND Student_Age = 24;

Output:

SQL WHERE Multiple Conditions

Among all the records in the students table, there is only one record with roll number ‘4’, which has gender as ‘male’, the home town as ‘Chandigarh’, and age equals to '24'. This is the only record that satisfies all three conditions.

Example 4:

mysql> SELECT *FROM students WHERE Student_Gender = "Male" AND  Student_Name = "Kartik Goenka" AND Student_Percentage = 92;

Output:

SQL WHERE Multiple Conditions

Among all the records in the students table, there is only one record with roll number ‘3’ in which all the three conditions are met, i.e., gender as male, name equals to 'Kartik Goenka' and percentage equals to '92'.

  •  OR operator with WHERE clause

Whenever OR operator is used in between the conditions with WHERE clause to check multiple conditions in SELECT query, then the results are displayed when at least one condition is met among all the other conditions written in a query.

Syntax:

SELECT *FROM table_name WHERE Condition 1 OR Condition 2 [OR Condition 3];

Example 1:

mysql> SELECT *FROM students WHERE Student_Gender = "Male" OR Student_HomeTown = "Chandigarh";

Output:

SQL WHERE Multiple Conditions

In students table, there are four records with roll number 1, 2, 3 and 4 which has gender as male or home town as Chandigarh. Even if any specified conditions are met, that record will be considered as part of the output. In some cases, the output may be an empty set if no record satisfies either condition.

Example 2:

mysql> SELECT *FROM students WHERE Student_Age = 21 OR Student_Percentage = 94;

Output:

SQL WHERE Multiple Conditions

There are two records roll numbers '4,' and ‘5’ in the students table, which has either age equals to ‘21’ or percentage equals to ‘94’.

Example 3:

mysql> SELECT *FROM students WHERE Student_Gender = "Female" OR Student_HomeTown = "Chandigarh" OR Student_Age = 24;

Output:

SQL WHERE Multiple Conditions

Among all the records in the students table, there are three records with roll number ‘2’, ‘4’ and ‘5’, which has either gender as ‘female'; home town as ‘Chandigarh’ or age equals to ‘24’. These are the records that satisfy either of the three conditions.

Example 4:

mysql> SELECT *FROM students WHERE Student_Gender = "Male" OR  Student_Name = "Kartik Goenka" OR Student_Percentage = 92;

Output:

SQL WHERE Multiple Conditions

Among all the records in the students table, there are three records with roll numbers '1’, ‘2’ and ‘3’ in which either of the three conditions is met, i.e., gender as male, name equals to ‘Kartik Goenka’ or percentage equals to ‘92’.

  • AND and OR operator with WHERE clause in a single query

In WHERE clause query with a SELECT statement, we can also use a combination of AND and OR operators in a single query. The purpose of using the combinations of AND and OR operators in a single query is to test the higher level of complicated conditions.

Syntax:

SELECT *FROM table_name WHERE Condition 1 AND/OR Condition 2 [AND/OR Condition 3];

Example 1:

mysql> SELECT *FROM students WHERE Student_Gender = "Female" AND Student_HomeTown = "Chandigarh" OR Student_Age = 24;

Output:

SQL WHERE Multiple Conditions

According to the conditions in a query, the record should have gender as 'female' and home town as 'Chandigarh'. The age of the student may or may not be ‘24’. So, there is only one record with roll number ‘4’, which meets these conditions. Even if any record has gender as ‘female’ and home town other than ‘Chandigarh’ or vice versa, then that record will not be considered in output.

Example 2:

SELECT * FROM students WHERE Student_Gender = "Female" OR Student_HomeTown = "Chandigarh" AND Student_Age = 24;

Output:

SQL WHERE Multiple Conditions

According to the conditions in a query, the record should have either gender as ‘female’ or home town as 'Chandigarh'. The age of the student should not be other than ‘24’. So, there are two records with roll number ‘4’ and ‘5’ which meet these conditions.

Example 3:

mysql> SELECT *FROM students WHERE Student_Gender = "Male" AND Student_HomeTown = "Lucknow" OR Student_Age = 23 AND Student_Percentage = 75;

Output:

SQL WHERE Multiple Conditions

There is only one record in students table with gender as 'male', the home town as ‘Lucknow’ and the percentage of the student should be '75'. The age of a student can be 23 or other than 23.

Example 4:

mysql> SELECT *FROM students WHERE Student_Gender = "Male" OR Student_HomeTown = "Lucknow" AND Student_Age = 23 OR Student_Percentage = 75;

Output:

SQL WHERE Multiple Conditions

In the students table, there are three records which have gender as 'male' and age as ‘23’. The home town may or may not be ‘Lucknow’. Also, the percentage may or may not be '75'.


Related Topics

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.