You are on page 1of 32

.. Internal Schema: How and where stored?

.. Describes (DBMS specific) the internal, physical representation of data:


How and where exactly the data is stored, internal record format, access paths,
etc.
.. Conceptual Schema: What is stored?
.. Overall presentation of the data model at the logical, (if possible) DBMS and
application independent level
.. For example, in relational representation, or even higher level of abstractio
n
(e.g. E/R model)
.. External Schema: How is data presented to the user?
.. (Partial) views of the database as required by applications or users
(e.g. HR requires different views / details other than top management or the
individual employee)
.. In the case of a relational database system implemented via views

Part 1: Exercise 01 - 19
01)
Create a list of employees. All columns are relevant.
02)
Which employees have fewer than 30 remaining vacation days?
03)
Which employees have fewer than 30 remaining vacation days?
Sort the output descending by RemainderDays and - for employees with the same nu
mber of
RemainderDays - ascending by DNumber.
04)
For which employees are the remaining vacation days unknown?
05)
For which employees are the remaining vacation days known?
06)
Create a list of employees. Only columns DNumber and Name are relevant.
07)
Create a list of employees. Only columns DNumber and Name are relevant.
The output column DNumber should be renamed to "PERSNUMBER".
08)
Create a list of employees. Only columns DNumber and Name are relevant.
The output column DNumber should be renamed to "Personal Number" (with small let
ters).
09)
How many remaining vacation days has Ms D ?
10)
Which employees have exactly 10 remaining vacation days?
11)
The value of a vacation day is estimated at EUR 300.- (all-in).
The output of the employee data should be extended by the calculated value of th
e remaining
vacation days.
12)
The value of a vacation day is estimated at EUR 300.- (all-in).
Which employees have remaining vacation days with a value of at least EUR 600.-?
13)
Which numbers of remaining vacation days can be found in the company?
Each quantity number should be displayed only once.
14)
Which employees work for department "A04" and have exactly 20 remaining vacation
days?

15)
Which employees work in department "A01" or "A04"? Use OR
16)
Which employees work in department "A01" or "A04"? Use IN
17)
Which employees not working for department "A01" have exactly 10 remaining vacat
ion days?
18)
Which employee names include an "L"?
19)
Which employees have a name beginning with "F" and in the third place an "a"?

Part 2: Exercise 20 - 29
20)
What is the number of employees?
21)
How many employees have exactly 20 remaining vacation days?
22)
In how many different departments do the employees work?
23)
What is the maximum number of remaining vacation days?
24)
What is the average number of remaining vacation days?
Employees whose remaining vacation days are unknown should not be taken into acc
ount.
25)
How many remaining vacation days have the employees accumulated in total?
26)
The minimum, average, maximum, and total number of remaining vacation days shoul
d be
displayed with a single query.
27)
What is the deviation of maximum remaining vacation days to the average of remai
ning
vacation days?
28)
The total number of remaining vacation days should be displayed for each departm
ent.
29)
The total number of remaining vacation days should be displayed for each departm
ent.
Only departments with more than 30 days should be taken into account.
The display of the total number of days should be sorted descending.

Part 3: Exercise 30 - 40
30)
Which employees work for department "A01" or A04"? Use UNION.
31)
Which employees work for which department? Use an implicit JOIN without tuple va
riables.
Employees without department and departments without employees should not be tak
en into
account.
32)
Which employees work for which department? Use an implicit JOIN with tuple varia
bles.
Employees without department and departments without employees should not be tak
en into
account.
33)
Which employees work for which department? Use an explicit JOIN without tuple va
riables.
Employees without department and departments without employees should not be tak
en into
account.
34)
Which employees work for which department? Use an explicit JOIN with tuple varia
bles.
Employees without department and departments without employees should not be tak
en into
account.
35)
Which employees work for which department?
Employees without department should be included, whereas departments without emp
loyees
should be ignored.
Use a LEFT OUTER JOIN with tuple variables.
36)
Which employees work for which department?
Employees without department should be ignored, whereas departments without empl
oyees
should be included.
Use a RIGHT OUTER JOIN with tuple variables.
37)
Which employees work for which department?
Employees without department and departments without employees should be include
d.
Use a FULL OUTER JOIN with tuple variables.
38)
Which employees work for the Sales department? Use an implicit Join with tuple v
ariables.
39)
Which employees work for the Sales department? Use an explicit Join with tuple v
ariables.

