# SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples.

The SQL Logical Operator displays the query result in one form. The query will display the data, whether it is true or false. The Logical operator is used to return true data or false data to merge one or more than one true or false data.

The operator falls under the logical operator category is as follows:

1. SQL OR Operator
2. SQL AND Operator
3. SQL NOT Operator
4. SQL BETWEEN Operator
5. SQL IN Operator
6. SQL LIKE Operator

Let's learn about each operator one by one with the help of examples

Consider the already existing table, which has the following data:

Table Number 1: - Diploma_Student

.

Table Number 2: Department

## 1. SQL OR Operator:

The SQL OR Operator displays those values that meet any conditions in the given SQL query. The OR operator is used with the SELECT statement, the UPDATE statement, and the DELETE statement.

Let’s understand how to use the OR operator in the SQL query with the help of an example.

Example: Execute a query to display the student’s information from the Diploma_Student table where the department id is 2, OR the fifth-semester percentage is greater than 80.

`SELECT * FROM Diploma_Student WHERE Department_Id = 2 OR Fifth_Sem > 80;`

Here, we have executed the SELECT query with the WHERE clause on the Department_Id field and the Fifth_Sem Field with the OR operator in between both the conditions. Any data in the Diploma_Student table that satisfies any given expression in the query will only be considered in the output.

The output of the above query is:

## 2. SQL AND Operator

The SQL AND Operator are used to display those values which meet both the conditions in the given SQL query. The AND operator is used with the SELECT statement, the UPDATE statement, and the DELETE statement.

Let’s understand how to use the AND operator in the SQL query with the help of an example.

Example: Execute a query to display the student’s information from the Diploma_Student table where the First_Sem percentage is greater than 75 and the Second_Sem percentage is less than 90.

`SELECT * FROM Diploma_Student WHERE First_Sem > 75 AND Second_Sem < 90;`

Here, we have executed the SELECT query with the WHERE clause on the First_Sem field and the Second_Sem Field with the AND operator in between both the conditions. Any data in the Diploma_Student table that satisfies the query's given expression will only be considered in the output.

The output of the above query is as follows:

## 3 SQL NOT Operator

The SQL NOT Operator displays the reverse values of the conditions given in the SQL query. The NOT operator is used with the SELECT query, the UPDATE query, and the DELETE query.

Let’s understand how to use the NOT operator in the SQL query with the help of an example.

Example: Execute a query to display the student’s information from the Diploma_Student table where the total percentage is not between 85 and 92.

`SELECT * FROM Diploma_Student WHERE Total NOT BETWEEN 85 AND 92;`

Since, we want to display only those student's information whose total percentage is not between the 85 and 92 in the above query

The output of the above query is as follows:

## 4. SQL BETWEEN Operator:

The SQL BETWEEN Operator displays the record between the starting and ending values.

Let’s understand how to use the BETWEEN operator in the SQL query with the help of an example.

Example: Execute a query to display the student’s information from the Diploma_Student table where department id is between 2 and 5.

`SELECT * FROM Diploma_Student WHERE Department_Id BETWEEN 2 AND 5;`

We have displayed the student's records whose department id is between 2 and 5 in the above query.

The output of the above query is as follows:

## 5. SQL IN Operator

The SQL IN Operator is used to list out one or more than one values in the SQL query

Let’s understand how to use the IN operator in the SQL query with the help of an example.

Example: Execute a query to display the student’s information from the Diploma_Student table where department name is ‘Mechanical Engineering’, ‘Automobile Engineering’, and ‘Civil Engineering’.

`SELECT * FROM Diploma_Student WHERE Department_Id IN (SELECT Department_Id FROM Department WHERE Department_Name IN ('Civil Engineering', 'Mechanical Engineering', 'Automobile Engineering'));`

We have displayed the student's information where department name is 'Mechanical Engineering', 'Automobile Engineering', and 'Civil Engineering'.

The output of the above query is as follows:

## 6. SQL LIKE Operator

The SQL LIKE Operator displays only those records from the table that match the specific pattern mentioned in the query. The two pattern matching wildcard characters used with the LIKE operator are Percentage (%) and Underscore (_).

Let’s understand how to use the LIKE operator in the SQL query with the help of an example.

Example: Execute a query to display the student’s information from the Diploma_Student table where the Student name starts with the letter 'S'.

`SELECT * FROM Diploma_Student WHERE Student_Name LIKE 'S%';`

We have fetched those student’s information whose name starts with the letter ‘S’.

The output of the above query is as follows: