Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

SQL – Logical Operators

  • Last Updated : 18 Oct, 2021

SQL logical operators are used to test for the truth of the condition. A logical operator like the Comparison operator returns a boolean value of TRUE, FALSE, or UNKNOWN.

Given below is the list of logical operators available in SQL.

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

Operator Meaning
ANDTRUE if both Boolean expressions are TRUE.
INTRUE if the operand is equal to one of a list of expressions.
NOTReverses the value of any other Boolean operator.
ORTRUE if either Boolean expression is TRUE.
LIKETRUE if the operand matches a pattern.
BETWEENTRUE if the operand is within a range.
ALLTRUE if all of a set of comparisons are TRUE.
ANYTRUE if any one of a set of comparisons is TRUE.
EXISTSTRUE if a subquery contains any rows.
SOMETRUE if some of a set of comparisons are TRUE.

In the below example, we will see how this logical operator works.

Step 1: Creating a Database 



In order to create a database, we need to use the CREATE operator.

Query:

CREATE DATABASE xstream_db;

Step 2: Create table employee 

In this step, we will create the table employee inside the xstream_db database.

Query:

CREATE TABLE employee (emp_id INT, emp_name VARCHAR(255), 
                                  emp_city VARCHAR(255),
                                  emp_country VARCHAR(255),
                                  PRIMARY KEY (emp_id));

In order to insert the data inside the database, we need to use the INSERT operator.



Query:

INSERT INTO employee VALUES (101, 'Utkarsh Tripathi', 'Varanasi', 'India'),
                            (102, 'Abhinav Singh', 'Varanasi', 'India'), 
                            (103, 'Utkarsh Raghuvanshi', 'Varanasi', 'India'),
                            (104, 'Utkarsh Singh', 'Allahabad', 'India'),
                            (105, 'Sudhanshu Yadav', 'Allahabad', 'India'),
                            (106, 'Ashutosh Kumar', 'Patna', 'India');

Output: 

Now the given below is an example of the logical operators.

AND Operator

Query : 

SELECT * FROM employee WHERE emp_city = 'Allahabad' AND emp_country = 'India';

Output: 

IN Operator

Query:



SELECT * FROM employee WHERE emp_city IN ('Allahabad', 'Patna');

Output: 

NOT Operator

Query:

SELECT * FROM employee WHERE emp_city NOT LIKE 'A%';

Output:

OR Operator

Query:

SELECT * FROM employee WHERE emp_city = 'Varanasi' OR emp_country = 'India';

Output:

LIKE Operator

Query:

SELECT * FROM employee WHERE emp_city LIKE 'P%';

Output:

BETWEEN Operator

Query:

SELECT * FROM employee WHERE emp_id BETWEEN 101 AND 104;

Output:

ALL Operator

Query:



SELECT * FROM employee WHERE emp_id = ALL 
                (SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');

Output:

ANY Operator

Query:

SELECT * FROM employee WHERE emp_id = ANY
                (SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');

Output:

EXISTS Operator

Query:

SELECT emp_name FROM employee WHERE EXISTS
                (SELECT emp_id FROM employee WHERE emp_city = 'Patna');

Output:

SOME Operator

Query:

SELECT * FROM employee WHERE emp_id < SOME 
                (SELECT emp_id FROM employee WHERE emp_city = 'Patna');

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :