You are on page 1of 6

GESTS482OPERATIONSANDDIGITALBUSINESSSQLEXERCISES

Exercise#1
Herearethedatacontainedinthetablesofarelationaldatabase,followedbyaSQLquerytobeexecuted
onthisdatabase.Whatwillbethedatatableresultingfromtheexecutionofthisquery?Pleasecompletethe
table below by watching to entitle the columns properly. Be sure to briefly explain your reasoning or to
detailtheintermediatesteps.Anyresponsenotorpoorlyjustifiedwillbeconsideredasnull.

Tableclients
Tablesales

c_id c_name c_country


v_client v_product v_quantity
v_date
1 Superman
USA
1
3
1
8/06/2012
2
Batman
USA
2
3
2
13/11/2012
3
Tintin
Belgium
3
1
1
14/09/2011
4
Asterix
France
5
4
3
1/08/2011
5 LuckyLuke Belgium
4
1
1
6/06/2012

2
4
2
7/07/2012
Tableproducts
3
4
4
1/05/2011

1
1
2
26/03/2012
p_id
p_name
p_category p_price
5
2
1
28/03/2011
1
Milou
Animals
100
2
2
1
1/06/2011
2
Cheval
Animals
500
3
4

Combinaison
Jeans

Clothes
Clothes

200
100

Requte
SELECT Year([v_date]) AS Year, p_category AS Category,
SUM([p_price]*[v_quantity]) AS Amount FROM products, clients, sales WHERE c_id =
v_client AND p_id = v_product AND NOT c_country="France" GROUP BY YEAR([v_date]),
p_category UNION SELECT Year([v_date]) AS Year, "Sum" AS Category,
SUM([p_price]*[v_quantity]) AS Amount FROM products, clients, sales WHERE c_id =
v_client AND p_id = v_product AND NOT c_country="France" GROUP BY YEAR([v_date]),
"Sum" ORDER BY Year, Category;

SOLUTION:

Year

Category

Amount

2011
2011
2011
2012
2012
2012

Animals
Clothes
Sum
Animals
Clothes
Sum

1100
700
1800
200
800
1000

PROF.N.VANZEEBROECKFALL2014

GESTS482OPERATIONSANDDIGITALBUSINESSSQLEXERCISES
Exercise#2
Herearethedatacontainedinthetablesofarelationaldatabase,followedbyaSQLquerytobeexecuted
onthisdatabase.Whatwillbethedatatableresultingfromtheexecutionofthisquery?Pleasecompletethe
table below by watching to entitle the columns properly. Be sure to briefly explain your reasoning or to
detailtheintermediatesteps.Anyresponsenotorpoorlyjustifiedwillbeconsideredasnull.Notethatthe
tablebelowmayprovidemorelinesorcolumnsthanwhattheexactanswerrequires.

Tableclients
Tablereservations

id_client
name
Country
id_client id_room
date
nb_nights
1
Gassner
USA
1
1
1/02/2013
2
2
Dewatripont Belgium
1
2
3/03/2013
3
3
vanZeebroeck Belgium
2
3
1/12/2012
5
4
Tudor
USA
3
2
15/01/2013
1

4
1
16/05/2013
4
TableRooms

3
3
20/07/2013
3
id_room nb_beds price
1
2
100
2
1
80
3
2
150

Requte
SELECT Beds, Amount AS Volume, Nunber AS Frequency, Amount/Number AS Average FROM
(SELECT nb_beds AS Beds, SUM([nb_nights]*[price]) AS Amount, SUM(1) AS Number
FROM clients, reservations, rooms WHERE rooms.id_room = reservations.id_room AND
clients.id_client = reservations.id_client AND Year([date])=2013 GROUP BY
nb_beds)ORDER BY Beds;

SOLUTION:

Beds

Volume

Frequency

Average

1
2

320
1050

2
3

160
350

PROF.N.VANZEEBROECKFALL2014

GESTS482OPERATIONSANDDIGITALBUSINESSSQLEXERCISES
Exercise#3
Herearethedatacontainedinthetablesofarelationaldatabase,followedbyaSQLquerytobeexecuted
onthisdatabase.Whatwillbethedatatableresultingfromtheexecutionofthisquery?Pleasecompletethe
table below by watching to entitle the columns properly. Be sure to briefly explain your reasoning or to
detailtheintermediatesteps.Anyresponsenotorpoorlyjustifiedwillbeconsideredasnull.Notethatthe
tablebelowmayprovidemorelinesorcolumnsthanwhattheexactanswerrequires.Notethatthefunction
Dates()sendbackthedateoftheday,i.e.28/05/2014,andthatitispossiblethatthetableprovidedforthe
responsecontainsmorelinesorcolumnsthanwhattheexactanswerrequires.

TableWork

TableReader
o_id o_title
o_type

1
2
3
4
5
6
7

Unbearablelightnessofthebeing
Thefoundationsofcomputing
Nostalgia,omydelices
Thefoundationsofcomputing
Thehumancondition
FateandtheNecessity
ScienceandHypothesis

Novel
Scientist
Shortstories
Scientist
Novel
Essay
Essay

l_id
1
2
3
4

l_name
NadgeFantasque
FranceLesoir
JrmyMonlivre
BrunoLelit

l_type
Student
Prof
Prof
Student

TableLoan

e_id e_reader e_work e_date


