03 laboratory exercise 1 - WORKING WITH CTE

Anne Lee

ADDBASE EXERCISE

F0004
03 Laboratory Exercise 1 *Property of STI
Page 1 of 4
Laboratory Exercise 3
Working With Common Table Expressions (CTE)
Objectives
At the end of this exercise, the students are
expected to:
 Create a Common Table Expression (CTE)
 Create a Recursive Common Table Expression
(CTE)
 Computer Terminal
 Software installed on the terminal:
 Sybase SQL Anywhere 9
 Opening documents/reading – i.e. MS
Word, Acrobat Reader, etc.
 Printer and Paper (as necessary)
Activity 1 Creating and Populating the Table
1. Open Sybase Central.
2. Create a new database and name it "ADDBASE_LAB3".
3. Using the Sybase Central, create the table Employee as shown in Figure 3.1.
Figure 3.1 Employee Table
4. Populate the Employee table. In the Folders pane of the Sybase Central, right-click on the
database name and select Open Interactive SQL.
5. In the Interactive SQL window, type in the following codes:
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Steve', NULL, 'UK', 50, 200)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('James', 1, 'UK', 30, 500)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Neil', 1, 'US', 35, 600)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Blair', 2, 'AU', 41, 250)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Ken', 1, 'DE', 38, 100)
F0004
03 Laboratory Exercise 1 *Property of STI
Page 2 of 4
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Paul', 3, 'FR', 36, 480)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Adrian', 3, 'FR', 32, 290)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Ian', 4, 'UK', 27, 120)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Matt', 7, 'DE', 34, 10)
INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES
('Rolly', 2, 'PH', 22, 90)
Activity 2 Creating a Simple Common Table Expression (CTE)
1. Using the Interactive SQL, type the code below and execute.
Note: The WITH clause introduces a CTE and we define an in-memory result table called TM.
WITH TM(id, sales) AS
(
SELECT id, sales FROM Employee
)
SELECT
E.id, T.sales
FROM
Employee E INNER JOIN T ON E.id = T.id.
2. List the output and explain the output of the result of the CTE.
Activity 3 Creating a Recursive Common Table Expression (CTE)
Note: A recursive CTE is denoted by two queries joined by a UNION ALL operator and the second query
references the CTE table itself.
1. Create a recursive common table expression to get an employee and all employees he is
responsible for. Define an in-memory result table called Manager.
F0004
03 Laboratory Exercise 1 *Property of STI
Page 3 of 4
2. Using the Interactive SQL, type the code below. Set the id value to 3 then execute.
WITH Recursive Manager(id, name, mgr_id) AS
(
SELECT
id, name, mgr_id
FROM
Employee WHERE id=3
-- To use recusion we must say UNION ALL
UNION ALL
SELECT
Employee.id,
Employee.name,
Employee.mgr_id
FROM
Employee INNER JOIN Manager
ON Employee.mgr_id = Manager.id
)
SELECT
*
FROM
Manager
3. List the output and explain the output of the result of the CTE.
4. Repeat Step 2 and set the id value to 1.
5. List the output and explain the output of the result of the CTE.
F0004
03 Laboratory Exercise 1 *Property of STI
Page 4 of 4
6. Repeat Step 2 and set the id value to 2.
7. List the output and explain the output of the result of the CTE.
8. Create a recursive CTE to get the total sales of each employee and all the employees under
them. Write the script and the output below.

Recommandé

Setting a gradebook calulation on Moodle par
Setting a gradebook calulation on MoodleSetting a gradebook calulation on Moodle
Setting a gradebook calulation on MoodleSusanne Mills
643 vues4 diapositives
Function 2 par
Function 2Function 2
Function 2Spy Seat
439 vues27 diapositives
ITC 111 Lesson 5-3.1 Multiple Choice Quiz par
ITC 111 Lesson 5-3.1 Multiple Choice QuizITC 111 Lesson 5-3.1 Multiple Choice Quiz
ITC 111 Lesson 5-3.1 Multiple Choice QuizDafnaHabrew
66 vues4 diapositives
Microsoft Office: Practice Questions par
Microsoft Office: Practice Questions Microsoft Office: Practice Questions
Microsoft Office: Practice Questions Makaha Rutendo
66.9K vues112 diapositives
Functions of ms excel 2003 par
Functions of ms excel 2003Functions of ms excel 2003
Functions of ms excel 2003gaurav jain
1.6K vues7 diapositives
Lab2 Exercise 1 & 2 par
Lab2   Exercise 1 & 2Lab2   Exercise 1 & 2
Lab2 Exercise 1 & 2DRS444
192 vues2 diapositives

