SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

SQL Commands
SQL Commands SQL Commands
SQL Commands
Sachidananda M H
 
Sql commands
Sql commandsSql commands
Sql commands
Pooja Dixit
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
Dhani Ahmad
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
Sql ppt
Sql pptSql ppt
Sql ppt
Anuja Lad
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
I L0V3 CODING DR
 
Group By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQLGroup By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQL
MSB Academy
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
Swapnali Pawar
 
Sql views
Sql viewsSql views
Sql views
arshid045
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
sunanditaAnand
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base Creating and Managing Tables -Oracle Data base
Creating and Managing Tables -Oracle Data base
Salman Memon
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
Nilt1234
 
SQL commands
SQL commandsSQL commands
SQL commands
GirdharRatne
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
rehaniltifat
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
Shrija Madhu
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
sinhacp
 
SQL Views
SQL ViewsSQL Views
SQL Views
Aaron Buma
 

Viewers also liked (20)

Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
Felipe Costa
 
Subqueries
SubqueriesSubqueries
Subqueries
Randy Riness @ South Puget Sound Community College
 
Sub query_SQL
Sub query_SQLSub query_SQL
Sub query_SQL
CoT
 
Sql subquery
Sql subquerySql subquery
Sql subquery
Vivek Singh
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
Salman Memon
 
dotnetMALAGA - Sql query tuning guidelines
dotnetMALAGA - Sql query tuning guidelinesdotnetMALAGA - Sql query tuning guidelines
dotnetMALAGA - Sql query tuning guidelines
Javier García Magna
 
Technical stream presentation
Technical stream presentationTechnical stream presentation
Technical stream presentation
Dynistics
 
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
IDERA Software
 
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
Iván Stepaniuk
 
Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
Antonios Chatzipavlis
 
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
 
Revista
RevistaRevista
Revista
Jose Sanchez
 
aplasia,metaplasia,displacia,neoplasia
 aplasia,metaplasia,displacia,neoplasia aplasia,metaplasia,displacia,neoplasia
aplasia,metaplasia,displacia,neoplasia
Diego Alvan Roblero Gonzalez
 
Expo innovacion-3°1
Expo innovacion-3°1Expo innovacion-3°1
Expo innovacion-3°1
Papito Kerido
 
Algoritmo
AlgoritmoAlgoritmo
Algoritmo
Papito Kerido
 
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
Asociación Civil Perijá
 
Microsoft SQL Server PowerPivot
Microsoft SQL Server PowerPivotMicrosoft SQL Server PowerPivot
Microsoft SQL Server PowerPivot
Mark Ginnebaugh
 
Public service radio in india
Public service radio in indiaPublic service radio in india
Public service radio in india
Dr. Anamika Ray Memorial Trust
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
Charan Reddy
 
Ad

Similar to Sql subquery (20)

Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
Syed Zaid Irshad
 
Data base management system full details
Data base management system full detailsData base management system full details
Data base management system full details
NitinYadav690862
 
7. Using Sub Queries
7. Using Sub Queries7. Using Sub Queries
7. Using Sub Queries
Evelyn Oluchukwu
 
More Complex SQL and Concurrency ControlModule 4.pptx
More Complex SQL and Concurrency ControlModule 4.pptxMore Complex SQL and Concurrency ControlModule 4.pptx
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
RamishaRauf
 
Les06
Les06Les06
Les06
Vijay Kumar
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
Gurpreet singh
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai1
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
Balqees Al.Mubarak
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
Satya P. Joshi
 
Les06
Les06Les06
Les06
Sudharsan S
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
Achmad Solichin
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai
 
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
baabtra.com - No. 1 supplier of quality freshers
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
ASHABOOPATHY
 
Sub-Queries
Sub-QueriesSub-Queries
Sub-Queries
S.M. Towhidul Islam
 
Nested Queries.pptx
Nested Queries.pptxNested Queries.pptx
Nested Queries.pptx
pavankumarjakkepalli
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 
U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)
Michael Rys
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
Syed Zaid Irshad
 
Data base management system full details
Data base management system full detailsData base management system full details
Data base management system full details
NitinYadav690862
 
More Complex SQL and Concurrency ControlModule 4.pptx
More Complex SQL and Concurrency ControlModule 4.pptxMore Complex SQL and Concurrency ControlModule 4.pptx
More Complex SQL and Concurrency ControlModule 4.pptx
bgscseise
 
Ch 6 Sub Query.pptx
Ch 6 Sub Query.pptxCh 6 Sub Query.pptx
Ch 6 Sub Query.pptx
RamishaRauf
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai1
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
Satya P. Joshi
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
Achmad Solichin
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
SherinRappai
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 
U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)U-SQL Does SQL (SQLBits 2016)
U-SQL Does SQL (SQLBits 2016)
Michael Rys
 
Ad

Recently uploaded (20)

How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 

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;
  翻译: