SlideShare une entreprise Scribd logo
1  sur  3
Télécharger pour lire hors ligne
F0004
* Property of STI
Page 1 of 11
Common Table Expressions
What is Common Table
Expressions?
 Common table expressions (CTE)
are temporary result set that are
known only within the scope of a
single SELECT, INSERT, UPDATE,
DELETE or CREATE VIEW statement.
 Common table expressions are
generally useful in a query that
involves multiple aggregate
functions.
1 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 3 of 11
Common Table Expressions
Using Common Table
Expressions
 Example 2:
WITH CountEmployees(dept_id, n)
AS
( SELECT dept_id, count(*) AS n
FROM employee GROUP BY dept_id )
SELECT a.dept_id, a.n,
b.dept_id, b.n
FROM CountEmployees AS a JOIN
CountEmployees AS b
ON a.n = b.n AND a.dept_id <
b.dept_id
3 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 2 of 11
Common Table Expressions
Using Common Table
Expressions
 The common table expressions are
defined using the WITH clause.
 Example 1:
WITH CountEmployees(dept_id, n)
AS
( SELECT dept_id, count(*) AS n
FROM employee GROUP BY dept_id )
SELECT dept_id, n
FROM CountEmployees
WHERE n = ( SELECT max(n)
FROM CountEmployees )
2 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 4 of 11
Common Table Expressions
Using Common Table
Expressions
 Example 3:
WITH
CountEmployees(dept_id, n) AS
( SELECT dept_id, count(*) AS n
FROM employee GROUP BY
dept_id ),
DeptPayroll( dept_id, amt ) AS
( SELECT dept_id, sum(salary)
AS amt
FROM employee GROUP BY dept_id )
SELECT count.dept_id, count.n,
pay.amt
FROM CountEmployees AS count
JOIN DeptPayroll AS pay
ON count.dept_id = pay.dept_id
WHERE count.n = ( SELECT max(n)
FROM CountEmployees )
OR pay.amt = ( SELECT min(amt)
FROM DeptPayroll )
4 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 5 of 11
Common Table Expressions
Exercise
 Assume that you need to
determine which class has the
most number of students. The
student table lists all the students
and specifies in which class each
belong. Using common table
expressions, find the following:
1. Extract the class with the most
students. Extract the class with
the fewest students.
2. List the class that has the highest
GPA of students.
5 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 7 of 11
Common Table Expressions
What is Recursive Common
Table Expressions
 Recursive common table
expressions allow you to query
tables that represent hierarchical
information.
 A recursive common table
expression is composed of an initial
subquery or seed and a recursive
subquery.
7 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 6 of 11
Common Table Expressions
Applications of Common
Table Expressions
 Common table expressions are
useful whenever multiple levels of
aggregation must occur within a
single query.
 Views within a procedure that
must contain a reference to a
program variable.
 Queries that use temporary result
set to store a set of values.
6 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 8 of 11
Common Table Expressions
Example
WITH RECURSIVE
manager ( emp_id, manager_id,
emp_fname, emp_lname, mgmt_level )
AS
( ( SELECT emp_id, manager_id, --
initial subquery
emp_fname, emp_lname, 0
FROM employee AS e
WHERE manager_id = emp_id )
UNION ALL
( SELECT e.emp_id, e.manager_id, -
- recursive subquery
e.emp_fname, e.emp_lname,
m.mgmt_level + 1
FROM employee AS e JOIN manager AS
m
ON e.manager_id = m.emp_id
AND e.manager_id <> e.emp_id
AND m.mgmt_level < 20 ) )
SELECT * FROM manager
ORDER BY mgmt_level, emp_lname,
emp_fname
8 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 9 of 11
Common Table Expressions
Restrictions on Recursive
Common Table Expression
 Recursive common table
expressions cannot be mutually
recursive.
 The only set operator permitted
between the initial subquery and
the recursive subquery is UNION
ALL.
 Within the definition of a recursive
subquery, a self-reference to the
recursive table expression can
appear only within the FROM
clause of the recursive subquery.
9 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 11 of 11
Common Table Expressions
Exercise
 Using the recursive common table