Contenu connexe

En vedette

Wk 15 ses 40 42 makrong kasanayan sa pagbabasa par
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
344 vues3 diapositives
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita par
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
357 vues3 diapositives
Database monitoring and performance management par
Database monitoring and performance managementDatabase monitoring and performance management
Database monitoring and performance managementAnne Lee
881 vues8 diapositives
Data warehousing par
Data warehousingData warehousing
Data warehousingAnne Lee
282 vues8 diapositives
Indexes - INSTRUCTOR'S GUIDE par
Indexes - INSTRUCTOR'S GUIDEIndexes - INSTRUCTOR'S GUIDE
Indexes - INSTRUCTOR'S GUIDEAnne Lee
322 vues4 diapositives
07 ohp slides 1 - INDEXES par
07 ohp slides 1 - INDEXES07 ohp slides 1 - INDEXES
07 ohp slides 1 - INDEXESAnne Lee
413 vues6 diapositives

En vedette(18)

Wk 15 ses 40 42 makrong kasanayan sa pagbabasa par Anne Lee
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
Anne Lee344 vues
Wk 13 ses 35 37 makrong kasanayan sa pagsasalita par Anne Lee
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
Anne Lee357 vues
Database monitoring and performance management par Anne Lee
Database monitoring and performance managementDatabase monitoring and performance management
Database monitoring and performance management
Anne Lee881 vues
Data warehousing par Anne Lee
Data warehousingData warehousing
Data warehousing
Anne Lee282 vues
Indexes - INSTRUCTOR'S GUIDE par Anne Lee
Indexes - INSTRUCTOR'S GUIDEIndexes - INSTRUCTOR'S GUIDE
Indexes - INSTRUCTOR'S GUIDE
Anne Lee322 vues
07 ohp slides 1 - INDEXES par Anne Lee
07 ohp slides 1 - INDEXES07 ohp slides 1 - INDEXES
07 ohp slides 1 - INDEXES
Anne Lee413 vues
08 ohp slides 1 par Anne Lee
08 ohp slides 108 ohp slides 1
08 ohp slides 1
Anne Lee395 vues
Wk 12 ses 32 34 makrong kasanayan sa pakikinig par Anne Lee
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
Anne Lee484 vues
Data mining par Anne Lee
Data miningData mining
Data mining
Anne Lee701 vues
Using olap par Anne Lee
Using olapUsing olap
Using olap
Anne Lee387 vues
06 laboratory exercise 1 par Anne Lee
06 laboratory exercise 106 laboratory exercise 1
06 laboratory exercise 1
Anne Lee1K vues
04 quiz 1 par Anne Lee
04 quiz 104 quiz 1
04 quiz 1
Anne Lee243 vues
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT par Anne Lee
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
Anne Lee594 vues
Week 17 slides 1 7 multidimensional, parallel, and distributed database par Anne Lee
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
Anne Lee973 vues
05 lcd slides 1 - CPU SCHEDULING (Powerpoint) par Anne Lee
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
Anne Lee931 vues
Advanced php par Anne Lee
Advanced phpAdvanced php
Advanced php
Anne Lee963 vues
Review of theory of database par Anne Lee
Review of theory of databaseReview of theory of database
Review of theory of database
Anne Lee710 vues
transportation and assignment models par Anne Lee
transportation and assignment modelstransportation and assignment models
transportation and assignment models
Anne Lee979 vues

Similaire à 03 laboratory exercise 1 - WORKING WITH CTE

Office session10 par
Office session10Office session10
Office session10Muhammad Ehtisham Siddiqui
161 vues36 diapositives
Page 3 of 11Delete this text and type your name here This .docx par
Page 3 of 11Delete this text and type your name here This .docxPage 3 of 11Delete this text and type your name here This .docx
Page 3 of 11Delete this text and type your name here This .docxalfred4lewis58146
2 vues12 diapositives
Spreadsheets 101 par
Spreadsheets 101Spreadsheets 101
Spreadsheets 101whita1bd
689 vues27 diapositives
North Carolina A&T State University .docx par
North Carolina A&T State University                         .docxNorth Carolina A&T State University                         .docx
North Carolina A&T State University .docxhenrymartin15260
4 vues8 diapositives
Pranavi verma-class-9-spreadsheet par
Pranavi verma-class-9-spreadsheetPranavi verma-class-9-spreadsheet
Pranavi verma-class-9-spreadsheetPranaviVerma
30 vues53 diapositives
Excel par
ExcelExcel
ExcelUrna Marie Baranda
255 vues13 diapositives

