SQL Test
SQL Test
ABIS Training & Consulting P.O. Box 220 B-3000 Leuven Belgium
SESSIONS - ENROLMENTS obtain session information for a certain enrolment SESSIONS.SNO = ENROLMENTS.E_SNO
PERSONS - SESSIONS obtain person information for the instructor of a certain session PERSONS.PNO = SESSIONS.SINS_PNO
PERSONS - ENROLMENTS obtain person information for a certain enrollee PERSONS.PNO = ENROLMENTS.E_PNO
19/04/2012
CID: required, alphanumeric: course number (primary key) CTITLE: required, alphanumeric: course title CDUR: required, numeric: course duration (in days).
SNO: required, numeric: session number (primary key). S_CID: optional, alphanumeric: course number (foreign key to COURSES). SDATE: optional: start date of the session. SINS_PNO: required, numeric: session instructor (foreign key to PERSONS). SCANCEL: optional: indicates if the session is cancelled (C means cancelled, empty (NULL) means not cancelled).
19/04/2012
PNO: required, numeric: person number (primary key). PNAME: optional, alphanumeric: name. P_CONO: optional, numeric: number of the company the person works for.
E_SNO: required, numeric: session number for the enrolment (foreign key to SESSIONS) (primary key together with E_PNO) E_PNO: required, numeric: the enrollee (foreign key to PERSONS) (primary key together with E_SNO) ECANCEL: optional: C when enrolment was cancelled, NULL if not cancelled.
19/04/2012
[_] [b]
SELECT PNO, COUNT(*) FROM PERSONS WHERE PNO = 2
Syntax error (PNO not guaranteed constant in group), so no result rows
[_] [c]
SELECT COUNT(*) FROM PERSONS GROUP BY PNO
Produces 19 rows
[_] [d]
SELECT PNAME FROM PERSONS INNER JOIN SESSIONS ON PNO = SINS_PNO WHERE PNO = 36
Produces three duplicate rows
[_] [e]
SELECT PNAME FROM PERSONS LEFT OUTER JOIN ENROLMENTS ON PNO = E_PNO WHERE PNO = 2 GROUP BY PNAME
Even for non-enrollees (PNO=2), PNAME will be shown because of the LEFT OUTER JOIN; the group by moreover guarantees that no duplicate rows will appear.
[_] [f]
SELECT SUM(CDUR) FROM COURSES, SESSIONS, ENROLMENTS WHERE CID = S_CID AND SNO = E_SNO GROUP BY CID
A row will be produced per course, containing its total number of course days.
19/04/2012
2.
0, or syntax error 2
Because two queries
8
7 unique values from the first query, and additionally the value 17 from the second. UNION removes duplicates, even if all of them come from the first query.
10
7+3 (so overlap is not eliminated, but duplicates are)
15
14+1 (union all would give this result)
17
14 (from enrolments) + 3 (from sessions)
19/04/2012
3.
Which queries produce the following table as the result? [3 correct answers.]
PNO 1 3 4 7 13 15 18 36 45 1 2 3 36 PNAME SMITHS DE KEYSER HEBBELYNCK DE WINDT PARKER DEHEM GELADE ADAMSON MOORS SMITHS TAVERNIER DE KEYSER ADAMSON ENROLLEE ENROLLEE ENROLLEE ENROLLEE ENROLLEE ENROLLEE ENROLLEE ENROLLEE ENROLLEE INSTRUCTOR INSTRUCTOR INSTRUCTOR INSTRUCTOR
[_] [a]
SELECT PNO, PNAME, 'ENROLLEE OR INSTRUCTOR' FROM PERSONS, SESSIONS, ENROLMENTS WHERE PNO = SINS_PNO AND PNO = E_PNO ORDER BY 3, 1
In this case, ENROLLEE OR INSTRUCTOR would go literally into column three.
[_] [b]
SELECT PNO, PNAME, CASE PNO WHEN E_PNO THEN 'ENROLLEE' ELSE 'INSTRUCTOR' END FROM PERSONS, SESSIONS, ENROLMENTS WHERE PNO = SINS_PNO AND PNO = E_PNO ORDER BY 3, 1
This query only gives instructors who are enrolled in their own session.
[_] [c]
SELECT PNO, PNAME, 'INSTRUCTOR' FROM PERSONS WHERE PNO IN (SELECT SINS_PNO FROM SESSIONS) UNION ALL SELECT PNO, PNAME, 'ENROLLEE' FROM PERSONS INNER JOIN ENROLMENTS ON PNO = E_PNO ORDER BY 3, 1
The fist query gives indeed the last four rows, but the second query produces duplicate rows.
[_] [d]
SELECT DISTINCT PNO, PNAME, 'INSTRUCTOR' FROM PERSONS, SESSIONS WHERE PNO = SINS_PNO UNION ALL SELECT PNO, PNAME, 'ENROLLEE' FROM PERSONS WHERE PNO IN (SELECT E_PNO FROM ENROLMENTS) ORDER BY 3, 1
DISTINCT removes the duplicates from the first query, the second one does not produce duplicates.
[_] [e]
19/04/2012
PNO, PNAME, 'ENROLLEE' PERSONS PNO IN (SELECT E_PNO FROM ENROLMENTS) ORDER BY 3, 1
Duplicates from the first query are removed by the use of UNION instead of UNION ALL.
[_] [f]
SELECT FROM WHERE UNION SELECT FROM WHERE DISTINCT PNO, PNAME, 'INSTRUCTOR' PERSONS, SESSIONS PNO = SINS_PNO
PNO, PNAME, 'ENROLLEE' PERSONS EXISTS (SELECT E_PNO FROM ENROLMENTS WHERE E_PNO = PERSONS.PNO) ORDER BY 3, 1
The correlated subquery produces all enrollees.
4.
0, or syntax error 1 9
The number of different values in column E_PNO of ENROLMENTS (would be correct when using INNER JOIN)
11
The number of non-cancelled ENROLMENTS
14
The number of rows of ENROLMENTS (would be correct with INNER JOIN and without DISTINCT)
19
The number of rows of PERSONS (because left outer join)
19/04/2012
5.
Give the number of all sessions for which none of the enrolments have been cancelled.
[_] [a]
SELECT FROM WHERE AND DISTINCT SNO SESSIONS, ENROLMENTS SNO = E_SNO ECANCEL IS NULL
[_] [b]
SELECT FROM WHERE AND DISTINCT SNO SESSIONS, ENROLMENTS SNO = E_SNO ECANCEL IS NOT NULL
Gives the sessions with at least 1 cancelled enrolment; i.e., the complement of what is asked for.
[_] [c]
SELECT SNO FROM SESSIONS WHERE SNO NOT IN (SELECT E_SNO FROM ENROLMENTS WHERE ECANCEL IS NOT NULL)
[_] [d]
SELECT SNO FROM SESSIONS WHERE SNO IN (SELECT E_SNO FROM ENROLMENTS WHERE ECANCEL IS NULL)
Gives the sessions for which there is at least 1 non-cancelled enrolment.
[_] [e]
SELECT SNO FROM SESSIONS WHERE NOT EXISTS (SELECT FROM WHERE AND
[_] [f]
SELECT SNO FROM SESSIONS INNER JOIN ENROLMENTS ON SNO = E_SNO WHERE ECANCEL IS NULL
Gives the sessions for which there is at least 1 non-cancelled enrolment.
[_] [g]
SELECT SNO FROM SESSIONS INNER JOIN ENROLMENTS ON SNO = E_SNO WHERE ECANCEL IS NOT NULL
Gives the sessions with at least 1 cancelled enrolment; i.e., the complement of what is asked for.
19/04/2012
6.
Which queries produce the following table all enrollees? [3 correct answers.]
PNAME SMITHS DE KEYSER HEBBELYNCK VAN HEIJKOOP DE WINDT PARKER DEHEM GELADE MOORS
[_] [a]
SELECT PNAME FROM PERSONS WHERE PNO IN (SELECT E_PNO FROM ENROLMENTS)
[_] [b]
SELECT PNAME FROM PERSONS, ENROLMENTS WHERE PNO = E_PNO
Result table will contain duplicates.
[_] [c]
SELECT PNAME FROM PERSONS WHERE PNO = ANY (SELECT E_PNO FROM ENROLMENTS)
[_] [d]
SELECT PNAME FROM PERSONS WHERE EXISTS (SELECT E_PNO FROM ENROLMENTS)
This is not a correlated subquery; will show all persons.
[_] [e]
SELECT PNAME FROM PERSONS INNER JOIN ENROLMENTS ON PNO = E_PNO WHERE E_PNO IS NOT NULL
Result table will contain duplicates.
[_] [f]
SELECT PNAME FROM PERSONS LEFT OUTER JOIN ENROLMENTS ON PNO = E_PNO GROUP BY PNAME
Result table will contain everybody, including non-enrollees.
19/04/2012
10
[_] [g]
SELECT PNAME FROM PERSONS RIGHT OUTER JOIN ENROLMENTS ON PNO = E_PNO GROUP BY PNAME
By using group by, duplicates are avoided.
7.
O (a)
Give per instructor the number of sessions he teaches. Give also the company where he is employed.
Counting happens in the table PERSONS; so this cannot count sessions.
O (b) O (c)
Give the number of sessions per course, and also the company where the instructor is employed.
For this purpose, the COURSES table or at least the column s_cid has to be consulted.
O (d)
19/04/2012
11
8.
Which ones of the following queries are equivalent to this query? [2 correct answers.]
SELECT PNAME FROM PERSONS WHERE PNO = (SELECT MAX(PNO) FROM PERSONS )
This gives the name of the person with the highest PNO value.
[_] [a]
SELECT PNAME FROM PERSONS WHERE PNO >= ANY (SELECT PNO FROM PERSONS)
This gives everybody, since the condition is always true.
[_] [b]
SELECT PNAME FROM PERSONS WHERE PNO >= ALL (SELECT PNO FROM PERSONS)
The condition is only true for the highest PNO.
[_] [c]
SELECT PNAME FROM PERSONS AS P1 WHERE EXISTS (SELECT MAX(PNO) FROM PERSONS AS P2 WHERE P1.PNO = P2.PNO)
The condition is always true: the correlated subquery generates always 1 row, for which max(pno) = p1.pno
[_] [d]
SELECT PNAME, MAX(PNO) FROM PERSONS GROUP BY PNAME
All groups consist of 1 row, so this generates all persons (with their own PNO)
[_] [e]
SELECT P1.PNAME FROM PERSONS AS P1 LEFT OUTER JOIN PERSONS AS P2 ON P1.PNO < P2.PNO GROUP BY P1.PNO, P1.PNAME HAVING COUNT(P2.PNO) = 0
This self-join generates larger groups as a person has a smaller PNO. Only the person with the highest PNOhas an empty group. An INNER JOIN wouldnt contain this group, hence the LEFT OUTER JOIN.
19/04/2012
12
9.
Give the list of all courses, also those for which no session has been planned yet. Give also all corresponding session numbers and the date on which each session starts.
[_] [a]
SELECT CID, SNO, SDATE FROM COURSES, SESSIONS WHERE CID = S_CID
This only generates a list of planned courses.
[_] [b]
SELECT CID, SNO, SDATE FROM COURSES INNER JOIN SESSIONS ON CID = S_CID
This only generates a list of planned courses.
[_] [c]
SELECT CID, SNO, SDATE FROM COURSES LEFT OUTER JOIN SESSIONS ON CID = S_CID
The LEFT OUTER JOIN guarantees the listing of all courses, including the non-planned.
[_] [d]
SELECT CID, SNO, SDATE FROM COURSES RIGHT OUTER JOIN SESSIONS ON CID = S_CID
This Right Outer Join guarantees all sessions, not all courses.
[_] [e]
SELECT CID, SNO, SDATE FROM COURSES, SESSIONS WHERE CID = S_CID UNION ALL SELECT CID, 0, CAST(NULL AS DATE) FROM COURSES WHERE CID NOT IN (SELECT S_CID FROM SESSIONS WHERE S_CID IS NOT NULL)
The first query lists all planned courses, the second all non-planned. The extra condition IS NOT NULL is necessary since the column S_CID could contain NULLs: the NOT IN condition would be perpetually false is the list generated in the subquery would contain a NULL.
[_] [f]
SELECT CID, SNO, SDATE FROM COURSES, SESSIONS WHERE CID = S_CID UNION ALL SELECT CID, 0, NULL FROM COURSES WHERE CID NOT IN (SELECT S_CID FROM SESSIONS)
The first query lists all the planned courses, the second all the non-planned ones.
19/04/2012
13
[_] [g]
SELECT CID, SNO, SDATE FROM COURSES, SESSIONS WHERE CID = S_CID UNION ALL SELECT S_CID, SNO, SDATE FROM SESSIONS WHERE S_CID IS NULL
The first query lists all planned courses, but the second doesnt list anything since the S_CID is never NULL for the current content of the table..
19/04/2012
14
10.
Give the names of all instructors who have in addition also followed more than 1 course.
O (a)
SELECT PNAME FROM PERSONS WHERE PNO IN (SELECT E_PNO FROM ENROLMENTS, SESSIONS WHERE E_SNO = SNO AND E_PNO = SINS_PNO AND ECANCEL IS NULL AND SCANCEL IS NULL GROUP BY E_PNO HAVING COUNT(*) > 1)
The subquery lists all instructors who were enrolled for their own sessions.
O (b)
SELECT PNAME FROM PERSONS WHERE PNO IN (SELECT FROM WHERE AND
SINS_PNO SESSIONS SCANCEL IS NULL SNO IN (SELECT E_SNO FROM ENROLMENTS WHERE ECANCEL IS NULL GROUP BY E_PNO HAVING COUNT(*) > 1))
Syntax error in the innermost subquery: E_SNO E_SNO not guaranteed constant in group. GROUP BY E_SNO instead of E_PNO would list all instructors of sessions having more than one not cancelled enrollment.
O (c)
SELECT FROM WHERE AND AND PNAME PERSONS, ENROLMENTS, SESSIONS E_PNO = PNO E_SNO = SNO PNO IN (SELECT SINS_PNO FROM SESSIONS) AND SCANCEL IS NULL AND ECANCEL IS NULL GROUP BY E_PNO, PNAME HAVING COUNT(*) > 1
GROUP BY PNAME only would be as accurate, since there could be two instructors with the same name.
O (d)
SELECT PNAME FROM PERSONS INNER JOIN ENROLMENTS ON PNO = E_PNO INNER JOIN SESSIONS S1 ON E_SNO = S1.SNO INNER JOIN SESSIONS S2 ON PNO = S1.SINS_PNO WHERE S1.SCANCEL IS NULL AND ECANCEL IS NULL GROUP BY E_PNO, PNAME HAVING COUNT(*) > 1
Instructors who gave more than one session, but only followed a single session themselves, are nonetheless listed.
19/04/2012
15
11.
Query can be executed and makes sense (according to the table and column definitions). Query can be executed but returns nonsense.
Query cannot be executed (gives a syntax error). Query can be executed and makes sense (according to the table and column definitions). Query can be executed but returns nonsense.
P_CONO and E_PNO dont have the same meaning, even though syntactically thers nothing wrong.
Query cannot be executed (gives a syntax error). Query can be executed and makes sense (according to the table and column definitions).
This query lists the last session of each course, including date and course number.
Query can be executed and makes sense (according to the table and column definitions). Query can be executed but returns nonsense.
19/04/2012
16
15.
Query cannot be executed (gives a syntax error). Query can be executed and makes sense (according to the table and column definitions).
The subquery generates a single number and as such can be used as a scalar subquery. The entire query produces also only one number, specifically the percentage of sessions for course number 8000 in relation to the total of all organised sessions..
19/04/2012
17
EVALUATION.
Here are the correct answers to all questions: 1. 2. 3. 4. 5. 6. 7. 8. 9. ae c def f ce acg d be cef
10. c 11. a 12. c 13. b 14. a 15. b Give yourself 1 point for each correctly answered question; for multiple answer questions, all answers should be correct. When your score is 80% or above, you are already an SQL expert. When your score is between 50% and 80%, following the course SQL workshop will allow you to refine your SQL knowledge. When your score is less than 50%, following the course SQL workshop is advisable. You will get a high return from this course. Be sure your basic SQL knowledge is sufficient: fill out the corresponding self-test (see PDF file) to verify this.
19/04/2012
18