SlideShare a Scribd company logo
1 of 15
SQL Subquery
By raveena thakur
What is SQL Sub query?
A sub query is a SQL query nested
inside a larger query.
Where Sub query occurs?
A subquery may occurs in:
• A SELECT clause
• A WHERE clause
• The sub query can be nested inside a SELECT,
INSERT, UPDATE, or DELETE statement or inside
another sub query.
• A sub query is usually added within the WHERE clause
of another SQL SELECT statement.
• You can use the comparison operators, such as >, <, or
=. The comparison operator can also be a multiple-row
operator, such as IN, ANY, or ALL.
SUBQUERY SYNTAX
• The subquery (inner query) executes before the main
query(outer query).
• The result of the subquery is used by the main query.
TYPE OF SUBQUERIES
• Single row subquery : Returns zero or one
row.
• Multiple row subquery : Returns one or more
rows.
• Multiple column subqueries : Returns one or
more columns.
SINGLE ROW SUBQUERIES
• The single row subquery returns one row. The single row query uses
any operator in the query i.e. (=,<=, >=, <>, <, > ).
• EXAMPLE:
• department table
Employee table
• Suppose you want to find out the ename, job,sal of the
employees whose salaries are less than that of an employee
whose empno= 7876 from EMP table. Now you need to
perform two queries in order to get the desired result.
1. We will find out the salary of the employee whose
empno=7876. the query is as under:
2. Now in second query we will apply the condition from which
we will find the ename, job and sal. We will use the query as
under:
• The above two queries can be used as single query by
using the concept of subquery. It will combine the result
into a single query as under:
MULTIPLE ROW SUBQUERY
• Returns sets of rows. Query uses the set comparison
operators (IN, ALL, ANY). if u use a multirow subquery
with the equals comparison operators, the database will
return an error if more than one row is returned.
SYMBOL MEANING
IN Equal to any member in a list.
ANY Return rows that match any value on a list.
ALL Return rows that match all the values in a list.
IN Operator
• The IN operator retirns true if the comparison value is
contained in the list.
• The following statement finds the employee whose
salary is the same as the minimum salary of the
employees in the department.
ANY Operator
• The ANY operator return true if the comparison value
matches any of the values in the list.
• Display the employees whose salary is more than the
minimum salary of the employees in any department.
ALL Operator
• Returns true only if the comparison value matches all the
values in the list.
• Display the employee detail with salary less than those whose
job is ‘MANAGER’.
MULTIPLE COLUMN SUBQUERY
• A subquery that compares more than one column
between the parent query and subquery is called the
multiple column subqueries.
• List the employees that makes the same salary as other
employee with empno=7521 with the same job also.
Deleting Tables
• One can delete from a table by using DELETE FROM
statement. The DELETE statement removes rows from a table,
but it does not release storage space.
• SYNTAX:
DELETE FROM table_name;
• Example: To remove all rows in the student table, you just
execute the following query: DELETE FROM student;
Thank you

More Related Content

What's hot

SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 

What's hot (20)

Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Subqueries
SubqueriesSubqueries
Subqueries
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
Sql commands
Sql commandsSql commands
Sql commands
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 

Viewers also liked

Adjunto i planteamiento ético,político y estratégico de incide
Adjunto i planteamiento ético,político y estratégico de incideAdjunto i planteamiento ético,político y estratégico de incide
Adjunto i planteamiento ético,político y estratégico de incide
derechoshumanos2017
 

Viewers also liked (19)

Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Sub query_SQL
Sub query_SQLSub query_SQL
Sub query_SQL
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
dotnetMALAGA - Sql query tuning guidelines
dotnetMALAGA - Sql query tuning guidelinesdotnetMALAGA - Sql query tuning guidelines
dotnetMALAGA - Sql query tuning guidelines
 
Pivot Unpivot
Pivot UnpivotPivot Unpivot
Pivot Unpivot
 
Technical stream presentation
Technical stream presentationTechnical stream presentation
Technical stream presentation
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
 
SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholders
 
Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
 
Adjunto i planteamiento ético,político y estratégico de incide
Adjunto i planteamiento ético,político y estratégico de incideAdjunto i planteamiento ético,político y estratégico de incide
Adjunto i planteamiento ético,político y estratégico de incide
 
