Logical operators in SQL are used to combine or negate conditions in WHERE
, HAVING
, and JOIN
clauses. They allow you to create complex conditions by combining multiple expressions. The most commonly used logical operators are AND
, OR
, and NOT
.
AND
, OR
, and NOT
.NOT
, AND
, OR
. Use parentheses to change the order of evaluation.TRUE
if all conditions are true.TRUE
if at least one condition is true.TRUE
if the condition is false.%
and _
).WHERE
, HAVING
, and JOIN
clauses to filter and combine data.The AND
operator returns TRUE
if all the conditions separated by AND
are true.
Retrieve employees from Tamil Nadu with a salary greater than ₹50,000.
Name | City | Salary |
---|---|---|
Bala | Coimbatore | 60000 |
Raj | Madurai | 70000 |
The OR
operator returns TRUE
if at least one of the conditions separated by OR
is true.
Retrieve employees from Chennai or Madurai.
Name | City | Salary |
---|---|---|
Anand | Chennai | 50000 |
Raj | Madurai | 70000 |
The NOT
operator negates a condition, returning TRUE
if the condition is false.
Retrieve employees who are not from Tamil Nadu.
Name | City | Salary |
---|---|---|
Kavitha | Karaikal | 55000 |
The EXISTS
operator checks if a subquery returns any rows. It returns TRUE
if the subquery returns at least one row, otherwise FALSE
.
Check if there are any employees in the HR department.
Name | City |
---|---|
Anand | Chennai |
The IN
operator checks if a value matches any value in a list or subquery.
Retrieve employees from Chennai or Madurai.
Name | City |
---|---|
Anand | Chennai |
Raj | Madurai |
The LIKE
operator is used for pattern matching with wildcards:
%
: Matches zero or more characters._
: Matches exactly one character.Retrieve employees whose names start with the letter “K”.
Name | City |
---|---|
Kavitha | Karaikal |
Kumar | Trichy |
The BETWEEN
operator checks if a value lies within a specified range (inclusive).
Retrieve employees with a salary between ₹50,000 and ₹60,000.
Name | Salary |
---|---|
Anand | 50000 |
Bala | 60000 |
Kavitha | 55000 |
You can combine multiple logical operators to create complex conditions. Use parentheses to control the order of evaluation.
Retrieve employees from Tamil Nadu with a salary greater than ₹50,000 or employees from Puducherry.
Name | City | Salary |
---|---|---|
Bala | Coimbatore | 60000 |
Raj | Madurai | 70000 |
Kavitha | Karaikal | 55000 |
Suppose you have a table named Students
that stores student details.
Table: Students
StudentID | Name | City | State | Percentage |
---|---|---|---|---|
1 | Ram | Chennai | Tamil Nadu | 92 |
2 | Karthik | Coimbatore | Tamil Nadu | 88 |
3 | David | Bangalore | Karnataka | 95 |
4 | Kannan | Karaikal | Puducherry | 91 |
5 | Siva | Madurai | Tamil Nadu | 89 |
Retrieve students from Tamil Nadu with a percentage greater than 90 or students from Karnataka.
Name | City | Percentage |
---|---|---|
Ram | Chennai | 92 |
David | Bangalore | 95 |
Check if there are any orders placed by customers from Tamil Nadu.
Retrieve students who scored above 90% in Mathematics or Science.
Retrieve customers whose names contain the substring “an”.
Retrieve orders placed between January 1, 2023, and December 31, 2023.
AND
, OR
, NOT
) are used to combine or negate conditions in SQL queries.AND
returns TRUE
if all conditions are true, OR
returns TRUE
if at least one condition is true, and NOT
negates a condition.%
and _
).WHERE
, HAVING
, and JOIN
clauses.