40)
Which department has accumulated how many remaining vacation days?
Only colleagues born after 03.02.1972 should be taken into account.
The output of the number of days should be sorted descending.
Only departments that accumulated more than 30 remaining vacation days should be
included.

Part 4: Exercise 41 - 45
41)
Which employees work for the Sales department?
Use an uncorrelated sub query.
42)
Which employees work for the Sales department?
Use a correlated sub query.
43)
Which employees accumulated the most remaining vacation days?
Use an uncorrelated sub query with "MAX".
44)
Which employees accumulated the most remaining vacation days?
Use an uncorrelated sub query without "MAX".
45)
Which employees have above average remaining vacation days?

Part 5: Exercise 46 - 53
46)
Copy the data from table "EMPLOYEE" into the (structural equal) table "STAFF".
47)
Set the remaining vacation days of "Mr A" of table "STAFF" to 50 days.
48)
Reduce the remaining vacation days to 18 for all employees (table "STAFF"), who
have more
than 18 days.
49)
Triple the remaining vacation days of employees (table "STAFF") working for depa
rtment
"Development" or "Test".
Use an uncorrelated sub query with "IN".
50)
Create a new entry in table "STAFF".
51)
"Mr A" is leaving the company. Delete his data from table "STAFF".
52)
Delete employees from table "STAFF", where the department information is unknown
.
53)
The departments "Planning" and "Test" are closed.
Delete all employees of those departments from table "STAFF".
Use an uncorrelated sub query with "IN".

Part 6: Exercise 54 - 60
54)
Create a new column table "Locations" with columns
.. "Token"
type VARCHAR, length 3
.. "Region"
type VARCHAR, length 20
.. "Country" type VARCHAR, length 20
.. "City"
type VARCHAR, length 20
Each location is uniquely identified by "Token".
When inserting data, "Country" and "Region" can be unknown, but not the "City".
If no "Region" is specified during insert the default value "EMEA" should be use
d.
55)
Extend the newly created table "Locations" with columns
.. "Population"
type INTEGER
.. "Mayor" type VARCHAR, length 30
56)
Remove the columns "Country" and "Population" from table "Locations".
57)
Change the table definition of "Locations" that column "City" can have NULL valu
es.
58)
Delete table "Locations".
59)
Create view "ManagerView".
The manager of department "A04" should only view data of his own employees.
To avoid discrimination/favoritism the birthday should be excluded.
Rename column "RemainderDays" to "VacationDays".
60)
Delete view "ManagerView".

43

Example illustrates that transaction level isolation is not exactly the same as is
olation-level repeatable
read in single-version concurrency control:
In classical DBMSs, T2 and T4 would see V2 if they read it first after T3 is com
mitted.

Part 1: Exercise 01 - 19
01)
Create a list of employees. All columns are relevant.
02)
Which employees have fewer than 30 remaining vacation days?
03)
Which employees have fewer than 30 remaining vacation days?
Sort the output descending by RemainderDays and - for employees with the same nu
mber of
RemainderDays - ascending by DNumber.
04)
For which employees are the remaining vacation days unknown?
05)
For which employees are the remaining vacation days known?
06)
Create a list of employees. Only columns DNumber and Name are relevant.
07)
Create a list of employees. Only columns DNumber and Name are relevant.
The output column DNumber should be renamed to "PERSNUMBER".
08)
Create a list of employees. Only columns DNumber and Name are relevant.
The output column DNumber should be renamed to "Personal Number" (with small let
ters).
09)
How many remaining vacation days has Ms D ?
10)
Which employees have exactly 10 remaining vacation days?
11)
The value of a vacation day is estimated at EUR 300.- (all-in).
The output of the employee data should be extended by the calculated value of th
e remaining
vacation days.
12)
The value of a vacation day is estimated at EUR 300.- (all-in).
Which employees have remaining vacation days with a value of at least EUR 600.-?
13)
Which numbers of remaining vacation days can be found in the company?
Each quantity number should be displayed only once.
14)
Which employees work for department "A04" and have exactly 20 remaining vacation
days?

15)
Which employees work in department "A01" or "A04"? Use OR
16)
Which employees work in department "A01" or "A04"? Use IN
17)
Which employees not working for department "A01" have exactly 10 remaining vacat
ion days?
18)
Which employee names include an "L"?
19)
Which employees have a name beginning with "F" and in the third place an "a"?

Part 1: Exercise 01 - 19
01) Create a list of employees. All columns are relevant.
SELECT *
FROM Employee;
02) Which employees have fewer than 30 remaining vacation days?
SELECT *
FROM Employee
WHERE RemainderDays < 30;
03) Which employees have fewer than 30 remaining vacation days?
Sort the output descending by RemainderDays and - for employees with the same nu
mber
of RemainderDays - ascending by DNumber.
SELECT *
FROM Employee
WHERE RemainderDays < 30
ORDER BY RemainderDays DESC, DNumber ASC;
04) For which employees are the remaining vacation days unknown?
SELECT *
FROM Employee
WHERE RemainderDays IS NULL;
05) For which employees are the remaining vacation days known?
SELECT *
FROM Employee
WHERE RemainderDays IS NOT NULL;
06) Create a list of employees. Only columns DNumber and Name are relevant.
SELECT DNumber, Name
FROM Employee;
07) Create a list of employees. Only columns DNumber and Name are relevant.
The output column DNumber should be renamed to "PERSNUMBER".
SELECT DNumber AS PersNumber, Name
FROM Employee;

08) Create a list of employees. Only columns DNumber and Name are relevant.
The output column DNumber should be renamed to "Personal Number" (with small let
ters).
SELECT DNumber AS "Personal Number", Name
FROM Employee;
09) How many remaining vacation days has Ms D ?
SELECT RemainderDays
FROM Employee
WHERE Name = 'Ms D';
10) Which employees have exactly 10 remaining vacation days?
SELECT *
FROM Employee
WHERE RemainderDays = 10;
11) The value of a vacation day is estimated at EUR 300.- (all-in).
The output of the employee data should be extended by the calculated value of th
e
remaining vacation days.
SELECT *, RemainderDays * 300 AS RemainderDaysValue
FROM Employee;
12) The value of a vacation day is estimated at EUR 300.- (all-in).
Which employees have remaining vacation days with a value of at least EUR 600.-?
SELECT *, RemainderDays * 300 AS RemainderDayValue
FROM Employee
WHERE RemainderDays * 300 >= 600;
13) Which numbers of remaining vacation days can be found in the company?
Each quantity number should be displayed only once.
SELECT DISTINCT RemainderDays
FROM Employee
WHERE RemainderDays IS NOT NULL;

14) Which employees work for department "A04" and have exactly 20 remaining vaca
tion days?
SELECT *
FROM Employee
WHERE DepID = 'A04' AND RemainderDays = 20;
15) Which employees work in department "A01" or "A04"? Use OR
SELECT *
FROM Employee
WHERE DepID = 'A01' OR DepID = 'A04';
16) Which employees work in department "A01" or "A04"? Use IN
SELECT *
FROM Employee
WHERE DepID IN ('A01', 'A04');
17) Which employees not working for department "A01" have exactly 10 remaining v
acation
days?
SELECT *
FROM Employee
WHERE DepID <> 'A01' AND RemainderDays = 10;
18) Which employee names include an "L"?
SELECT *
FROM Employee
WHERE Name LIKE '%L%';
19) Which employees have a name beginning with "F" and in the third place an "a"
?
SELECT *
FROM Employee
WHERE Name LIKE 'F_a%';

Part 2: Exercise 20 - 29
20)
What is the number of employees?
21)
How many employees have exactly 20 remaining vacation days?
22)
In how many different departments do the employees work?
23)
What is the maximum number of remaining vacation days?
24)
What is the average number of remaining vacation days?
Employees whose remaining vacation days are unknown should not be taken into acc
ount.
25)
How many remaining vacation days have the employees accumulated in total?
26)
The minimum, average, maximum, and total number of remaining vacation days shoul
d be
displayed with a single query.
27)
What is the deviation of maximum remaining vacation days to the average of remai
ning vacation
days?
28)
The total number of remaining vacation days should be displayed for each departm
ent.
29)
The total number of remaining vacation days should be displayed for each departm
ent.
Only departments with more than 30 days should be taken into account.
The display of the total number of days should be sorted descending.

