SQL - Logical Operators
SQL Logical Operators are essential tools used to test the truth of conditions in SQL queries. They return boolean values such as TRUE, FALSE, or UNKNOWN, making them invaluable for filtering, retrieving, or manipulating data. These operators allow developers to build complex queries by combining, negating, or comparing conditions effectively.
In this article, we will explore the various Logical Operators in SQL, their types, and examples. To ensure clarity, all examples will reference a defined sample table.
What Are Logical Operators in SQL?
Logical operators in SQL are used to combine multiple conditions in a query to control the flow of execution. They evaluate whether these conditions are TRUE, FALSE, or NULL, assisting in refining query results effectively. By using these operators, developers can retrieve highly specific data based on given conditions.
We will use the following employee
table throughout the examples. This table represents employee details, including their unique ID, name, city, and country.

Below is the comprehensive list of SQL Logical Operators along with their meanings, detailed explanations, and practical examples:
1. AND Operator
The AND operator is used to combine two or more conditions in an SQL query. It returns records only when all conditions specified in the query are true. This operator is commonly used when filtering data that must satisfy multiple criteria simultaneously.
Let's explore an example to understand how the AND operator works in an SQL query.
Example
Retrieve the records of employees from the employees
table who are located in '
Allahabad
'
and belong to '
India
'
, ensuring that both conditions are met.
Query:
SELECT * FROM employee WHERE emp_city = 'Allahabad' AND emp_country = 'India';
Output

Explanation:
In the output, both conditions (emp_city = 'Allahabad'
and emp_country = 'India'
) are satisfied for the listed employees, so these records are returned by the query.
2. IN Operator
The IN operator simplifies the process of checking if a value matches any value in a list, making it more efficient and readable compared to using multiple OR conditions. This operator is especially helpful when we need to filter results based on multiple possible values for a given column, reducing the complexity of the query.
Example
Retrieve the records of employees from the employee
table who are located in either '
Allahabad
'
or '
Patna
'
.
Query:
SELECT * FROM employee WHERE emp_city IN ('Allahabad', 'Patna');
Output

Explanation:
In this query, the IN operator checks if the value of the emp_city
column matches any value in the list ('Allahabad', 'Patna')
. The query returns all employees who are located in either of these two cities.
3. NOT Operator
The NOT operator is used to reverse the result of a condition, returning TRUE when the condition is FALSE. It is typically used to exclude records that match a specific condition, making it useful for filtering out unwanted data.
Example
Retrieve the records of employees from the employee
table whose city names do not start with the letter 'A'
.
Query:
SELECT * FROM employee WHERE emp_city NOT LIKE 'A%';
Output

Explanation:
In this query, the NOT operator negates the LIKE condition. The LIKE operator is used to match patterns in string data, and the 'A%'
pattern matches any city name that starts with the letter 'A'
. By using the NOT operator, we exclude cities starting with 'A'
from the result set.
4. OR Operator
The OR operator combines multiple conditions in a SQL query and returns TRUE if at least one of the conditions is satisfied. It is ideal for situations where you want to retrieve records that meet any of several possible conditions.
Example
Retrieve the records of employees from the employee
table who are either from '
Varanasi
'
or have '
India
'
as their country.
Query
SELECT * FROM employee WHERE emp_city = 'Varanasi' OR emp_country = 'India';
Output

Explanation:
In this case, the output includes employees from '
Varanasi
'
as well as those who have '
India
'
as their country, even if they are from different cities. The query returns all records where at least one of the conditions is true.
5. LIKE Operator
The LIKE operator in SQL is used in the WHERE clause to search for a specified pattern in a column. It is particularly useful when we want to perform pattern matching on string data. The LIKE operator works with two main wildcards:
- %: Represents zero or more characters. It allows matching any sequence of characters in the string.
- _: Represents exactly one character. It is used when you want to match a specific number of characters at a given position.
Example
Retrieve the records of employees from the employee
table whose city names start with the letter 'P'
.
Query:
SELECT * FROM employee WHERE emp_city LIKE 'P%';
Output

