×

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 Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

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 Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.