×

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

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881

.

Table Number 2: Department

Department_IdDepartment_Name
1Computer Engineering
2Information Technology
3Mechanical Engineering
4Automobile Engineering
5Civil Engineering
6Electrical Engineering
7Electronics and Telecommunication Engineering
8Chemical Engineering

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:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL Logical Operator

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:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202113Yash Dhull908894878590893
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202118Sakshi Patil807874788077782
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL Logical Operator

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:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
SQL Logical Operator

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:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202222Venkatesh Iyer908987909291903
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
SQL Logical Operator

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:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202113Yash Dhull908894878590893
202116Meena Mishra787580748577783
202222Venkatesh Iyer908987909291903
202114Sonali Patole959092889290914
202220Prajwal Lokhande808585757880814
202227Mayuri Wagh808085808285824
202117Mahesh Kumbhar758075788076775
202228Utkarsh Rokade858080908484845
SQL Logical Operator

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:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202114Sonali Patole959092889290914
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202226Shwetali Bhagwat908085889080866
SQL Logical Operator

Related Topics

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

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

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

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

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.

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.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

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

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

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

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

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

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

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 INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.