SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
Advanced Subqueries
http://ecomputernotes.com
Objectives
After completing this lesson, you should be able
to do the following:
" Write a multiple-column subquery
" Describe and explain the behavior of subqueries when
null values are retrieved
" Write a subquery in a FROM clause
" Use scalar subqueries in SQL
" Describe the types of problems that can be solved with
correlated subqueries
" Write correlated subqueries
" Update and delete rows using correlated subqueries
" Use the EXISTS and NOT EXISTS operators
" Use the WITH clause
http://ecomputernotes.com
What Is a Subquery?
A subquery is a SELECT statement embedded in a
clause of another SQL statement.
Main SELECT ...
query FROM ...
WHERE ...
(SELECT ... Subquery
FROM ...
WHERE ...)
http://ecomputernotes.com
Subqueries
SELECTselect_list
FRO M table
WHERE expr operator (SELECT select_list
FRO M table);
"The subquery (inner query) executes once before
the main query.
"The result of the subquery is used by the main
query (outer query).
http://ecomputernotes.com
Using a Subquery
SELECT last_name
10500FROM employees
WHERE salary >
(SELECT salary
FROM employees
WHERE employee_id = 149) ;
http://ecomputernotes.com
Multiple-Column Subqueries
Main query
WHERE (MANAGER_ID, DEPARTMENT_ID) IN
Subquery
100 90
102 60
124 50
Each row of the main query is compared to
values from avalues from a multiple-row and multiplerow
column
subquery.
http://ecomputernotes.com
Column Comparisons
Column comparisons in a multiple-column subquery
can be:
" Pairwise comparisons
" Nonpairwise comparisons
http://ecomputernotes.com
Pairwise Comparison Subquery
Display the details of the employees who are managed by
the same manager and work in the same department as the
employees with EMPLOYEE_ID 178 or 174.
LECT employee_id, manager_id, department_id
O M employees
HERE (manager_id, department_id) IN
(SELECT manager_id, department_id
FROM employees
WHERE employee_id IN (178,174))
ND employee_id NOT IN (178,174);
http://ecomputernotes.com
Nonpairwise Comparison Subquery
isplay the details of the employees who are managed by he
same manager as the employees with EMPLOYEE_ID 74 or
141 and work in the same department as the mployees with
EMPLOYEE_ID 174 or 141.
SELECT employee_id, manager_id, department_id
FROM employees
WHERE manager_id IN
(SELECT manager_id
FROM employees
WHERE employee_id IN (174,141))
AND department_id IN
(SELECT department_id
FROM employees
WHERE employee_id IN (174,141))
AND employee_id NOT IN(174,141);
http://ecomputernotes.com
Using a Subquery
in the FROM Clause
SELECT a.last_name, a.salary,
a.department_id, b.salavg
FROM employees a, (SELECT department_id,
AVG(salary) salavg
FROM employees
GROUP BY department_id) b
WHERE a.department_id = b.department_id
AND a.salary > b.salavg;
http://ecomputernotes.com
Scalar Subquery Expressions
"A scalar subquery expression is a subquery that
returns exactly one column value from one row. "Scalar
subqueries were supported in Oracle8i only in a
limited set of cases, For example:
± SELECT statement (FROM and WHERE clauses) ±
VALUES list of an INSERT statement "In Oracle9i, scalar
subqueries can be used in:
Condition and expression part of DECODE and CASE All
clauses of SELECT except GROUP BY
http://ecomputernotes.com
Scalar Subqueries: Examples
calar Subqueries in CASE Expressions
ELECT employee_id, last_name,
(CASE 20
WHEN department_id =
(SELECT department_id FROM departments
WHERE location_id = 1800)
THEN 'Canada' ELSE 'USA' END) location
ROM employees;
Scalar Subqueries in ORDER BY Clause
ELECT employee_id, last_name
ROM employees e
RDER BY (SELECT department_name
FROM departments d
WHERE e.department_id = d.department_id);
http://ecomputernotes.com
Correlated Subqueries
Correlated subqueries are used for row-by-row
processing. Each subquery is executed once for
every row of the outer query.
GET
candidate row from outer query
EXECUTE
inner query using candidate row value
USE
values from inner query to qualify or
disqualify candidate row
http://ecomputernotes.com
Correlated Subqueries
SELECT column1, column2, ...
FROM table1 outer
WHERE column1 operator
(SELECT colum1, column2
FROM table2
WHERE expr1 =
outer .expr2);
The subquery references a column from a table in he
parentthe parent query.the parent query.
http://ecomputernotes.com
Using Correlated Subqueries
Find all employees who earn more than the average
salary in their department.
SELECT last_name, salary, department_id
FROM employees outer
WHERE salary >
(SELECT AVG(salary)
FROM employees
WHERE department_id =
outer.department_id) ;
http://ecomputernotes.com
Using Correlated Subqueries
isplay details of those employees who have switched
obs at least twice.
SELECT e.employee_id, last_name,e.job_id
FROM employees e
WHERE 2 <= (SELECT COUNT(*)
FROM job_history
WHERE employee_id = e.employee_id);
http://ecomputernotes.com
Using the EXISTS Operator
"The EXISTS operator tests for existence of rows in the
results set of the subquery.
"If a subquery row value is found:
The search does not continue in the inner query
The condition is flagged TRUE
"If a subquery row value is not found:
The condition is flagged FALSE The
search continues in the inner query
http://ecomputernotes.com
Using the EXISTS Operator
Find employees who have at least one person
reporting to them.
SELECT employee_id, last_name, job_id, department_id
FROM employees outer
WHERE EXISTS ( SELECT 'X'
FROM employees
WHERE manager_id =
outer.employee_id);
http://ecomputernotes.com
Using the NOT EXISTS Operator
ind all departments that do not have any employees.
SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (SELECT 'X'
FROM employees
WHERE department_id
= d.department_id);
http://ecomputernotes.com
Correlated UPDATE
UPDATE table1 alias1
SET column = (SELECT expression
FROM table2 alias2
WHERE alias1.column =
alias2.column );
se a correlated subquery to update rows in one
able based on rows from another table.
http://ecomputernotes.com
Correlated UPDATE
" Denormalize the EMPLOYEES table by adding a
column to store the department name.
" Populate the table by using a correlated
update.
ALTER TABLE employees
ADD(department_name VARCHAR2(14));
UPDATE employees e
SET department_name =
(SELECT department_name
FROM departments d
WHERE e.department_id = d.department_id);
http://ecomputernotes.com
Correlated DELETE
DELETE FROM table1 alias1 WHERE
column operator (SELECT expression
FROM table2 alias2
WHERE alias1.column = alias2.column);
se a correlated subquery to delete rows in one table
ased on rows from another table.
http://ecomputernotes.com
Correlated DELETE
se a correlated subquery to delete only those rows
rom the EMPLOYEES table that also exist in the
MP_HISTORY table.
DELETE FROM employees E WHERE
employee_id = (SELECT employee_id
FROM emp_history
WHERE employee_id = E.employee_id);
http://ecomputernotes.com
The WITH Clause
" Using the WITH clause, you can use the same
query block in a SELECT statement when it occurs
more than once within a complex query.
" The WITH clause retrieves the results of a query
block and stores it in the user's temporary
tablespace.
" The WITH clause improves performance
http://ecomputernotes.com
WITH Clause: Example
Using the WITH clause, write a query to display the
department name and total salaries for those
departments whose total salary is greater than the
average salary across departments.
http://ecomputernotes.com
WITH Clause: Example
WITH
dept_costs AS (
SELECT d.department_name, SUM(e.salary) AS dept_total
FRO M employees e, departments d
WHERE e.department_id = d.department_id
GROUP BY d.department_name),
avg_cost AS (
SELECT SUM(dept_total)/COUNT(*) AS dept_avg
FRO M dept_costs)
SELECT *
FROM dept_costs WHERE
dept_total > (SELECT dept_avg
FRO M avg_cost )
ORDER BY department_name;
http://ecomputernotes.com