Part 2: Exercise 20 - 29
20) What is the number of employees?
SELECT COUNT(*)
FROM Employee;
21) How many employees have exactly 20 remaining vacation days?
SELECT COUNT(*)
FROM Employee
WHERE RemainderDays = 20;
22) In how many different departments do the employees work?
SELECT COUNT(DISTINCT DepID)
FROM Employee;
23) What is the maximum number of remaining vacation days?
SELECT MAX(RemainderDays)
FROM Employee;
24) What is the average number of remaining vacation days?
Employees whose remaining vacation days are unknown should not be taken into acc
ount.
SELECT AVG(RemainderDays)
FROM Employee;
25) How many remaining vacation days have the employees accumulated in total?
SELECT SUM(RemainderDays)
FROM Employee;
26) The minimum, average, maximum, and total number of remaining vacation days s
hould be
displayed with a single query.
SELECT MIN(RemainderDays) AS "min",
AVG(RemainderDays) AS "avg",
MAX(RemainderDays) AS "max",
SUM(RemainderDays) AS "sum"
FROM Employee;

27) What is the deviation of maximum remaining vacation days to the average of r
emaining
vacation days?
SELECT MAX(RemainderDays) - AVG(RemainderDays)
FROM Employee;
28) The total number of remaining vacation days should be displayed for each dep
artment.
SELECT DepID, SUM(RemainderDays)
FROM Employee
GROUP BY DepID;
29) The total number of remaining vacation days should be displayed for each dep
artment.
Only departments with more than 30 days should be taken into account.
The display of the total number of days should be sorted descending.
SELECT DepID, SUM(RemainderDays)
FROM Employee
GROUP BY DepID
HAVING SUM(RemainderDays) > 30
ORDER BY 2 DESC;

Part 3: Exercise 30 - 40
30)
Which employees work for department "A01" or A04"? Use UNION.
31)
Which employees work for which department? Use an implicit JOIN without tuple va
riables.
Employees without department and departments without employees should not be tak
en into
account.
32)
Which employees work for which department? Use an implicit JOIN with tuple varia
bles.
Employees without department and departments without employees should not be tak
en into
account.
33)
Which employees work for which department? Use an explicit JOIN without tuple va
riables.
Employees without department and departments without employees should not be tak
en into
account.
34)
Which employees work for which department? Use an explicit JOIN with tuple varia
bles.
Employees without department and departments without employees should not be tak
en into
account.
35)
Which employees work for which department?
Employees without department should be included, whereas departments without emp
loyees
should be ignored.
Use a LEFT OUTER JOIN with tuple variables.
36)
Which employees work for which department?
Employees without department should be ignored, whereas departments without empl
oyees
should be included.
Use a RIGHT OUTER JOIN with tuple variables.
37)
Which employees work for which department?
Employees without department and departments without employees should be include
d.
Use a FULL OUTER JOIN with tuple variables.
38)
Which employees work for the Sales department? Use an implicit Join with tuple v
ariables.
39)
Which employees work for the Sales department? Use an explicit Join with tuple v
ariables.

40)
Which department has accumulated how many remaining vacation days?
Only colleagues born after 03.02.1972 should be taken into account.
The output of the number of days should be sorted descending.
Only departments that accumulated more than 30 remaining vacation days should be
included.

Part 3: Exercise 30 - 40
30) Which employees work for department "A01" or A04"? Use UNION.
SELECT *
FROM Employee
WHERE DepID = 'A01'
UNION
SELECT *
FROM Employee
WHERE DepID = 'A04';
31) Which employees work for which department?
Use an implicit JOIN without tuple variables.
Employees without department and departments without employees should
en into
account.
SELECT Employee.Name, Department.Name
FROM Employee, Department
WHERE Employee.DepID = Department.DepID;
32) Which employees work for which department?
Use an implicit JOIN with tuple variables.
Employees without department and departments without employees should
en into
account.
SELECT e.Name, d.Name
FROM Employee e, Department d
WHERE e.DepID = d.DepID;
33) Which employees work for which department?
Use an explicit JOIN without tuple variables.
Employees without department and departments without employees should
en into
account.
SELECT Employee.Name, Department.Name
FROM Employee JOIN Department
ON Employee.DepID = Department.DepID;
34) Which employees work for which department?
Use an explicit JOIN with tuple variables.
Employees without department and departments without employees should
en into
account.
SELECT e.Name, d.Name
FROM Employee e JOIN Department d
ON e.DepID = d.DepID;

