SlideShare a Scribd company logo
3
Most read
4
Most read
9
Most read
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 (20)

PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
PPT
Constraints In Sql
Anurag
 
PPTX
Triggers
Pooja Dixit
 
PPTX
SQL JOIN
Ritwik Das
 
PPT
Aggregate functions
sinhacp
 
PPTX
Introduction to SQL
Ehsan Hamzei
 
PPTX
SQL Join Basic
Naimul Arif
 
PPTX
introdution to SQL and SQL functions
farwa waqar
 
PDF
SQL Joins With Examples | Edureka
Edureka!
 
PPT
Types Of Join In Sql Server - Join With Example In Sql Server
programmings guru
 
PPTX
DATABASE CONSTRAINTS
sunanditaAnand
 
PDF
SQL Overview
Stewart Rogers
 
PPT
MYSQL.ppt
webhostingguy
 
PPTX
Aggregate Function - Database
Shahadat153031
 
PPTX
Sql queries presentation
NITISH KUMAR
 
PPTX
SQL Queries Information
Nishant Munjal
 
PPT
Introduction to sql
VARSHAKUMARI49
 
PPT
Sql ppt
Anuja Lad
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Constraints In Sql
Anurag
 
Triggers
Pooja Dixit
 
SQL JOIN
Ritwik Das
 
Aggregate functions
sinhacp
 
Introduction to SQL
Ehsan Hamzei
 
SQL Join Basic
Naimul Arif
 
introdution to SQL and SQL functions
farwa waqar
 
SQL Joins With Examples | Edureka
Edureka!
 
Types Of Join In Sql Server - Join With Example In Sql Server
programmings guru
 
DATABASE CONSTRAINTS
sunanditaAnand
 
SQL Overview
Stewart Rogers
 
MYSQL.ppt
webhostingguy
 
Aggregate Function - Database
Shahadat153031
 
Sql queries presentation
NITISH KUMAR
 
SQL Queries Information
Nishant Munjal
 
Introduction to sql
VARSHAKUMARI49
 
Sql ppt
Anuja Lad
 

Viewers also liked (20)

PDF
Nested Queries Lecture
Felipe Costa
 
PDF
Sub query_SQL
CoT
 
PDF
Sql subquery
Vivek Singh
 
PPT
Subqueries -Oracle DataBase
Salman Memon
 
PPTX
dotnetMALAGA - Sql query tuning guidelines
Javier García Magna
 
DOC
Pivot Unpivot
Görkem Sezgin
 
PPTX
Technical stream presentation
Dynistics
 
PPTX
Geek Sync | Rewriting Bad SQL Code 101
IDERA Software
 
PDF
SQL 101 for business experts and stakeholders
Iván Stepaniuk
 
PDF
Using T-SQL
Antonios Chatzipavlis
 
PDF
Adjunto i planteamiento ético,político y estratégico de incide
derechoshumanos2017
 
DOCX
Revista
Jose Sanchez
 
PPTX
aplasia,metaplasia,displacia,neoplasia
Diego Alvan Roblero Gonzalez
 
PPTX
Expo innovacion-3°1
Papito Kerido
 
PPTX
Algoritmo
Papito Kerido
 
PDF
Declaración de la red animadora de mujeres indígena
Asociación Civil Perijá
 
PDF
Microsoft SQL Server PowerPivot
Mark Ginnebaugh
 
PPTX
Public service radio in india
Dr. Anamika Ray Memorial Trust
 
DOCX
Complex queries in sql
Charan Reddy
 
Nested Queries Lecture
Felipe Costa
 
Sub query_SQL
CoT
 
Sql subquery
Vivek Singh
 
Subqueries -Oracle DataBase
Salman Memon
 
dotnetMALAGA - Sql query tuning guidelines
Javier García Magna
 
Pivot Unpivot
Görkem Sezgin
 
Technical stream presentation
Dynistics
 
Geek Sync | Rewriting Bad SQL Code 101
IDERA Software
 
SQL 101 for business experts and stakeholders
Iván Stepaniuk
 
Using T-SQL
Antonios Chatzipavlis
 
Adjunto i planteamiento ético,político y estratégico de incide
derechoshumanos2017
 
Revista
Jose Sanchez
 
aplasia,metaplasia,displacia,neoplasia
Diego Alvan Roblero Gonzalez
 
Expo innovacion-3°1
Papito Kerido
 
Algoritmo
Papito Kerido
 
Declaración de la red animadora de mujeres indígena
Asociación Civil Perijá
 
Microsoft SQL Server PowerPivot
Mark Ginnebaugh
 
Public service radio in india
Dr. Anamika Ray Memorial Trust
 
Complex queries in sql
Charan Reddy
 
Ad

Similar to Sql subquery (20)

PPTX
Lab5 sub query
Balqees Al.Mubarak
 
PPTX
Ch 6 Sub Query.pptx
RamishaRauf
 
PDF
SQL Subqueries - Oracle SQL Fundamentals
MuhammadWaheed44
 
PPTX
Data base management system full details
NitinYadav690862
 
PPT
e computer notes - Advanced subqueries
ecomputernotes
 
PPT
Les06- Subqueries.ppt
gznfrch1
 
PPTX
Week6_Theory.pptx
MDTOUKERAHMED
 
PPT
Les06
Sudharsan S
 
PPT
Les18
Vijay Kumar
 
PPT
SQL WORKSHOP::Lecture 6
Umair Amjad
 
PPT
Les06 (using subqueries to solve queries)
Achmad Solichin
 
PDF
7. Using Sub Queries
Evelyn Oluchukwu
 
PPT
Les06
Vijay Kumar
 
PPTX
Unit 3-p2relationaldatabasescodeing.pptx
artipunia12
 
PPT
Using subqueries to solve queries
Syed Zaid Irshad
 
PPT
7992267.ppt
ssuser767860
 
PPT
Les07.ppt 0125566655656959652323265656565
nourhandardeer3
 
PPT
Les06[1]Subqueries
siavosh kaviani
 
PPT
Les06
Sudharsan S
 
Lab5 sub query
Balqees Al.Mubarak
 
Ch 6 Sub Query.pptx
RamishaRauf
 
SQL Subqueries - Oracle SQL Fundamentals
MuhammadWaheed44
 
Data base management system full details
NitinYadav690862
 
e computer notes - Advanced subqueries
ecomputernotes
 
Les06- Subqueries.ppt
gznfrch1
 
Week6_Theory.pptx
MDTOUKERAHMED
 
SQL WORKSHOP::Lecture 6
Umair Amjad
 
Les06 (using subqueries to solve queries)
Achmad Solichin
 
7. Using Sub Queries
Evelyn Oluchukwu
 
Unit 3-p2relationaldatabasescodeing.pptx
artipunia12
 
Using subqueries to solve queries
Syed Zaid Irshad
 
7992267.ppt
ssuser767860
 
Les07.ppt 0125566655656959652323265656565
nourhandardeer3
 
Les06[1]Subqueries
siavosh kaviani
 
Ad

Recently uploaded (20)

PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
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
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Dimensions of Societal Planning in Commonism
StefanMz
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 

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;