đ The AND operator in SQL is a logical operator used to combine two or more conditions in a query. It returns records only when all specified conditions are true. The AND operator is commonly used with the WHERE, HAVING, and JOIN clauses to retrieve more precise results.
đ What is the AND Operator?
The AND operator evaluates multiple conditions together. A row is included in the result set only if every condition connected by AND evaluates to TRUE. If even one condition is false, the row is excluded.
Information
đ Basic Syntax
General Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1
AND condition2
AND condition3;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks | City |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | Chennai |
| 102 | Bob | Mathematics | 85 | Coimbatore |
| 103 | Charlie | Computer Science | 78 | Madurai |
| 104 | David | Computer Science | 95 | Chennai |
| 105 | Eva | Physics | 88 | Chennai |
đ¯ Using AND with Two Conditions
Retrieve students who belong to the Computer Science department and have marks greater than 90.
AND Example
SELECT *
FROM Students
WHERE Department = 'Computer Science'
AND Marks > 90;Only students who satisfy both conditions are returned.
đ Using AND with Multiple Conditions
The AND operator can connect more than two conditions.
Multiple AND Conditions
SELECT *
FROM Students
WHERE Department = 'Computer Science'
AND Marks >= 90
AND City = 'Chennai';This query returns only students who:
- â Belong to the Computer Science department.
- â Have marks greater than or equal to 90.
- â Live in Chennai.
âī¸ AND with Comparison Operators
The AND operator works with all SQL comparison operators.
| Operator | Example |
|---|---|
| = | Department = 'Physics' |
| > | Marks > 80 |
| < | Age < 25 |
| >= | Salary >= 50000 |
| <= | Price <= 1000 |
| <> | City <> 'Delhi' |
đ AND with BETWEEN
Combine AND with BETWEEN to apply additional filters.
BETWEEN with AND
SELECT *
FROM Students
WHERE Marks BETWEEN 80 AND 95
AND Department = 'Computer Science';đ AND with LIKE
You can combine pattern matching with other conditions.
LIKE with AND
SELECT *
FROM Students
WHERE Name LIKE 'A%'
AND Department = 'Computer Science';đ AND with ORDER BY
After filtering the data using AND, the results can be sorted using ORDER BY.
Filter and Sort
SELECT Name,
Marks
FROM Students
WHERE Department = 'Computer Science'
AND Marks >= 80
ORDER BY Marks DESC;đ§Ž AND with Aggregate Functions
The AND operator is also useful in the HAVING clause for filtering grouped data.
HAVING with AND
SELECT Department,
COUNT(*) AS TotalStudents,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
HAVING COUNT(*) > 1
AND AVG(Marks) >= 85;â ī¸ AND vs OR
| Feature | AND | OR |
|---|---|---|
| Condition Requirement | All conditions must be true. | At least one condition must be true. |
| Result Set | More restrictive. | Less restrictive. |
| Typical Usage | Apply multiple filters. | Match alternative conditions. |
đŧ Real-World Example
A university administrator wants to find students from the Computer Science department who scored at least 90 marks and live in Chennai.
Real-World Query
SELECT StudentID,
Name,
Marks,
City
FROM Students
WHERE Department = 'Computer Science'
AND Marks >= 90
AND City = 'Chennai'
ORDER BY Marks DESC;Only students meeting all three conditions appear in the result.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ AND combines two or more conditions.
- đ Every condition connected by AND must evaluate to TRUE.
- đ It is commonly used with WHERE and HAVING.
- đ It can be combined with operators such as BETWEEN, LIKE, and comparison operators.
- đ Use parentheses when mixing AND with OR to avoid ambiguity.