Weitere ähnliche Inhalte

Was ist angesagt?

e computer notes - Using set operator
e computer notes - Using set operatore computer notes - Using set operator
e computer notes - Using set operatorecomputernotes
 
Les07 (using the set operators)
Les07 (using the set operators)Les07 (using the set operators)
Les07 (using the set operators)Achmad Solichin
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queriesSyed Zaid Irshad
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库renguzi
 
Mocks Enabling Test-Driven Design
Mocks Enabling Test-Driven DesignMocks Enabling Test-Driven Design
Mocks Enabling Test-Driven DesignAlexandre Martins
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating dataecomputernotes
 
e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting dataecomputernotes
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functionsecomputernotes
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementsecomputernotes
 
Sql where clause
Sql where clauseSql where clause
Sql where clauseVivek Singh
 

Was ist angesagt? (16)

e computer notes - Using set operator
e computer notes - Using set operatore computer notes - Using set operator
e computer notes - Using set operator
 
Les07 (using the set operators)
Les07 (using the set operators)Les07 (using the set operators)
Les07 (using the set operators)
 
1 z0 047
1 z0 0471 z0 047
1 z0 047
 
Les07
Les07Les07
Les07
 
Dump Answers
Dump AnswersDump Answers
Dump Answers
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
1 z0 001
1 z0 0011 z0 001
1 z0 001
 
Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库Oracle OCP 1Z0-007题库
Oracle OCP 1Z0-007题库
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
Mocks Enabling Test-Driven Design
Mocks Enabling Test-Driven DesignMocks Enabling Test-Driven Design
Mocks Enabling Test-Driven Design
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
 