not be tak

not be tak

not be tak

not be tak

35) Which employees work for which department?


Employees without department should be included, whereas departments without emp
loyees
should be ignored.
Use a LEFT OUTER JOIN with tuple variables.
SELECT e.Name, d.Name
FROM Employee e LEFT OUTER JOIN Department d
ON e.DepID = d.DepID;
36) Which employees work for which department?
Employees without department should be ignored, whereas departments without empl
oyees
should be included.
Use a RIGHT OUTER JOIN with tuple variables.
SELECT e.Name, d.Name
FROM Employee e RIGHT OUTER JOIN Department d
ON e.DepID = d.DepID;
37) Which employees work for which department?
Employees without department and departments without employees should be include
d.
Use a FULL OUTER JOIN with tuple variables.
SELECT e.Name, d.Name
FROM Employee e FULL OUTER JOIN Department d
ON e.DepID = d.DepID;
38) Which employees work for the Sales department?
Use an implicit Join with tuple variables.
SELECT e.*
FROM Employee e, Department d
WHERE e.DepID = d.DepID AND d.Name = 'Sales';
39) Which employees work for the Sales department?
Use an explicit Join with tuple variables.
SELECT e.*
FROM Employee e JOIN Department d
ON e.DepID = d.DepID
WHERE d.Name = 'Sales';

40) Which department has accumulated how many remaining vacation days?
Only colleagues born after 03.02.1972 should be taken into account.
The output of the number of days should be sorted descending.
Only departments that accumulated more than 30 remaining vacation days should be
included.
SELECT d.Name, SUM(e.RemainderDays) AS DepartmentsVacation
FROM Employee e JOIN Department d ON e.DepID = d.DepID
WHERE e.DateOfBirth > '1972-02-03'
GROUP BY d.Name, d.DepID
HAVING SUM(e.RemainderDays) > 30
ORDER BY DepartmentsVacation DESC;

Part 4: Exercise 41 - 45
41)
Which employees work for the Sales department?
Use an uncorrelated sub query.
42)
Which employees work for the Sales department?
Use a correlated sub query.
43)
Which employees accumulated the most remaining vacation days?
Use an uncorrelated sub query with "MAX".
44)
Which employees accumulated the most remaining vacation days?
Use an uncorrelated sub query without "MAX".
45)
Which employees have above average remaining vacation days?

Part 4: Exercise 41 - 45
41) Which employees work for the Sales department?
Use an uncorrelated sub query.
SELECT *
FROM Employee
WHERE DepID = ANY (SELECT DepID
FROM Department
WHERE Name = 'Sales');
42) Which employees work for the Sales department?
Use a correlated sub query.
SELECT e.*
FROM Employee e
WHERE EXISTS (SELECT *
FROM Department d
WHERE d.DepID = e.DepID
AND d.Name = 'Sales');
43) Which employees accumulated the most remaining vacation days?
Use an uncorrelated sub query with "MAX".
SELECT *
FROM Employee
WHERE RemainderDays = (SELECT MAX(RemainderDays)
FROM Employee);
44) Which employees accumulated the most remaining vacation days?
Use an uncorrelated sub query without "MAX".
SELECT *
FROM Employee
WHERE RemainderDays >= ALL (SELECT RemainderDays
FROM Employee
WHERE RemainderDays
IS NOT NULL);
45) Which employees have above average remaining vacation days?
SELECT *
FROM Employee
WHERE RemainderDays > (SELECT AVG(RemainderDays)
FROM Employee);

Part 5: Exercise 46 - 53
46)
Copy the data from table "EMPLOYEE" into the (structural equal) table "STAFF".
47)
Set the remaining vacation days of "Mr A" of table "STAFF" to 50 days.
48)
Reduce the remaining vacation days to 18 for all employees (table "STAFF"), who
have more
than 18 days.
49)
Triple the remaining vacation days of employees (table "STAFF") working for depa
rtment
"Development" or "Test".
Use an uncorrelated sub query with "IN".
50)
Create a new entry in table "STAFF".
51)
"Mr A" is leaving the company. Delete his data from table "STAFF".
52)
Delete employees from table "STAFF", where the department information is unknown
.
53)
The departments "Planning" and "Test" are closed.
Delete all employees of those departments from table "STAFF".
Use an uncorrelated sub query with "IN".

