SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
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.

Contenu connexe

Similaire à Sql logical operators

SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptxRUBAB79
 
Sql select statement
Sql select statementSql select statement
Sql select statementVivek Singh
 
SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptxssuserb8d5cb
 
1- Which of the following is an invalid SQL keyword- For each employ.docx
1- Which of the following is an invalid SQL keyword-   For each employ.docx1- Which of the following is an invalid SQL keyword-   For each employ.docx
1- Which of the following is an invalid SQL keyword- For each employ.docxEvandWyBurgesss
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankpptnewrforce
 
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL MuhammadWaheed44
 
SQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSSQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSGagandeep Nanda
 
5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people know5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people knowBoutros CHALOUHY
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clauseVivek Singh
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQLRaajendra M
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQLMSB Academy
 
Database systems administration traning 04
Database systems administration traning  04Database systems administration traning  04
Database systems administration traning 04Shahid Riaz
 
Dbms presentationnnnn
Dbms presentationnnnnDbms presentationnnnn
Dbms presentationnnnnsumi haque
 
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....Racharla Rohit Varma
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfMadhusha15
 

Similaire à Sql logical operators (20)

SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptx
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptx
 
1- Which of the following is an invalid SQL keyword- For each employ.docx
1- Which of the following is an invalid SQL keyword-   For each employ.docx1- Which of the following is an invalid SQL keyword-   For each employ.docx
1- Which of the following is an invalid SQL keyword- For each employ.docx
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Les02
Les02Les02
Les02
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
Sql2
Sql2Sql2
Sql2
 
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL
 
SQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSSQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTS
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people know5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people know
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clause
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
Database systems administration traning 04
Database systems administration traning  04Database systems administration traning  04
Database systems administration traning 04
 
Dbms presentationnnnn
Dbms presentationnnnnDbms presentationnnnn
Dbms presentationnnnn
 
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 

Plus de Vivek Singh

C programming session 14
C programming session 14C programming session 14
C programming session 14Vivek Singh
 
C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
 
C programming session 07
C programming session 07C programming session 07
C programming session 07Vivek Singh
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Vivek Singh
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operatorsVivek Singh
 

Plus de Vivek Singh (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 13
C programming session 13C programming session 13
C programming session 13
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql rename
Sql renameSql rename
Sql rename
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operators
 

Dernier

The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptxmary850239
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfNOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfSumit Tiwari
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxDr. Santhosh Kumar. N
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxKatherine Villaluna
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17Celine George
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphNetziValdelomar1
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsEugene Lysak
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
Latin American Revolutions, c. 1789-1830
Latin American Revolutions, c. 1789-1830Latin American Revolutions, c. 1789-1830
Latin American Revolutions, c. 1789-1830Dave Phillips
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxKatherine Villaluna
 

Dernier (20)

The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
3.19.24 Urban Uprisings and the Chicago Freedom Movement.pptx
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdfNOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
NOTES OF DRUGS ACTING ON NERVOUS SYSTEM .pdf
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptx
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
Presentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a ParagraphPresentation on the Basics of Writing. Writing a Paragraph
Presentation on the Basics of Writing. Writing a Paragraph
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George Wells
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
Latin American Revolutions, c. 1789-1830
Latin American Revolutions, c. 1789-1830Latin American Revolutions, c. 1789-1830
Latin American Revolutions, c. 1789-1830
 
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptxPractical Research 1: Lesson 8 Writing the Thesis Statement.pptx
Practical Research 1: Lesson 8 Writing the Thesis Statement.pptx
 

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.