expression, write a query that
displays the Fibonacci sequence.
TIP: The Fibonacci sequence is the sequence in
which each number is the sum of the two
preceding numbers such as 1, 1, 2, 3, 5, 8, 13,
21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,
2584, 4181, ... (each number is the sum of
the previous two). The Fibonacci sequence,
generated by the rule f1 = f2 = 1 , fn+1 = fn +
fn-1, is well known in many different areas of
mathematics and science.
11 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
F0004
* Property of STI
Page 10 of 11
Common Table Expressions
Restrictions on Recursive
Common Table Expression
 The recursive subquery cannot
contain DISTINCT, or a GROUP BY
or an ORDER BY clause.
 The recursive subquery can not
make use of any aggregate
function.
 To prevent runaway recursive
queries, an error is generated if
the number of levels of recursion
exceeds the current setting of the
MAX_RECURSIVE_ITERATIONS
option.
10 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Contenu connexe

Tendances

Employment opportunities
Employment opportunitiesEmployment opportunities
Employment opportunitiesabigail270595
 
Bi year 4 paper 2
Bi year 4 paper 2Bi year 4 paper 2
Bi year 4 paper 2Salleh Loki
 
The Jacket Graphic Organizer (Mr. Schroeder's Class)
The Jacket Graphic Organizer (Mr. Schroeder's Class)The Jacket Graphic Organizer (Mr. Schroeder's Class)
The Jacket Graphic Organizer (Mr. Schroeder's Class)epfund
 
BAHASA INGGERIS TAHUN 4 2017 KERTAS 2
BAHASA INGGERIS TAHUN 4 2017 KERTAS 2BAHASA INGGERIS TAHUN 4 2017 KERTAS 2
BAHASA INGGERIS TAHUN 4 2017 KERTAS 2Noor Lubna Ismail
 
Sjkc eng paper 2 set 2
Sjkc eng paper 2 set 2Sjkc eng paper 2 set 2
Sjkc eng paper 2 set 2SELVAM PERUMAL
 
Sjkc eng paper 2 set 1
Sjkc  eng  paper 2 set 1Sjkc  eng  paper 2 set 1
Sjkc eng paper 2 set 1SELVAM PERUMAL
 
Presentation handout edtc 640
Presentation handout  edtc 640Presentation handout  edtc 640
Presentation handout edtc 640debnatb
 
ADDIE Model
ADDIE ModelADDIE Model
ADDIE Modelctd515
 
English exam for 2015 (Mid year 5)
English exam for 2015 (Mid year 5)English exam for 2015 (Mid year 5)
English exam for 2015 (Mid year 5)Siti Farah Idayu
 
Set 1 kertas 2(English UPSR)
Set 1 kertas 2(English UPSR)Set 1 kertas 2(English UPSR)
Set 1 kertas 2(English UPSR)SKDH2
 
(Baru) kertas soalan bi tahun 1
(Baru) kertas soalan bi tahun 1(Baru) kertas soalan bi tahun 1
(Baru) kertas soalan bi tahun 1Izzaida Ibrahim
 
Change 4 to its simplest form
Change 4 to its simplest formChange 4 to its simplest form
Change 4 to its simplest formHazifahrun Fixa
 
Fractions shapes amounts
Fractions shapes amountsFractions shapes amounts
Fractions shapes amountsSean Dillon
 
Transformative Learning
Transformative LearningTransformative Learning
Transformative Learningctd515
 

Tendances (19)

Employment opportunities
Employment opportunitiesEmployment opportunities
Employment opportunities
 
Bi year 4 paper 2
Bi year 4 paper 2Bi year 4 paper 2
Bi year 4 paper 2
 
The Jacket Graphic Organizer (Mr. Schroeder's Class)
The Jacket Graphic Organizer (Mr. Schroeder's Class)The Jacket Graphic Organizer (Mr. Schroeder's Class)
The Jacket Graphic Organizer (Mr. Schroeder's Class)
 
BAHASA INGGERIS TAHUN 4 2017 KERTAS 2
BAHASA INGGERIS TAHUN 4 2017 KERTAS 2BAHASA INGGERIS TAHUN 4 2017 KERTAS 2
BAHASA INGGERIS TAHUN 4 2017 KERTAS 2
 
Sjkc eng paper 2 set 2
Sjkc eng paper 2 set 2Sjkc eng paper 2 set 2
Sjkc eng paper 2 set 2
 
Sjkc eng paper 2 set 1
Sjkc  eng  paper 2 set 1Sjkc  eng  paper 2 set 1
Sjkc eng paper 2 set 1
 
Presentation handout edtc 640
Presentation handout  edtc 640Presentation handout  edtc 640
Presentation handout edtc 640
 
Martin luther king jr
Martin luther king jrMartin luther king jr
Martin luther king jr
 
Mock
MockMock
Mock
 
Kssr English Yr 4 Mid term exam
Kssr  English Yr 4 Mid term examKssr  English Yr 4 Mid term exam
Kssr English Yr 4 Mid term exam
 
ADDIE Model
ADDIE ModelADDIE Model
ADDIE Model
 
English exam for 2015 (Mid year 5)
English exam for 2015 (Mid year 5)English exam for 2015 (Mid year 5)
English exam for 2015 (Mid year 5)
 
Nift 2010 cat sample question papers
Nift 2010 cat sample question papersNift 2010 cat sample question papers
Nift 2010 cat sample question papers
 
Set 1 kertas 2(English UPSR)
Set 1 kertas 2(English UPSR)Set 1 kertas 2(English UPSR)
Set 1 kertas 2(English UPSR)
 
(Baru) kertas soalan bi tahun 1
(Baru) kertas soalan bi tahun 1(Baru) kertas soalan bi tahun 1
(Baru) kertas soalan bi tahun 1
 
Change 4 to its simplest form
Change 4 to its simplest formChange 4 to its simplest form
Change 4 to its simplest form
 
Fractions shapes amounts
Fractions shapes amountsFractions shapes amounts
Fractions shapes amounts
 
Bi paper pat kp
Bi paper  pat kpBi paper  pat kp
Bi paper pat kp
 
Transformative Learning
Transformative LearningTransformative Learning
Transformative Learning
 

En vedette

Public Parks and Fitness Groups
Public Parks and Fitness GroupsPublic Parks and Fitness Groups
Public Parks and Fitness GroupsExSite
 
O flautista
O flautistaO flautista
O flautistaespin
 
The Balanced Care Method Flyer
The Balanced Care Method FlyerThe Balanced Care Method Flyer
The Balanced Care Method FlyerJared Caplan
 
Proyecto valle
Proyecto valleProyecto valle
Proyecto vallefraconma
 
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)Anne Lee
 
Strategic Communications and National Security
Strategic Communications and National SecurityStrategic Communications and National Security
Strategic Communications and National SecurityBob Crawshaw
 
El párrafo: introducción, desarrollo y cierre
El párrafo: introducción, desarrollo y cierreEl párrafo: introducción, desarrollo y cierre
El párrafo: introducción, desarrollo y cierrejoseorrlandoabantoquevedo
 
Organizadores de bodas
Organizadores de bodasOrganizadores de bodas
Organizadores de bodasYoriOsorio
 

En vedette (9)

Monitor PS Sardegna
Monitor PS SardegnaMonitor PS Sardegna
Monitor PS Sardegna
 
Public Parks and Fitness Groups
Public Parks and Fitness GroupsPublic Parks and Fitness Groups
Public Parks and Fitness Groups
 
O flautista
O flautistaO flautista
O flautista
 
The Balanced Care Method Flyer
The Balanced Care Method FlyerThe Balanced Care Method Flyer
The Balanced Care Method Flyer
 
Proyecto valle
Proyecto valleProyecto valle
Proyecto valle
 
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
 
Strategic Communications and National Security
Strategic Communications and National SecurityStrategic Communications and National Security
Strategic Communications and National Security
 
El párrafo: introducción, desarrollo y cierre
El párrafo: introducción, desarrollo y cierreEl párrafo: introducción, desarrollo y cierre
El párrafo: introducción, desarrollo y cierre
 
Organizadores de bodas
Organizadores de bodasOrganizadores de bodas
Organizadores de bodas
 

Similaire à 03 ohp slide handout 1

CA Database Scavenger Hunt pt. 1
CA Database Scavenger Hunt pt. 1CA Database Scavenger Hunt pt. 1
CA Database Scavenger Hunt pt. 1amytaylor
 
08 final tv drama revision session
08 final tv drama revision session08 final tv drama revision session
08 final tv drama revision sessionibz10
 
Year 13 sports studies exam 1
Year 13 sports studies   exam 1Year 13 sports studies   exam 1
Year 13 sports studies exam 1dcalevelpe
 
Test your idea questionnaire
Test your idea questionnaireTest your idea questionnaire
Test your idea questionnairestartupJamaica
 
Learning Team Case – Week 4A firm has five identified capital p.docx
Learning Team Case – Week  4A firm has five identified capital p.docxLearning Team Case – Week  4A firm has five identified capital p.docx
Learning Team Case – Week 4A firm has five identified capital p.docxSHIVA101531
 
Output Devices In-Class Worksheet (Hard)
Output Devices In-Class Worksheet (Hard)Output Devices In-Class Worksheet (Hard)
Output Devices In-Class Worksheet (Hard)Biscette InfoTech
 
Support Worksheets
Support WorksheetsSupport Worksheets
Support Worksheetssafia
 
Q 1-3 Chapter 1Q 4 Chapter 2Q 5-6 Chapter 3Q 7- 8 Ch.docx
Q 1-3  Chapter 1Q 4      Chapter 2Q 5-6  Chapter 3Q 7- 8  Ch.docxQ 1-3  Chapter 1Q 4      Chapter 2Q 5-6  Chapter 3Q 7- 8  Ch.docx
Q 1-3 Chapter 1Q 4 Chapter 2Q 5-6 Chapter 3Q 7- 8 Ch.docxmakdul
 
Fba Powerpoint For Inservice Print Copy For Participants
Fba Powerpoint For Inservice Print Copy For ParticipantsFba Powerpoint For Inservice Print Copy For Participants
Fba Powerpoint For Inservice Print Copy For ParticipantsMac Barnett
 
Test1foundation nimby
Test1foundation nimbyTest1foundation nimby
Test1foundation nimbySusie Nash
 
job analysis questionnaire
job analysis questionnairejob analysis questionnaire
job analysis questionnaireHarve Abella
 
Milk learning board
Milk learning boardMilk learning board
Milk learning boardsiobhanpdst
 
Ice mid semester exam 2013 student
Ice mid semester exam 2013 studentIce mid semester exam 2013 student
Ice mid semester exam 2013 studentRichard Chamberlain
 

Similaire à 03 ohp slide handout 1 (20)

04 quiz 1
04 quiz 104 quiz 1
04 quiz 1
 
CA Database Scavenger Hunt pt. 1
CA Database Scavenger Hunt pt. 1CA Database Scavenger Hunt pt. 1
CA Database Scavenger Hunt pt. 1
 
08 final tv drama revision session
08 final tv drama revision session08 final tv drama revision session
08 final tv drama revision session
 
Year 13 sports studies exam 1
Year 13 sports studies   exam 1Year 13 sports studies   exam 1
Year 13 sports studies exam 1
 
Test your idea questionnaire
Test your idea questionnaireTest your idea questionnaire
Test your idea questionnaire
 
Dna (1)
Dna (1)Dna (1)
Dna (1)
 
Learning Team Case – Week 4A firm has five identified capital p.docx
Learning Team Case – Week  4A firm has five identified capital p.docxLearning Team Case – Week  4A firm has five identified capital p.docx
Learning Team Case – Week 4A firm has five identified capital p.docx
 
Work
WorkWork
Work
 
Output Devices In-Class Worksheet (Hard)
Output Devices In-Class Worksheet (Hard)Output Devices In-Class Worksheet (Hard)
Output Devices In-Class Worksheet (Hard)
 
Support Worksheets
Support WorksheetsSupport Worksheets
Support Worksheets
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Q 1-3 Chapter 1Q 4 Chapter 2Q 5-6 Chapter 3Q 7- 8 Ch.docx
Q 1-3  Chapter 1Q 4      Chapter 2Q 5-6  Chapter 3Q 7- 8  Ch.docxQ 1-3  Chapter 1Q 4      Chapter 2Q 5-6  Chapter 3Q 7- 8  Ch.docx
Q 1-3 Chapter 1Q 4 Chapter 2Q 5-6 Chapter 3Q 7- 8 Ch.docx
 
Landscape Outline
Landscape OutlineLandscape Outline
Landscape Outline
 
Fba Powerpoint For Inservice Print Copy For Participants
Fba Powerpoint For Inservice Print Copy For ParticipantsFba Powerpoint For Inservice Print Copy For Participants
Fba Powerpoint For Inservice Print Copy For Participants
 
Test1foundation nimby
Test1foundation nimbyTest1foundation nimby
Test1foundation nimby
 
job analysis questionnaire
job analysis questionnairejob analysis questionnaire
job analysis questionnaire
 
Application biocamp 2011
Application biocamp 2011Application biocamp 2011
Application biocamp 2011
 
Milk learning board
Milk learning boardMilk learning board
Milk learning board
 
Ice mid semester exam 2013 student
Ice mid semester exam 2013 studentIce mid semester exam 2013 student
Ice mid semester exam 2013 student
 
Building a Teacher-Professional Team
Building a Teacher-Professional TeamBuilding a Teacher-Professional Team
Building a Teacher-Professional Team
 

Plus de Anne Lee

Week 17 slides 1 7 multidimensional, parallel, and distributed database
Week 17 slides 1 7 multidimensional, parallel, and distributed databaseWeek 17 slides 1 7 multidimensional, parallel, and distributed database
Week 17 slides 1 7 multidimensional, parallel, and distributed databaseAnne Lee
 
Data mining
Data miningData mining
Data miningAnne Lee
 
Data warehousing
Data warehousingData warehousing
Data warehousingAnne Lee
 
Database backup and recovery
Database backup and recoveryDatabase backup and recovery
Database backup and recoveryAnne Lee
 
Database monitoring and performance management
Database monitoring and performance managementDatabase monitoring and performance management
Database monitoring and performance managementAnne Lee
 
transportation and assignment models
transportation and assignment modelstransportation and assignment models
transportation and assignment modelsAnne Lee
 
Database Security Slide Handout
Database Security Slide HandoutDatabase Security Slide Handout
Database Security Slide HandoutAnne Lee
 
Database Security Handout
Database Security HandoutDatabase Security Handout
Database Security HandoutAnne Lee
 
Database Security - IG
Database Security - IGDatabase Security - IG
Database Security - IGAnne Lee
 
03 laboratory exercise 1 - WORKING WITH CTE
03 laboratory exercise 1 - WORKING WITH CTE03 laboratory exercise 1 - WORKING WITH CTE
03 laboratory exercise 1 - WORKING WITH CTEAnne Lee
 
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLESAnne Lee
 
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATIONAnne Lee
 
Indexes - INSTRUCTOR'S GUIDE
Indexes - INSTRUCTOR'S GUIDEIndexes - INSTRUCTOR'S GUIDE
Indexes - INSTRUCTOR'S GUIDEAnne Lee
 
07 ohp slides 1 - INDEXES
07 ohp slides 1 - INDEXES07 ohp slides 1 - INDEXES
07 ohp slides 1 - INDEXESAnne Lee
 
07 ohp slide handout 1 - INDEXES
07 ohp slide handout 1 - INDEXES07 ohp slide handout 1 - INDEXES
07 ohp slide handout 1 - INDEXESAnne Lee
 
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat
Wk 16 ses 43 45 makrong kasanayan sa pagsusulatWk 16 ses 43 45 makrong kasanayan sa pagsusulat
Wk 16 ses 43 45 makrong kasanayan sa pagsusulatAnne Lee
 
Wk 15 ses 40 42 makrong kasanayan sa pagbabasa
Wk 15 ses 40 42 makrong kasanayan sa pagbabasaWk 15 ses 40 42 makrong kasanayan sa pagbabasa
Wk 15 ses 40 42 makrong kasanayan sa pagbabasaAnne Lee
 
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita
Wk 13 ses 35 37 makrong kasanayan sa pagsasalitaWk 13 ses 35 37 makrong kasanayan sa pagsasalita
Wk 13 ses 35 37 makrong kasanayan sa pagsasalitaAnne Lee
 
Wk 12 ses 32 34 makrong kasanayan sa pakikinig
Wk 12 ses 32 34 makrong kasanayan sa pakikinigWk 12 ses 32 34 makrong kasanayan sa pakikinig
Wk 12 ses 32 34 makrong kasanayan sa pakikinigAnne Lee
 
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1Anne Lee
 

Plus de Anne Lee (20)

Week 17 slides 1 7 multidimensional, parallel, and distributed database
Week 17 slides 1 7 multidimensional, parallel, and distributed databaseWeek 17 slides 1 7 multidimensional, parallel, and distributed database
Week 17 slides 1 7 multidimensional, parallel, and distributed database
 
Data mining
Data miningData mining
Data mining
 
Data warehousing
Data warehousingData warehousing
Data warehousing
 
Database backup and recovery
Database backup and recoveryDatabase backup and recovery
Database backup and recovery
 
Database monitoring and performance management
Database monitoring and performance managementDatabase monitoring and performance management
Database monitoring and performance management
 
transportation and assignment models
transportation and assignment modelstransportation and assignment models
transportation and assignment models
 
Database Security Slide Handout
Database Security Slide HandoutDatabase Security Slide Handout
Database Security Slide Handout
 
Database Security Handout
Database Security HandoutDatabase Security Handout
Database Security Handout
 
Database Security - IG
Database Security - IGDatabase Security - IG
Database Security - IG
 
03 laboratory exercise 1 - WORKING WITH CTE
03 laboratory exercise 1 - WORKING WITH CTE03 laboratory exercise 1 - WORKING WITH CTE
03 laboratory exercise 1 - WORKING WITH CTE
 
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES
 
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION
 
Indexes - INSTRUCTOR'S GUIDE
Indexes - INSTRUCTOR'S GUIDEIndexes - INSTRUCTOR'S GUIDE
Indexes - INSTRUCTOR'S GUIDE
 
07 ohp slides 1 - INDEXES
07 ohp slides 1 - INDEXES07 ohp slides 1 - INDEXES
07 ohp slides 1 - INDEXES
 
07 ohp slide handout 1 - INDEXES
07 ohp slide handout 1 - INDEXES07 ohp slide handout 1 - INDEXES
07 ohp slide handout 1 - INDEXES
 
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat
Wk 16 ses 43 45 makrong kasanayan sa pagsusulatWk 16 ses 43 45 makrong kasanayan sa pagsusulat
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat
 
Wk 15 ses 40 42 makrong kasanayan sa pagbabasa
Wk 15 ses 40 42 makrong kasanayan sa pagbabasaWk 15 ses 40 42 makrong kasanayan sa pagbabasa
Wk 15 ses 40 42 makrong kasanayan sa pagbabasa
 
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita
Wk 13 ses 35 37 makrong kasanayan sa pagsasalitaWk 13 ses 35 37 makrong kasanayan sa pagsasalita
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita
 
Wk 12 ses 32 34 makrong kasanayan sa pakikinig
Wk 12 ses 32 34 makrong kasanayan sa pakikinigWk 12 ses 32 34 makrong kasanayan sa pakikinig
Wk 12 ses 32 34 makrong kasanayan sa pakikinig
 
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1
 

Dernier

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 

Dernier (20)

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 

03 ohp slide handout 1

  • 1. F0004 * Property of STI Page 1 of 11 Common Table Expressions What is Common Table Expressions?  Common table expressions (CTE) are temporary result set that are known only within the scope of a single SELECT, INSERT, UPDATE, DELETE or CREATE VIEW statement.  Common table expressions are generally useful in a query that involves multiple aggregate functions. 1 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 3 of 11 Common Table Expressions Using Common Table Expressions  Example 2: WITH CountEmployees(dept_id, n) AS ( SELECT dept_id, count(*) AS n FROM employee GROUP BY dept_id ) SELECT a.dept_id, a.n, b.dept_id, b.n FROM CountEmployees AS a JOIN CountEmployees AS b ON a.n = b.n AND a.dept_id < b.dept_id 3 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 2 of 11 Common Table Expressions Using Common Table Expressions  The common table expressions are defined using the WITH clause.  Example 1: WITH CountEmployees(dept_id, n) AS ( SELECT dept_id, count(*) AS n FROM employee GROUP BY dept_id ) SELECT dept_id, n FROM CountEmployees WHERE n = ( SELECT max(n) FROM CountEmployees ) 2 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 4 of 11 Common Table Expressions Using Common Table Expressions  Example 3: WITH CountEmployees(dept_id, n) AS ( SELECT dept_id, count(*) AS n FROM employee GROUP BY dept_id ), DeptPayroll( dept_id, amt ) AS ( SELECT dept_id, sum(salary) AS amt FROM employee GROUP BY dept_id ) SELECT count.dept_id, count.n, pay.amt FROM CountEmployees AS count JOIN DeptPayroll AS pay ON count.dept_id = pay.dept_id WHERE count.n = ( SELECT max(n) FROM CountEmployees ) OR pay.amt = ( SELECT min(amt) FROM DeptPayroll ) 4 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________
  • 2. F0004 * Property of STI Page 5 of 11 Common Table Expressions Exercise  Assume that you need to determine which class has the most number of students. The student table lists all the students and specifies in which class each belong. Using common table expressions, find the following: 1. Extract the class with the most students. Extract the class with the fewest students. 2. List the class that has the highest GPA of students. 5 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 7 of 11 Common Table Expressions What is Recursive Common Table Expressions  Recursive common table expressions allow you to query tables that represent hierarchical information.  A recursive common table expression is composed of an initial subquery or seed and a recursive subquery. 7 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 6 of 11 Common Table Expressions Applications of Common Table Expressions  Common table expressions are useful whenever multiple levels of aggregation must occur within a single query.  Views within a procedure that must contain a reference to a program variable.  Queries that use temporary result set to store a set of values. 6 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 8 of 11 Common Table Expressions Example WITH RECURSIVE manager ( emp_id, manager_id, emp_fname, emp_lname, mgmt_level ) AS ( ( SELECT emp_id, manager_id, -- initial subquery emp_fname, emp_lname, 0 FROM employee AS e WHERE manager_id = emp_id ) UNION ALL ( SELECT e.emp_id, e.manager_id, - - recursive subquery e.emp_fname, e.emp_lname, m.mgmt_level + 1 FROM employee AS e JOIN manager AS m ON e.manager_id = m.emp_id AND e.manager_id <> e.emp_id AND m.mgmt_level < 20 ) ) SELECT * FROM manager ORDER BY mgmt_level, emp_lname, emp_fname 8 __________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________
  • 3. F0004 * Property of STI Page 9 of 11 Common Table Expressions Restrictions on Recursive Common Table Expression  Recursive common table expressions cannot be mutually recursive.  The only set operator permitted between the initial subquery and the recursive subquery is UNION ALL.  Within the definition of a recursive subquery, a self-reference to the recursive table expression can appear only within the FROM clause of the recursive subquery. 9 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 11 of 11 Common Table Expressions Exercise  Using the recursive common table expression, write a query that displays the Fibonacci sequence. TIP: The Fibonacci sequence is the sequence in which each number is the sum of the two preceding numbers such as 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, ... (each number is the sum of the previous two). The Fibonacci sequence, generated by the rule f1 = f2 = 1 , fn+1 = fn + fn-1, is well known in many different areas of mathematics and science. 11 _________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ F0004 * Property of STI Page 10 of 11 Common Table Expressions Restrictions on Recursive Common Table Expression  The recursive subquery cannot contain DISTINCT, or a GROUP BY or an ORDER BY clause.  The recursive subquery can not make use of any aggregate function.  To prevent runaway recursive queries, an error is generated if the number of levels of recursion exceeds the current setting of the MAX_RECURSIVE_ITERATIONS option. 10 ________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________ ___________________