Part 5: Exercise 46 - 53
46) Copy the data from table "EMPLOYEE" into the (structural equal) table "STAFF
".
INSERT INTO Staff SELECT * FROM Employee;
47) Set the remaining vacation days of "Mr A" of table "STAFF" to 50 days.
UPDATE Staff
SET RemainderDays = 50
WHERE Name = 'Mr A';
48) Reduce the remaining vacation days to 18 for all employees (table "STAFF"),
who have
more than 18 days.
UPDATE Staff
SET RemainderDays = 18
WHERE RemainderDays > 18;
49) Triple the remaining vacation days of employees (table "STAFF") working for
department
"Development" or "Test".
Use an uncorrelated sub query with "IN".
UPDATE Staff
SET RemainderDays = 3 * RemainderDays
WHERE DepID IN (SELECT DepID
FROM Department
WHERE Name = 'Development'
OR Name = 'Test');
50) Create a new entry in table "STAFF".
INSERT INTO Staff
VALUES ('D200000', 'Mr X', '1977-07-07', 10, 'A01');
51) "Mr A" is leaving the company. Delete his data from table "STAFF".
DELETE
FROM Staff
WHERE Name = 'Mr A';
52) Delete employees from table "STAFF", where the department information is unk
nown.
DELETE
FROM Staff
WHERE DepID IS NULL;

53) The departments "Planning" and "Test" are closed.


Delete all employees of those departments from table "STAFF".
Use an uncorrelated sub query with "IN".
DELETE
FROM Staff
WHERE DepID IN (SELECT DepID
FROM Department
WHERE Name = 'Planning'
OR Name = 'Test');

Part 6: Exercise 54 - 60
54)
Create a new column table "Locations" with columns
.. "Token"
type VARCHAR, length 3
.. "Region"
type VARCHAR, length 20
.. "Country" type VARCHAR, length 20
.. "City"
type VARCHAR, length 20
Each location is uniquely identified by "Token".
When inserting data, "Country" and "Region" can be unknown, but not the "City".
If no "Region" is specified during insert the default value "EMEA" should be use
d.
55)
Extend the newly created table "Locations" with columns
.. "Population"
type INTEGER
.. "Mayor" type VARCHAR, length 30
56)
Remove the columns "Country" and "Population" from table "Locations".
57)
Change the table definition of "Locations" that column "City" can have NULL valu
es.
58)
Delete table "Locations".
59)
Create view "ManagerView".
The manager of department "A04" should only view data of his own employees.
To avoid discrimination/favoritism the birthday should be excluded.
Rename column "RemainderDays" to "VacationDays".
60)
Delete view "ManagerView".

Part 6: Exercise 54 - 60
54) Create a new column table "Locations" with columns "Token", "Region", "Count
ry" and
"City".
Each location is uniquely identified by "Token".
When inserting data, "Country" and "Region" can be unknown, but not the "City".
If no "Region" is specified during insert the default value "EMEA" should be use
d.
CREATE COLUMN TABLE Locations
(Token VARCHAR(3) PRIMARY KEY,
Region VARCHAR(20) DEFAULT 'EMEA',
Country VARCHAR(20),
City VARCHAR(20) NOT NULL);
55) Extend the newly created table "Locations" with columns "Population" and "Ma
yor".
ALTER TABLE Locations
ADD (Population INTEGER,
Mayor VARCHAR(30));
56) Remove the columns "Country" and "Population" from table "Locations".
ALTER TABLE Locations
DROP (Country,
Population);
57) Change the table definition of "Locations" that column "City" can have NULL
values.
ALTER TABLE Locations
ALTER (City VARCHAR(20) NULL);
58) Delete table "Locations".
DROP TABLE Locations;
59) Create view "ManagerView".
The manager of department "A04" should only view data of his own employees.
To avoid discrimination/favoritism the birthday should be excluded.
Rename column "RemainderDays" to "VacationDays".
CREATE VIEW ManagerView AS
SELECT DNumber, Name, RemainderDays AS VacationDays
FROM Employee
WHERE DepID = 'A04';
60) Delete view "ManagerView".
DROP VIEW ManagerView;
End