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.