e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting data
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functions
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 

Ähnlich wie e computer notes - Advanced subqueries

Ähnlich wie e computer notes - Advanced subqueries (20)

e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Les06
Les06Les06
Les06
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Les06
Les06Les06
Les06
 
App C
App CApp C
App C
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
 
Les05
Les05Les05
Les05
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 
sql language
sql languagesql language
sql language
 
Module03
Module03Module03
Module03
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle Database
 
Les08
Les08Les08
Les08
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Query
QueryQuery
Query
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
 

Mehr von ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 

Mehr von ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 

Kürzlich hochgeladen

Kempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdfKempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdfHenry Tapper
 
The Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance LeaderThe Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance LeaderArianna Varetto
 
Liquidity Decisions in Financial management
Liquidity Decisions in Financial managementLiquidity Decisions in Financial management
Liquidity Decisions in Financial managementshrutisingh143670
 
Global Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride ConsultingGlobal Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride Consultingswastiknandyofficial
 
Aon-UK-DC-Pension-Tracker-Q1-2024. slideshare
Aon-UK-DC-Pension-Tracker-Q1-2024. slideshareAon-UK-DC-Pension-Tracker-Q1-2024. slideshare
Aon-UK-DC-Pension-Tracker-Q1-2024. slideshareHenry Tapper
 
Building pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the futureBuilding pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the futureResolutionFoundation
 
Hello this ppt is about seminar final project
Hello this ppt is about seminar final projectHello this ppt is about seminar final project
Hello this ppt is about seminar final projectninnasirsi
 
2B Nation-State.pptx contemporary world nation
2B  Nation-State.pptx contemporary world nation2B  Nation-State.pptx contemporary world nation
2B Nation-State.pptx contemporary world nationko9240888
 
Thoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension FundThoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension FundAshwinJey
 
Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward
 
Gender and caste discrimination in india
Gender and caste discrimination in indiaGender and caste discrimination in india
Gender and caste discrimination in indiavandanasingh01072003
 
