SlideShare a Scribd company logo
8/10/13 SQL Logical Operators
beginner-sql-tutorial.com/sql-logical-operators.htm 1/4
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_namelast_namesubject
---------
8/10/13 SQL Logical Operators
beginner-sql-tutorial.com/sql-logical-operators.htm 2/4
------------- --------------
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_namelast_nameage
------------- -------------
----
--
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:
8/10/13 SQL Logical Operators
beginner-sql-tutorial.com/sql-logical-operators.htm 3/4
SELECT first_name, last_name, games
FROM student_details
WHERE NOT games = 'Football'
The output would be something like,
first_namelast_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_namelast_nameagegames
------------- -------------
----
----
--------
----
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.
8/10/13 SQL Logical Operators
beginner-sql-tutorial.com/sql-logical-operators.htm 4/4
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.

More Related Content

Similar to Sql logical operators (20)

PPTX
SQL Operators.pptx
ssuserb8d5cb
 
PPTX
TNS ppt.pptx
varshinireddy022
 
PPTX
SQL logical operators
Dr. C.V. Suresh Babu
 
PPT
Chapter-4.ppt
CindyCuesta
 
PPT
Les02
Vijay Kumar
 
PDF
SQL Operator.pdf
KalyankumarVenkat1
 
PPT
SQL WORKSHOP::Lecture 2
Umair Amjad
 
PPT
Sql2
Nargis Ehsan
 
PPT
Les02.ppt
gfhfghfghfgh1
 
PPTX
Les02.pptx
NishaTariq1
 
PDF
SQL Lesson 6 - Select.pdf
Madhusha15
 
PPT
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
PPTX
SQL Operators.pptx
RUBAB79
 
PPT
chap2 (3).ppt
eemantariq2
 
PPT
Restricting and sorting data
HuzaifaMushtaq3
 
PPT
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
PPTX
Sql operator
Pooja Dixit
 
PPT
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
PPTX
SQL Presentation.pptx
UzefPatil
 
PDF
Basics of SELECT Statement - Oracle SQL
MuhammadWaheed44
 
SQL Operators.pptx
ssuserb8d5cb
 
TNS ppt.pptx
varshinireddy022
 
SQL logical operators
Dr. C.V. Suresh Babu
 
Chapter-4.ppt
CindyCuesta
 
SQL Operator.pdf
KalyankumarVenkat1
 
SQL WORKSHOP::Lecture 2
Umair Amjad
 
Les02.ppt
gfhfghfghfgh1
 
Les02.pptx
NishaTariq1
 
SQL Lesson 6 - Select.pdf
Madhusha15
 
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
SQL Operators.pptx
RUBAB79
 
chap2 (3).ppt
eemantariq2
 
Restricting and sorting data
HuzaifaMushtaq3
 
Les02 Restricting and Sorting Data using SQL.ppt
DrZeeshanBhatti
 
Sql operator
Pooja Dixit
 
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
SQL Presentation.pptx
UzefPatil
 
Basics of SELECT Statement - Oracle SQL
MuhammadWaheed44
 

More from Vivek Singh (20)

PPS
C programming session 14
Vivek Singh
 
PPS
C programming session 13
Vivek Singh
 
PPS
C programming session 11
Vivek Singh
 
PPS
C programming session 10
Vivek Singh
 
PPS
C programming session 08
Vivek Singh
 
PPS
C programming session 07
Vivek Singh
 
PPS
C programming session 05
Vivek Singh
 
PPS
C programming session 04
Vivek Singh
 
PPS
C programming session 02
Vivek Singh
 
PPS
C programming session 01
Vivek Singh
 
PPS
C programming session 16
Vivek Singh
 
PDF
Niit aptitude question paper
Vivek Singh
 
PDF
Excel shortcut and tips
Vivek Singh
 
PDF
Sql where clause
Vivek Singh
 
PDF
Sql update statement
Vivek Singh
 
PDF
Sql tutorial, tutorials sql
Vivek Singh
 
PDF
Sql subquery
Vivek Singh
 
PDF
Sql select statement
Vivek Singh
 
PDF
Sql rename
Vivek Singh
 
PDF
Sql query tuning or query optimization
Vivek Singh
 
C programming session 14
Vivek Singh
 
C programming session 13
Vivek Singh
 
C programming session 11
Vivek Singh
 
C programming session 10
Vivek Singh
 
C programming session 08
Vivek Singh
 
C programming session 07
Vivek Singh
 
C programming session 05
Vivek Singh
 
C programming session 04
Vivek Singh
 
C programming session 02
Vivek Singh
 
C programming session 01
Vivek Singh
 
C programming session 16
Vivek Singh
 
Niit aptitude question paper
Vivek Singh
 
Excel shortcut and tips
Vivek Singh
 
Sql where clause
Vivek Singh
 
Sql update statement
Vivek Singh
 
Sql tutorial, tutorials sql
Vivek Singh
 
Sql subquery
Vivek Singh
 
Sql select statement
Vivek Singh
 
Sql rename
Vivek Singh
 
Sql query tuning or query optimization
Vivek Singh
 
Ad

Recently uploaded (20)

PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Dimensions of Societal Planning in Commonism
StefanMz
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Ad

Sql logical operators

  • 1. 8/10/13 SQL Logical Operators beginner-sql-tutorial.com/sql-logical-operators.htm 1/4 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_namelast_namesubject ---------
  • 2. 8/10/13 SQL Logical Operators beginner-sql-tutorial.com/sql-logical-operators.htm 2/4 ------------- -------------- 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_namelast_nameage ------------- ------------- ---- -- 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:
  • 3. 8/10/13 SQL Logical Operators beginner-sql-tutorial.com/sql-logical-operators.htm 3/4 SELECT first_name, last_name, games FROM student_details WHERE NOT games = 'Football' The output would be something like, first_namelast_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_namelast_nameagegames ------------- ------------- ---- ---- -------- ---- 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.
  • 4. 8/10/13 SQL Logical Operators beginner-sql-tutorial.com/sql-logical-operators.htm 4/4 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.