e_duration e_return
1
1
2 18/05/2014
7
0
2
4
3 19/05/2014
7
1
3
1
1 20/05/2014
2
0
4
3
4 20/05/2014
5
0
5
2
5 21/05/2014
5
0
6
3
3 22/05/2014
7
0
7
1
1 23/05/2014
7
0
8
3
6 24/05/2014
2
0
9
3
7 24/05/2014
2
1
10
2
7 26/05/2014
2
0

Requte
SELECT o_type AS Group, COUNT(e_id) AS Number, SUM(Date()-(e_date+e_duration)) AS
Days FROM Work, Loan WHERE o_id = e_work AND e_id IN (SELECT e_id FROM Loan WHERE
e_return=0 AND (Date()-(e_date+e_duration))>0) GROUP BY o_type ORDER BY
COUNT(e_id) DESC, o_type;

SOLUTION:

Group

Number

Days

Novel
Scientist
Essay

2
2
1

8
6
2

PROF.N.VANZEEBROECKFALL2014

GESTS482OPERATIONSANDDIGITALBUSINESSSQLEXERCISES
Exercise#4
DownloadandopentheAccessDBnamedStudents.mdb.PleasecreatethefollowingSQLqueriesandrun
themonthedata.

1. Displaythelistofstudents
2. Displaythenameandfirstnameofstudents<20years
3. Displaystudentswhosenamebeginwith"Ma"
4. Displaystudentssortedoutbydecreasingage
5. Displaytheaverageratingpercourse
6. Displaythetranscriptofeachstudent
7. Displaytheaverage,minandmaxofeachstudentstudentssortedoutalphabetically
8. DisplaytheratingsofECONS202lowerthantheaverageofthecourse
9. Displaystudentswithanaveragecorrespondingto"honors"(distinction)(i.e.average>=12and<14)
10. Insertanewcourse(INFOS203)inthetableCourses
11. InsertallthestudentsinthetableRatingsforthisnewcourse
12. Give12toallstudentsforthisnewcourse
13. Add1pointincomputingforallstudents
14. Raisethecomputing'sgradeofthestudentswhofailed(<10)
15. Deletetheaddedcourse
Whatistheissueraisedbythelastquery?andhowtosolveit?

SOLUTIONS:

1.Displaythelistofstudents

SELECT *
FROM students
2.Displaythenameandfirstnameofstudents<20years

SELECT nom, prenom, age


FROM students
WHERE age<20
3.Displaystudentswhosenamebeginwith"Ma"

SELECT nom, prenom


FROM students
WHERE nom LIKE 'Ma*'
4.Displaystudentssortedoutbydecreasingage

SELECT nom, prenom, age


FROM students
ORDER BY age DESC

PROF.N.VANZEEBROECKFALL2014

GESTS482OPERATIONSANDDIGITALBUSINESSSQLEXERCISES
5.Displaytheaverageratingpercourse

SELECT cours, AVG(cote)


FROM cotes
GROUP BY cours
ORDER BY cours
6.Displaythetranscriptofeachstudent

SELECT students.nom, students.prenom, cotes.cote, cours.titre


FROM students, cotes, cours
WHERE students.matricule=cotes.etudiant AND cours.mnemonique=cotes.cours
ORDER BY students.nom, students.prenom, cours.titre
7.Displaytheaverage,minandmaxofeachstudentstudentssortedoutalphabetically

SELECT students.nom, students.prenom, AVG(cotes.cote) AS moyenne,


MIN(cotes.cote) AS minimum, MAX(cotes.cote) AS maximum
FROM students, cotes
WHERE students.matricule=cotes.etudiant
GROUP BY students.nom, students.prenom
ORDER BY students.nom, students.prenom
8.DisplaytheratingsofECONS202lowerthantheaverageofthecourse(INFOS202????)

SELECT students.nom, students.prenom, cotes.cote


FROM cotes, students
WHERE students.matricule = cotes.etudiant AND cotes.cours='INFOS202' AND
cotes.cote<(SELECT AVG(cote) FROM cotes WHERE cours='INFOS202')
ORDER BY nom, prenom
9.Displaystudentswithanaveragecorrespondingto"honors"(distinction)(i.e.average>=12and<14)

SELECT students.nom, students.prenom, AVG(cotes.cote) AS moyenne


FROM students, cotes
WHERE students.matricule=cotes.etudiant
GROUP BY students.nom, students.prenom
HAVING AVG(cotes.cote)>=12 AND AVG(cotes.cote)<14
ORDER BY AVG(cotes.cote) DESC, students.nom, students.prenom
10.Insertanewcourse(INFOS203)inthetableCourses
INSERT INTO cours(mnemonique, titre, ects)
VALUES("INFOS203","Chic, encore de linformatique",5)
11.InsertallthestudentsinthetableRatingsforthisnewcourse

INSERT INTO cotes(etudiant, cours)


SELECT matricule, "INFOS203" AS cours FROM students

PROF.N.VANZEEBROECKFALL2014

GESTS482OPERATIONSANDDIGITALBUSINESSSQLEXERCISES

12.Give12toallstudentsforthisnewcourse

UPDATE cotes
SET cote=12
WHERE cours="INFOS203"
13.Add1pointincomputingforallstudents

UPDATE cotes
SET cote=cote+1
WHERE cours="INFOS202"
14.Raisethecomputing'sgradeofthestudentswhofailed(<10)

UPDATE cotes
SET cote=10
WHERE cours="INFOS202" AND cote<10
15.Deletetheaddedcourse
DELETE FROM cours
WHERE mnemonique="INFOS203"

PROF.N.VANZEEBROECKFALL2014

You might also like