Explanation:
In this case, the output includes only those employees whose emp_city
starts with 'P'
. The %
wildcard ensures that the query matches any city name starting with the specified letter, regardless of how many additional characters follow it.
6. BETWEEN Operator
The BETWEEN operator in SQL allows us to test if a value or expression lies within a specified range. The BETWEEN condition is inclusive, meaning it includes both the lower and in the results. This operator is particularly useful when we need to filter records based on a range of values, such as numerical ranges, dates, or even text values.
Example
Retrieve the records of employees from the employee
table whose emp_id
values fall within the range of 101 to 104 (inclusive).
Query:
SELECT * FROM employee WHERE emp_id BETWEEN 101 AND 104;
Output

Explanation:
In this query, the BETWEEN operator is used to filter employees with emp_id
values ranging from 101 to 104. Since the BETWEEN operator is inclusive, employees with emp_id
values of 101, 102, 103, and 104 will be included in the result set.
7. ALL Operator
The ALL operator in SQL is used to compare a value to all values returned by a subquery. It returns TRUE if the condition specified is TRUE for all values retrieved by the subquery. The ALL operator is commonly used with SELECT, WHERE, and HAVING clauses to ensure that a value satisfies a condition when compared to a set of values.
Example
Retrieve the records of employees whose emp_id
is equal to all emp_id
values in the employees
table where the emp_city
is '
Varanasi
'
.
Query:
SELECT * FROM employee WHERE emp_id = ALL
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');
Output

Explanation:
The query checks whether emp_id
in the outer query is equal to every emp_id
from the subquery (which retrieves emp_id
values from employees in 'Varanasi'
). In this case, the output will include employees whose emp_id
matches all the values in the subquery, i.e., employees with emp_id
values 101, 102, and 103 who are in 'Varanasi'
.
8. ANY Operator
The ANY operator in SQL is used to compare a value with the results of a subquery. It returns TRUE if the value satisfies the condition with any of the values returned by the subquery. This operator allows for greater flexibility when you want to check if a value matches at least one of the results in a set of values.
Example
Retrieve the records of employees whose emp_id
matches any of the emp_id
values in the employees
table where the emp_city
is '
Varanasi
'
.
Query:
SELECT * FROM employee WHERE emp_id = ANY
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');
Output
Explanation:
The output includes employees whose emp_id
matches any of the emp_id
values from the subquery. In this case, the subquery selects emp_id
values from employees in '
Varanasi
'
. The outer query then returns records where emp_id
matches at least one of these values, which includes employees with emp_id
101, 102, and 106.
9. EXISTS Operator
The EXISTS operator in SQL is used to check whether a subquery returns any rows. It evaluates to TRUE if the subquery results in one or more rows. The EXISTS operator is typically used with SELECT, UPDATE, INSERT, and DELETE statements to determine if any rows exist that meet a specified condition. It is often used in correlated subqueries where the subquery references columns from the outer query.
Example
Retrieve the names of employees from the employee
table if there are any employees in the employee
table who are located in 'Patna'
.
Query
SELECT emp_name FROM employee WHERE EXISTS
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');
Output

Explanation:
The output includes the employee names because the EXISTS operator checks if there are any employees from Patna
. If any rows are returned from the subquery, the EXISTS operator returns TRUE, and the employee names are included in the result. The query will return all employees as long as there are employees from Patna
.
10. SOME Operator
The SOME operator in SQL is used in conjunction with comparison operators such as <
, >
, =
, <=
, etc., to compare a value with the results of a subquery. It returns TRUE if the condition is met with any value returned by the subquery. The SOME operator allows us to perform comparisons with any of the values returned by a subquery, and it is particularly useful when we want to match a value against a set of values rather than a single value.
Example
Retrieve the records of employees from the employee
table where the emp_id
is less than any of the emp_id
values from employees located in 'Patna'
.
Query:
SELECT * FROM employee WHERE emp_id < SOME
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');
Output

Explanation:
The output includes employees whose emp_id
is less than any of the emp_id
values of employees located in '
Patna
'
. In this case, the query checks if the emp_id
values are less than the corresponding emp_id
values from the 'Patna'
employees. If the condition is satisfied for at least one of the values in the subquery, those employees are included in the result.
Conclusion
SQL Logical Operators are crucial for building complex queries that filter and retrieve data efficiently. From combining conditions with AND and OR to using pattern-matching capabilities with LIKE, these operators enhance the functionality of SQL queries. By mastering these operators, developers can optimize their queries and achieve precise data manipulation. Understanding these operators is essential for database management, whether we are working with small-scale projects or enterprise-level systems