Similaire à 03 laboratory exercise 1 - WORKING WITH CTE(20)

Page 3 of 11Delete this text and type your name here This .docx par alfred4lewis58146
Page 3 of 11Delete this text and type your name here This .docxPage 3 of 11Delete this text and type your name here This .docx
Page 3 of 11Delete this text and type your name here This .docx
Spreadsheets 101 par whita1bd
Spreadsheets 101Spreadsheets 101
Spreadsheets 101
whita1bd689 vues
Pranavi verma-class-9-spreadsheet par PranaviVerma
Pranavi verma-class-9-spreadsheetPranavi verma-class-9-spreadsheet
Pranavi verma-class-9-spreadsheet
PranaviVerma30 vues
Hw8Excel - Exercise 8 Mail Merge-2.docINFS 3250In Class Pro.docx par adampcarr67227
Hw8Excel - Exercise 8 Mail Merge-2.docINFS 3250In Class Pro.docxHw8Excel - Exercise 8 Mail Merge-2.docINFS 3250In Class Pro.docx
Hw8Excel - Exercise 8 Mail Merge-2.docINFS 3250In Class Pro.docx
adampcarr6722710 vues
Succeeding in Business with Microsoft Excel 2010 A Problem Solving Approach 1... par KeithRomeros
Succeeding in Business with Microsoft Excel 2010 A Problem Solving Approach 1...Succeeding in Business with Microsoft Excel 2010 A Problem Solving Approach 1...
Succeeding in Business with Microsoft Excel 2010 A Problem Solving Approach 1...
KeithRomeros313 vues
04 laboratory exercise 1 par Anne Lee
04 laboratory exercise 104 laboratory exercise 1
04 laboratory exercise 1
Anne Lee537 vues
Lecture 6 spreadsheets par BBAMUMU2014
Lecture 6 spreadsheetsLecture 6 spreadsheets
Lecture 6 spreadsheets
BBAMUMU2014395 vues
Page 8 of 8Delete this text and type your name here This fil.docx par alfred4lewis58146
Page 8 of 8Delete this text and type your name here This fil.docxPage 8 of 8Delete this text and type your name here This fil.docx
Page 8 of 8Delete this text and type your name here This fil.docx
Instructions sc access_1a[1] par gaaaaaaaasa
Instructions sc access_1a[1]Instructions sc access_1a[1]
Instructions sc access_1a[1]
gaaaaaaaasa397 vues
PLEASE HELP ME COMPLETE THESE PYTHON PROGRAMMING ACTIVITIES Activity 1.docx par madalynbb3ja
PLEASE HELP ME COMPLETE THESE PYTHON PROGRAMMING ACTIVITIES Activity 1.docxPLEASE HELP ME COMPLETE THESE PYTHON PROGRAMMING ACTIVITIES Activity 1.docx
PLEASE HELP ME COMPLETE THESE PYTHON PROGRAMMING ACTIVITIES Activity 1.docx
madalynbb3ja3 vues
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21 par chinthala Vijaya Kumar
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
Pseudocode algorithim flowchart par fika sweety
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchart
fika sweety7.9K vues
Dbms question par Ricky Dky
Dbms questionDbms question
Dbms question
Ricky Dky986 vues
Make sure to make a copy of the Google Doc for this lab into.pdf par adityastores21
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdf

Plus de Anne Lee

Database backup and recovery par
Database backup and recoveryDatabase backup and recovery
Database backup and recoveryAnne Lee
7K vues8 diapositives
Database Security Slide Handout par
Database Security Slide HandoutDatabase Security Slide Handout
Database Security Slide HandoutAnne Lee
335 vues11 diapositives
Database Security Handout par
Database Security HandoutDatabase Security Handout
Database Security HandoutAnne Lee
239 vues3 diapositives
Database Security - IG par
Database Security - IGDatabase Security - IG
Database Security - IGAnne Lee
489 vues10 diapositives
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES par
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
284 vues4 diapositives
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION par
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
822 vues11 diapositives

