×

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 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 UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

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

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

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

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 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 Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

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

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

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

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

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.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

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.

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.

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.