Friday, December 21, 2012

SQL Logical Operators

SQL Logical Operators
There are three Logical Operators namely, AND, OR, and NOT. These operators compare two conditions at a time to determine whether a row can be selected for the output. When retrieving data using a SELECT statement, you can use logical operators in the WHERE clause, which allows you to combine more than one condition.
Logical Operators
Description
OR
For the row to be selected at least one of the conditions must be true.
AND
For a row to be selected all the specified conditions must be true.
NOT
For a row to be selected the specified condition must be false.

"OR" Logical Operator:

If you want to select rows that satisfy at least one of the given conditions, you can use the logical operator, OR.
For example: if you want to find the names of students who are studying either Maths or Science, the query would be like,

SELECT first_name, last_name, subject 
FROM student_details 
WHERE subject = 'Maths' OR subject = 'Science' 

The output would be something like,
first_name
last_name
subject
-------------
-------------
----------
Anajali
Bhagwat
Maths
Shekar
Gowda
Maths
Rahul
Sharma
Science
Stephen
Fleming
Science
The following table describes how logical "OR" operator selects a row.

Column1 Satisfied?
Column2 Satisfied?
Row Selected
YES
YES
YES
YES
NO
YES
NO
YES
YES
NO
NO
NO

"AND" Logical Operator:

If you want to select rows that must satisfy all the given conditions, you can use the logical operator, AND.

For Example: To find the names of the students between the age 10 to 15 years, the query would be like:
SELECT first_name, last_name, age 
FROM student_details 
WHERE age >= 10 AND age <= 15;
The output would be something like,

first_name
last_name
age
-------------
-------------
------
Rahul
Sharma
10
Anajali
Bhagwat
12
Shekar
Gowda
15



The following table describes how logical "AND" operator selects a row.
Column1 Satisfied?
Column2 Satisfied?
Row Selected
YES
YES
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO

"NOT" Logical Operator:

If you want to find rows that do not satisfy a condition, you can use the logical operator, NOT. NOT results in the reverse of a condition. That is, if a condition is satisfied, then the row is not returned.

For example: If you want to find out the names of the students who do not play football, the query would be like:
SELECT first_name, last_name, games 
FROM student_details 
WHERE NOT games = 'Football' 
The output would be something like,
first_name
last_name
games
----------------
----------------
-----------
Rahul
Sharma
Cricket
Stephen
Fleming
Cricket
Shekar
Gowda
Badminton
Priya
Chandra
Chess
The following table describes how logical "NOT" operator selects a row.
Column1 Satisfied?
NOT Column1 Satisfied?
Row Selected
YES
NO
NO
NO
YES
YES
Nested Logical Operators:
You can use multiple logical operators in an SQL statement. When you combine the logical operators in a SELECT statement, the order in which the statement is processed is
1) NOT 
2) AND 
3) OR 
For example: If you want to select the names of the students who age is between 10 and 15 years, or those who do not play football, the
SELECT statement would be
SELECT first_name, last_name, age, games 
FROM student_details 
WHERE age >= 10 AND age <= 15 
OR NOT games = 'Football'

The output would be something like,
first_name
last_name
age
games
-------------
-------------
--------
------------
Rahul
Sharma
10
Cricket
Priya
Chandra
15
Chess
In this case, the filter works as follows:
Condition 1: All the students you do not play football are selected.
Condition 2: All the students whose are aged between 10 and 15 are selected.
Condition 3: Finally the result is, the rows which satisfy atleast one of the above conditions is returned.
NOTE:The order in which you phrase the condition is important, if the order changes you are likely to get a different result.

3 comments:

  1. Thanks for the very clear explanation about SQL logical operators.It’s very useful. I found one of the good resource related to SQL. It is useful for beginners as well as Advance learner's to learn about the various concepts of SQL.To know more: link:https://intellipaat.com/sql-training/

    ReplyDelete
  2. Thanks for sharing and very clear information.Really like this blog.Thanks for sharing.


    DML Training

    ReplyDelete
  3. "Hi
    This site is super helpful on the topic. I tried to come up with my strategy and write a post on a similar topic. Some of your points helped me to map my content in better shape. I would love to share it with you and it will be great if you visit and read this blog.
    You should visit on SQL Developer Certification site."

    ReplyDelete

Thank you :
- kareem