Plus de Anne Lee(15)

Database backup and recovery par Anne Lee
Database backup and recoveryDatabase backup and recovery
Database backup and recovery
Anne Lee7K vues
Database Security Slide Handout par Anne Lee
Database Security Slide HandoutDatabase Security Slide Handout
Database Security Slide Handout
Anne Lee335 vues
Database Security Handout par Anne Lee
Database Security HandoutDatabase Security Handout
Database Security Handout
Anne Lee239 vues
Database Security - IG par Anne Lee
Database Security - IGDatabase Security - IG
Database Security - IG
Anne Lee489 vues
02 laboratory exercise 1 - RETRIEVING DATA FROM SEVERAL TABLES par Anne Lee
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
Anne Lee284 vues
01 laboratory exercise 1 - DESIGN A SIMPLE DATABASE APPLICATION par Anne Lee
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
Anne Lee822 vues
07 ohp slide handout 1 - INDEXES par Anne Lee
07 ohp slide handout 1 - INDEXES07 ohp slide handout 1 - INDEXES
07 ohp slide handout 1 - INDEXES
Anne Lee191 vues
Wk 16 ses 43 45 makrong kasanayan sa pagsusulat par Anne Lee
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
Anne Lee388 vues
Wk 11 ses 29 31 konseptong pangkomunikasyon - FILIPINO 1 par Anne Lee
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
Anne Lee746 vues
07 lcd slides 1 - DEADLOCKS POWERPOINT par Anne Lee
07 lcd slides 1 - DEADLOCKS POWERPOINT07 lcd slides 1 - DEADLOCKS POWERPOINT
07 lcd slides 1 - DEADLOCKS POWERPOINT
Anne Lee330 vues
05 lcd slide handout 1 - CPU SCHEDULING par Anne Lee
05 lcd slide handout 1 - CPU SCHEDULING05 lcd slide handout 1 - CPU SCHEDULING
05 lcd slide handout 1 - CPU SCHEDULING
Anne Lee300 vues
04 quiz 1 answer key par Anne Lee
04 quiz 1 answer key04 quiz 1 answer key
04 quiz 1 answer key
Anne Lee2.1K vues
03 ohp slides 1 par Anne Lee
03 ohp slides 103 ohp slides 1
03 ohp slides 1
Anne Lee133 vues
03 ohp slide handout 1 par Anne Lee
03 ohp slide handout 103 ohp slide handout 1
03 ohp slide handout 1
Anne Lee234 vues
03 laboratory exercise 1 par Anne Lee
03 laboratory exercise 103 laboratory exercise 1
03 laboratory exercise 1
Anne Lee585 vues

Dernier

WebAssembly par
WebAssemblyWebAssembly
WebAssemblyJens Siebert
33 vues18 diapositives
Winter '24 Release Chat.pdf par
Winter '24 Release Chat.pdfWinter '24 Release Chat.pdf
Winter '24 Release Chat.pdfmelbourneauuser
9 vues20 diapositives
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan... par
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...Deltares
11 vues30 diapositives
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... par
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Donato Onofri
711 vues34 diapositives
SAP FOR TYRE INDUSTRY.pdf par
SAP FOR TYRE INDUSTRY.pdfSAP FOR TYRE INDUSTRY.pdf
SAP FOR TYRE INDUSTRY.pdfVirendra Rai, PMP
23 vues3 diapositives
Roadmap y Novedades de producto par
Roadmap y Novedades de productoRoadmap y Novedades de producto
Roadmap y Novedades de productoNeo4j
50 vues33 diapositives

Dernier(20)

DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan... par Deltares
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...
DSD-INT 2023 Baseline studies for Strategic Coastal protection for Long Islan...
Deltares11 vues
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ... par Donato Onofri
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Unmasking the Dark Art of Vectored Exception Handling: Bypassing XDR and EDR ...
Donato Onofri711 vues
Roadmap y Novedades de producto par Neo4j
Roadmap y Novedades de productoRoadmap y Novedades de producto
Roadmap y Novedades de producto
Neo4j50 vues
Consulting for Data Monetization Maximizing the Profit Potential of Your Data... par Flexsin
Consulting for Data Monetization Maximizing the Profit Potential of Your Data...Consulting for Data Monetization Maximizing the Profit Potential of Your Data...
Consulting for Data Monetization Maximizing the Profit Potential of Your Data...
Flexsin 15 vues
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx par animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm13 vues
Fleet Management Software in India par Fleetable
Fleet Management Software in India Fleet Management Software in India
Fleet Management Software in India
Fleetable11 vues
A first look at MariaDB 11.x features and ideas on how to use them par Federico Razzoli
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use them
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ... par Deltares
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
Deltares9 vues
DSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - Prida par Deltares
DSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - PridaDSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - Prida
DSD-INT 2023 Dam break simulation in Derna (Libya) using HydroMT_SFINCS - Prida
Deltares18 vues
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema par Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 vues
Software evolution understanding: Automatic extraction of software identifier... par Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
MariaDB stored procedures and why they should be improved par Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker par Deltares
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - ParkerDSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
DSD-INT 2023 SFINCS Modelling in the U.S. Pacific Northwest - Parker
Deltares9 vues

03 laboratory exercise 1 - WORKING WITH CTE

  • 1. F0004 03 Laboratory Exercise 1 *Property of STI Page 1 of 4 Laboratory Exercise 3 Working With Common Table Expressions (CTE) Objectives At the end of this exercise, the students are expected to:  Create a Common Table Expression (CTE)  Create a Recursive Common Table Expression (CTE)  Computer Terminal  Software installed on the terminal:  Sybase SQL Anywhere 9  Opening documents/reading – i.e. MS Word, Acrobat Reader, etc.  Printer and Paper (as necessary) Activity 1 Creating and Populating the Table 1. Open Sybase Central. 2. Create a new database and name it "ADDBASE_LAB3". 3. Using the Sybase Central, create the table Employee as shown in Figure 3.1. Figure 3.1 Employee Table 4. Populate the Employee table. In the Folders pane of the Sybase Central, right-click on the database name and select Open Interactive SQL. 5. In the Interactive SQL window, type in the following codes: INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Steve', NULL, 'UK', 50, 200) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('James', 1, 'UK', 30, 500) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Neil', 1, 'US', 35, 600) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Blair', 2, 'AU', 41, 250) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Ken', 1, 'DE', 38, 100)
  • 2. F0004 03 Laboratory Exercise 1 *Property of STI Page 2 of 4 INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Paul', 3, 'FR', 36, 480) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Adrian', 3, 'FR', 32, 290) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Ian', 4, 'UK', 27, 120) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Matt', 7, 'DE', 34, 10) INSERT INTO Employee(name, mgr_id, country, age, sales) VALUES ('Rolly', 2, 'PH', 22, 90) Activity 2 Creating a Simple Common Table Expression (CTE) 1. Using the Interactive SQL, type the code below and execute. Note: The WITH clause introduces a CTE and we define an in-memory result table called TM. WITH TM(id, sales) AS ( SELECT id, sales FROM Employee ) SELECT E.id, T.sales FROM Employee E INNER JOIN T ON E.id = T.id. 2. List the output and explain the output of the result of the CTE. Activity 3 Creating a Recursive Common Table Expression (CTE) Note: A recursive CTE is denoted by two queries joined by a UNION ALL operator and the second query references the CTE table itself. 1. Create a recursive common table expression to get an employee and all employees he is responsible for. Define an in-memory result table called Manager.
  • 3. F0004 03 Laboratory Exercise 1 *Property of STI Page 3 of 4 2. Using the Interactive SQL, type the code below. Set the id value to 3 then execute. WITH Recursive Manager(id, name, mgr_id) AS ( SELECT id, name, mgr_id FROM Employee WHERE id=3 -- To use recusion we must say UNION ALL UNION ALL SELECT Employee.id, Employee.name, Employee.mgr_id FROM Employee INNER JOIN Manager ON Employee.mgr_id = Manager.id ) SELECT * FROM Manager 3. List the output and explain the output of the result of the CTE. 4. Repeat Step 2 and set the id value to 1. 5. List the output and explain the output of the result of the CTE.
  • 4. F0004 03 Laboratory Exercise 1 *Property of STI Page 4 of 4 6. Repeat Step 2 and set the id value to 2. 7. List the output and explain the output of the result of the CTE. 8. Create a recursive CTE to get the total sales of each employee and all the employees under them. Write the script and the output below.