SQL AND & OR Operators

SQL AND operator is used to filter the records by combining two boolean expressions and returns true when both boolean expressions are true whereas SQL OR operator returns true value when either the first condition or the second condition is true or when both conditions are true.

Here we are going to use STUDENT table for demonstration purpose.

StudentTable

SQL AND Operator

As you can see in below query, AND operator evaluates FirstName and Country columns and when both conditions are met then it returns the row.

SELECT *FROM Student
WHERE FirstName = 'Gail' AND Country = 'Germany'

AND operator

We will consider one more example where we will pass one false value and will see the result. As you can see there is no record available to satisfy below condition hence it returns no value.

AND operator2

SQL OR Operator

As you can see from below example, we are using OR operator and evaluating the same conditions which we used in AND operator. Here OR operator returns all records where conditions for either column values FirstName OR Country are satisfied.

SELECT *FROM Student
WHERE FirstName = 'Gail' OR Country = 'Germany'

OR operator

Combine AND & OR Operator

You can also combine both AND & OR operators to evaluate the conditions. When more than one logical operators are used in a query then AND operator is evaluated before OR operator.

SELECT *FROM Student
WHERE FirstName = 'Roberto' AND (RollNumber = 1005 OR Country = 'India')

AND OR

Click below links to watch live practical.

Reference: Manzoor Siddiqui [www.SQLServerLog.com]

You may also like...

Leave a Reply

Your email address will not be published.