Introduction to Health Economics Dr. R. Kurinji Malar.pptx
Introduction to Health Economics Dr. R. Kurinji Malar.pptxIntroduction to Health Economics Dr. R. Kurinji Malar.pptx
Introduction to Health Economics Dr. R. Kurinji Malar.pptxDrRkurinjiMalarkurin
 
10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdfglobusfinanza
 
Banking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxBanking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxANTHONYAKINYOSOYE1
 
2024-04-09 - Pension Playpen roundtable - slides.pptx
2024-04-09 - Pension Playpen roundtable - slides.pptx2024-04-09 - Pension Playpen roundtable - slides.pptx
2024-04-09 - Pension Playpen roundtable - slides.pptxHenry Tapper
 
What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024prajwalgopocket
 
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptxOAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptxhiddenlevers
 
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance VerificationCrypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance VerificationAny kyc Account
 
ekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdfekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdfSteliosTheodorou4
 
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτοςΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτοςNewsroom8
 

Kürzlich hochgeladen (20)

Kempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdfKempen ' UK DB Endgame Paper Apr 24 final3.pdf
Kempen ' UK DB Endgame Paper Apr 24 final3.pdf
 
The Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance LeaderThe Inspirational Story of Julio Herrera Velutini - Global Finance Leader
The Inspirational Story of Julio Herrera Velutini - Global Finance Leader
 
Liquidity Decisions in Financial management
Liquidity Decisions in Financial managementLiquidity Decisions in Financial management
Liquidity Decisions in Financial management
 
Global Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride ConsultingGlobal Economic Outlook, 2024 - Scholaride Consulting
Global Economic Outlook, 2024 - Scholaride Consulting
 
Aon-UK-DC-Pension-Tracker-Q1-2024. slideshare
Aon-UK-DC-Pension-Tracker-Q1-2024. slideshareAon-UK-DC-Pension-Tracker-Q1-2024. slideshare
Aon-UK-DC-Pension-Tracker-Q1-2024. slideshare
 
Building pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the futureBuilding pressure? Rising rents, and what to expect in the future
Building pressure? Rising rents, and what to expect in the future
 
Hello this ppt is about seminar final project
Hello this ppt is about seminar final projectHello this ppt is about seminar final project
Hello this ppt is about seminar final project
 
2B Nation-State.pptx contemporary world nation
2B  Nation-State.pptx contemporary world nation2B  Nation-State.pptx contemporary world nation
2B Nation-State.pptx contemporary world nation
 
Thoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension FundThoma Bravo Equity - Presentation Pension Fund
Thoma Bravo Equity - Presentation Pension Fund
 
Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024Money Forward Integrated Report “Forward Map” 2024
Money Forward Integrated Report “Forward Map” 2024
 
Gender and caste discrimination in india
Gender and caste discrimination in indiaGender and caste discrimination in india
Gender and caste discrimination in india
 
Introduction to Health Economics Dr. R. Kurinji Malar.pptx
Introduction to Health Economics Dr. R. Kurinji Malar.pptxIntroduction to Health Economics Dr. R. Kurinji Malar.pptx
Introduction to Health Economics Dr. R. Kurinji Malar.pptx
 
10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf10 QuickBooks Tips 2024 - Globus Finanza.pdf
10 QuickBooks Tips 2024 - Globus Finanza.pdf
 
Banking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptxBanking: Commercial and Central Banking.pptx
Banking: Commercial and Central Banking.pptx
 
2024-04-09 - Pension Playpen roundtable - slides.pptx
2024-04-09 - Pension Playpen roundtable - slides.pptx2024-04-09 - Pension Playpen roundtable - slides.pptx
2024-04-09 - Pension Playpen roundtable - slides.pptx
 
What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024What is sip and What are its Benefits in 2024
What is sip and What are its Benefits in 2024
 
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptxOAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
OAT_RI_Ep18 WeighingTheRisks_Mar24_GlobalCredit.pptx
 
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance VerificationCrypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
Crypto Confidence Unlocked: AnyKYCaccount's Shortcut to Binance Verification
 
ekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdfekthesi-trapeza-tis-ellados-gia-2023.pdf
ekthesi-trapeza-tis-ellados-gia-2023.pdf
 
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτοςΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
ΤτΕ: Ανάπτυξη 2,3% και πληθωρισμός 2,8% φέτος
 

e computer notes - Advanced subqueries

  • 2. Objectives After completing this lesson, you should be able to do the following: " Write a multiple-column subquery " Describe and explain the behavior of subqueries when null values are retrieved " Write a subquery in a FROM clause " Use scalar subqueries in SQL " Describe the types of problems that can be solved with correlated subqueries " Write correlated subqueries " Update and delete rows using correlated subqueries " Use the EXISTS and NOT EXISTS operators " Use the WITH clause http://ecomputernotes.com
  • 3. What Is a Subquery? A subquery is a SELECT statement embedded in a clause of another SQL statement. Main SELECT ... query FROM ... WHERE ... (SELECT ... Subquery FROM ... WHERE ...) http://ecomputernotes.com
  • 4. Subqueries SELECTselect_list FRO M table WHERE expr operator (SELECT select_list FRO M table); "The subquery (inner query) executes once before the main query. "The result of the subquery is used by the main query (outer query). http://ecomputernotes.com
  • 5. Using a Subquery SELECT last_name 10500FROM employees WHERE salary > (SELECT salary FROM employees WHERE employee_id = 149) ; http://ecomputernotes.com
  • 6. Multiple-Column Subqueries Main query WHERE (MANAGER_ID, DEPARTMENT_ID) IN Subquery 100 90 102 60 124 50 Each row of the main query is compared to values from avalues from a multiple-row and multiplerow column subquery. http://ecomputernotes.com
  • 7. Column Comparisons Column comparisons in a multiple-column subquery can be: " Pairwise comparisons " Nonpairwise comparisons http://ecomputernotes.com
  • 8. Pairwise Comparison Subquery Display the details of the employees who are managed by the same manager and work in the same department as the employees with EMPLOYEE_ID 178 or 174. LECT employee_id, manager_id, department_id O M employees HERE (manager_id, department_id) IN (SELECT manager_id, department_id FROM employees WHERE employee_id IN (178,174)) ND employee_id NOT IN (178,174); http://ecomputernotes.com
  • 9. Nonpairwise Comparison Subquery isplay the details of the employees who are managed by he same manager as the employees with EMPLOYEE_ID 74 or 141 and work in the same department as the mployees with EMPLOYEE_ID 174 or 141. SELECT employee_id, manager_id, department_id FROM employees WHERE manager_id IN (SELECT manager_id FROM employees WHERE employee_id IN (174,141)) AND department_id IN (SELECT department_id FROM employees WHERE employee_id IN (174,141)) AND employee_id NOT IN(174,141); http://ecomputernotes.com
  • 10. Using a Subquery in the FROM Clause SELECT a.last_name, a.salary, a.department_id, b.salavg FROM employees a, (SELECT department_id, AVG(salary) salavg FROM employees GROUP BY department_id) b WHERE a.department_id = b.department_id AND a.salary > b.salavg; http://ecomputernotes.com
  • 11. Scalar Subquery Expressions "A scalar subquery expression is a subquery that returns exactly one column value from one row. "Scalar subqueries were supported in Oracle8i only in a limited set of cases, For example: ± SELECT statement (FROM and WHERE clauses) ± VALUES list of an INSERT statement "In Oracle9i, scalar subqueries can be used in: Condition and expression part of DECODE and CASE All clauses of SELECT except GROUP BY http://ecomputernotes.com
  • 12. Scalar Subqueries: Examples calar Subqueries in CASE Expressions ELECT employee_id, last_name, (CASE 20 WHEN department_id = (SELECT department_id FROM departments WHERE location_id = 1800) THEN 'Canada' ELSE 'USA' END) location ROM employees; Scalar Subqueries in ORDER BY Clause ELECT employee_id, last_name ROM employees e RDER BY (SELECT department_name FROM departments d WHERE e.department_id = d.department_id); http://ecomputernotes.com
  • 13. Correlated Subqueries Correlated subqueries are used for row-by-row processing. Each subquery is executed once for every row of the outer query. GET candidate row from outer query EXECUTE inner query using candidate row value USE values from inner query to qualify or disqualify candidate row http://ecomputernotes.com
  • 14. Correlated Subqueries SELECT column1, column2, ... FROM table1 outer WHERE column1 operator (SELECT colum1, column2 FROM table2 WHERE expr1 = outer .expr2); The subquery references a column from a table in he parentthe parent query.the parent query. http://ecomputernotes.com
  • 15. Using Correlated Subqueries Find all employees who earn more than the average salary in their department. SELECT last_name, salary, department_id FROM employees outer WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = outer.department_id) ; http://ecomputernotes.com
  • 16. Using Correlated Subqueries isplay details of those employees who have switched obs at least twice. SELECT e.employee_id, last_name,e.job_id FROM employees e WHERE 2 <= (SELECT COUNT(*) FROM job_history WHERE employee_id = e.employee_id); http://ecomputernotes.com
  • 17. Using the EXISTS Operator "The EXISTS operator tests for existence of rows in the results set of the subquery. "If a subquery row value is found: The search does not continue in the inner query The condition is flagged TRUE "If a subquery row value is not found: The condition is flagged FALSE The search continues in the inner query http://ecomputernotes.com
  • 18. Using the EXISTS Operator Find employees who have at least one person reporting to them. SELECT employee_id, last_name, job_id, department_id FROM employees outer WHERE EXISTS ( SELECT 'X' FROM employees WHERE manager_id = outer.employee_id); http://ecomputernotes.com
  • 19. Using the NOT EXISTS Operator ind all departments that do not have any employees. SELECT department_id, department_name FROM departments d WHERE NOT EXISTS (SELECT 'X' FROM employees WHERE department_id = d.department_id); http://ecomputernotes.com
  • 20. Correlated UPDATE UPDATE table1 alias1 SET column = (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column ); se a correlated subquery to update rows in one able based on rows from another table. http://ecomputernotes.com
  • 21. Correlated UPDATE " Denormalize the EMPLOYEES table by adding a column to store the department name. " Populate the table by using a correlated update. ALTER TABLE employees ADD(department_name VARCHAR2(14)); UPDATE employees e SET department_name = (SELECT department_name FROM departments d WHERE e.department_id = d.department_id); http://ecomputernotes.com
  • 22. Correlated DELETE DELETE FROM table1 alias1 WHERE column operator (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); se a correlated subquery to delete rows in one table ased on rows from another table. http://ecomputernotes.com
  • 23. Correlated DELETE se a correlated subquery to delete only those rows rom the EMPLOYEES table that also exist in the MP_HISTORY table. DELETE FROM employees E WHERE employee_id = (SELECT employee_id FROM emp_history WHERE employee_id = E.employee_id); http://ecomputernotes.com
  • 24. The WITH Clause " Using the WITH clause, you can use the same query block in a SELECT statement when it occurs more than once within a complex query. " The WITH clause retrieves the results of a query block and stores it in the user's temporary tablespace. " The WITH clause improves performance http://ecomputernotes.com
  • 25. WITH Clause: Example Using the WITH clause, write a query to display the department name and total salaries for those departments whose total salary is greater than the average salary across departments. http://ecomputernotes.com
  • 26. WITH Clause: Example WITH dept_costs AS ( SELECT d.department_name, SUM(e.salary) AS dept_total FRO M employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name), avg_cost AS ( SELECT SUM(dept_total)/COUNT(*) AS dept_avg FRO M dept_costs) SELECT * FROM dept_costs WHERE dept_total > (SELECT dept_avg FRO M avg_cost ) ORDER BY department_name; http://ecomputernotes.com