Revista
RevistaRevista
Revista
 
aplasia,metaplasia,displacia,neoplasia
 aplasia,metaplasia,displacia,neoplasia aplasia,metaplasia,displacia,neoplasia
aplasia,metaplasia,displacia,neoplasia
 
Expo innovacion-3°1
Expo innovacion-3°1Expo innovacion-3°1
Expo innovacion-3°1
 
Algoritmo
AlgoritmoAlgoritmo
Algoritmo
 
Declaración de la red animadora de mujeres indígena
Declaración de la red animadora de mujeres  indígenaDeclaración de la red animadora de mujeres  indígena
Declaración de la red animadora de mujeres indígena
 
Microsoft SQL Server PowerPivot
Microsoft SQL Server PowerPivotMicrosoft SQL Server PowerPivot
Microsoft SQL Server PowerPivot
 
Public service radio in india
Public service radio in indiaPublic service radio in india
Public service radio in india
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 

Similar to Sql subquery

Similar to Sql subquery (20)

Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
7. Using Sub Queries
7. Using Sub Queries7. Using Sub Queries
7. Using Sub Queries
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
 
Les06
Les06Les06
Les06
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
Les06
Les06Les06
Les06
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
SUBQUERIES.pptx
SUBQUERIES.pptxSUBQUERIES.pptx
SUBQUERIES.pptx
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Sub-Queries
Sub-QueriesSub-Queries
Sub-Queries
 
Nested Queries.pptx
Nested Queries.pptxNested Queries.pptx
Nested Queries.pptx
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)
 
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic FunctionsOOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 

Sql subquery

  • 2. What is SQL Sub query? A sub query is a SQL query nested inside a larger query.
  • 3. Where Sub query occurs? A subquery may occurs in: • A SELECT clause • A WHERE clause • The sub query can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another sub query. • A sub query is usually added within the WHERE clause of another SQL SELECT statement. • You can use the comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator, such as IN, ANY, or ALL.
  • 4. SUBQUERY SYNTAX • The subquery (inner query) executes before the main query(outer query). • The result of the subquery is used by the main query.
  • 5. TYPE OF SUBQUERIES • Single row subquery : Returns zero or one row. • Multiple row subquery : Returns one or more rows. • Multiple column subqueries : Returns one or more columns.
  • 6. SINGLE ROW SUBQUERIES • The single row subquery returns one row. The single row query uses any operator in the query i.e. (=,<=, >=, <>, <, > ). • EXAMPLE: • department table Employee table
  • 7. • Suppose you want to find out the ename, job,sal of the employees whose salaries are less than that of an employee whose empno= 7876 from EMP table. Now you need to perform two queries in order to get the desired result. 1. We will find out the salary of the employee whose empno=7876. the query is as under: 2. Now in second query we will apply the condition from which we will find the ename, job and sal. We will use the query as under:
  • 8. • The above two queries can be used as single query by using the concept of subquery. It will combine the result into a single query as under:
  • 9. MULTIPLE ROW SUBQUERY • Returns sets of rows. Query uses the set comparison operators (IN, ALL, ANY). if u use a multirow subquery with the equals comparison operators, the database will return an error if more than one row is returned. SYMBOL MEANING IN Equal to any member in a list. ANY Return rows that match any value on a list. ALL Return rows that match all the values in a list.
  • 10. IN Operator • The IN operator retirns true if the comparison value is contained in the list. • The following statement finds the employee whose salary is the same as the minimum salary of the employees in the department.
  • 11. ANY Operator • The ANY operator return true if the comparison value matches any of the values in the list. • Display the employees whose salary is more than the minimum salary of the employees in any department.
  • 12. ALL Operator • Returns true only if the comparison value matches all the values in the list. • Display the employee detail with salary less than those whose job is ‘MANAGER’.
  • 13. MULTIPLE COLUMN SUBQUERY • A subquery that compares more than one column between the parent query and subquery is called the multiple column subqueries. • List the employees that makes the same salary as other employee with empno=7521 with the same job also.
  • 14. Deleting Tables • One can delete from a table by using DELETE FROM statement. The DELETE statement removes rows from a table, but it does not release storage space. • SYNTAX: DELETE FROM table_name; • Example: To remove all rows in the student table, you just execute the following query: DELETE FROM student;