SQL AND and OR Operator Examples
Let’s look at some examples of AND and OR operators in SQL and understand their working.
Now, we consider a table database to demonstrate AND & OR operators with multiple cases.
SQL AND Operator Example
If suppose we want to fetch all the records from the Student table where Age is 18 and ADDRESS is Delhi.
Query
SELECT * FROM Student WHERE Age = 18 AND ADDRESS = 'Delhi';
Output
ROLL_NO | NAME | ADDRESS | PHONE | Age |
---|---|---|---|---|
1 | Ram | Delhi | XXXXXXXXXX | 18 |
4 | SURESH | Delhi | XXXXXXXXXX | 18 |
SQL OR Operator Example
To fetch all the records from the Student table where NAME is Ram or NAME is SUJIT.
Query
SELECT * FROM Student WHERE NAME = 'Ram' OR NAME = 'SUJIT';
Output
ROLL_NO | NAME | ADDRESS | PHONE | Age |
---|---|---|---|---|
1 | Ram | Delhi | XXXXXXXXXX | 18 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
AND and OR Operator in SQL
SQL AND and OR operators are used for data filtering and getting precise results based on conditions. They are used with the WHERE clause and are also called conjunctive operators.
AND and OR operators in SQL can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.