RAML in Action - Relational Algebra and SQL Examples PDF
RAML in Action - Relational Algebra and SQL Examples PDF
RAML in Action:
Relational Algebra and SQL Examples
Artem Chebotko, Ph.D.
Abstract
This document presents a collection of relational algebra and SQL examples that can be useful for
students learning relational databases. It also serves as a demonstration of RAML and RAML Render.
Table Of Contents
1 Database Schema
2 Database Instance
3 SQL DDL Scripts
4 Relational Queries
Projection: Q1 Q2 Q3
Selection: Q4 Q5 Q6 Q7 Q8
Renaming: Q9 Q10
Cartesian Product: Q11 Q12
Inner Join: Q13 Q14 Q15 Q16 Q17
Outer Join: Q18 Q19
Union: Q20 Q21 Q22
Intersection: Q23 Q24 Q25
Set Difference: Q26 Q27 Q28
Division Q29 Q30 Q31
Self-Join: Q32 Q33 Q34
Aggregates, Grouping, Sorting: Q35 Q36 Q37 Q38 Q39 Q40
1 Database Schema
Relation schemas:
Transcript (sid: INT, cid: INT, pid: INT, semester: VARCHAR(9), year: YEAR, grade: CHAR(1))
SQL DDL:
2 Database Instance
Student
sid name address major
101 Nathan Edinburg CS
105 Hussein Edinburg IT
103 Jose McAllen CE
102 Wendy Mission CS
104 Maria Pharr CS
106 Mike Edinburg CE
107 Lily McAllen NULL
r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 2/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples
Professor
pid name department
201 Artem CS
203 John CS
202 Virgil MATH
204 Pearl CS
205 Christine CS
Course
cid title credits area
4333 Database 3 DB
1201 Comp literacy 2 INTRO
6333 Advanced Database 3 DB
6315 Applied Database 3 DB
3326 Java 3 PL
1370 CS I 4 INTRO
Transcript
sid cid pid semester year grade
101 4333 201 Spring 2009 A
101 6333 201 Fall 2009 A
101 6315 201 Fall 2009 A
103 4333 203 Summer I 2010 B
102 4333 201 Fall 2009 A
103 3326 204 Spring 2008 A
104 1201 205 Fall 2009 B
104 1370 203 Summer II 2010 A
106 1201 205 Fall 2009 C
106 1370 203 Summer II 2010 C
105 3326 204 Spring 2001 A
105 6315 203 Fall 2008 A
4 Relational Queries
Q1. Find all information about courses
RA:
Course
SQL:
SELECT *
FROM Course;
Result:
+------+-------------------+---------+-------+
| cid | title | credits | area |
+------+-------------------+---------+-------+
| 1201 | Comp literacy | 2 | INTRO |
| 1370 | CS I | 4 | INTRO |
| 3326 | Java | 3 | PL |
| 4333 | Database | 3 | DB |
| 6315 | Applied Database | 3 | DB |
| 6333 | Advanced Database | 3 | DB |
+------+-------------------+---------+-------+
RA:
πtitle(Course)
SQL:
SELECT title
FROM Course;
Result:
+-------------------+
| title |
+-------------------+
| Comp literacy |
| CS I |
| Java |
| Database |
| Applied Database |
| Advanced Database |
+-------------------+
r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 4/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples
+-------------------+
RA:
πarea(Course)
SQL:
Result:
+-------+
| area |
+-------+
| INTRO |
| PL |
| DB |
+-------+
RA:
πsid (σmajor='CS'(Student))
SQL:
SELECT sid
FROM Student
WHERE major = 'CS';
Result:
+-----+
| sid |
+-----+
| 101 |
| 102 |
| 104 |
+-----+
RA:
SQL:
SELECT sid
FROM Student
WHERE major = 'CS' OR major = 'CE';
Result:
+-----+
| sid |
+-----+
| 101 |
| 102 |
| 103 |
| 104 |
| 106 |
+-----+
RA:
SQL:
SELECT sid
FROM Student
WHERE major = 'CS' AND address = 'Edinburg';
Result:
+-----+
| sid |
+-----+
| 101 |
+-----+
RA:
r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 6/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples
SQL:
SELECT sid
FROM Student
WHERE major IS NULL;
Result:
+-----+
| sid |
+-----+
| 107 |
+-----+
Q8. Find titles of all graduate courses (cid is 6000 or higher) in the DB area
RA:
πtitle(σcid≥6000 ∧ area='DB'(Course))
SQL:
SELECT title
FROM Course
WHERE cid >= 6000 AND area = 'DB';
Result:
+-------------------+
| title |
+-------------------+
| Applied Database |
| Advanced Database |
+-------------------+
Q9. Find all course identifiers and show (rename) them as course numbers
RA:
ρcid→CourseNumber(πcid (Course))
πCourseNumber(ρcid→CourseNumber(Course))
r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 7/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples
SQL:
Result:
+--------------+
| CourseNumber |
+--------------+
| 1201 |
| 1370 |
| 3326 |
| 4333 |
| 6315 |
| 6333 |
+--------------+
Q10. Find all course identifiers with their respective course areas and show (rename) them as
course numbers and groups
RA:
SQL:
Result:
+--------------+-------------+
| CourseNumber | CourseGroup |
+--------------+-------------+
| 1201 | INTRO |
| 1370 | INTRO |
| 3326 | PL |
| 4333 | DB |
| 6315 | DB |
| 6333 | DB |
+--------------+-------------+
RA:
SQL:
Result:
+-----+-----+
| sid | pid |
+-----+-----+
| 101 | 201 |
| 101 | 202 |
| 101 | 203 |
| 101 | 204 |
| 101 | 205 |
| 102 | 201 |
| 102 | 202 |
| 102 | 203 |
| 102 | 204 |
| 102 | 205 |
| 103 | 201 |
| 103 | 202 |
| 103 | 203 |
| 103 | 204 |
| 103 | 205 |
| 104 | 201 |
| 104 | 202 |
| 104 | 203 |
| 104 | 204 |
| 104 | 205 |
r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 9/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples
| 105 | 201 |
| 105 | 202 |
| 105 | 203 |
| 105 | 204 |
| 105 | 205 |
| 106 | 201 |
| 106 | 202 |
| 106 | 203 |
| 106 | 204 |
| 106 | 205 |
| 107 | 201 |
| 107 | 202 |
| 107 | 203 |
| 107 | 204 |
| 107 | 205 |
+-----+-----+
RA:
SQL:
Result:
+-------+-------+
| cssid | cesid |
+-------+-------+
| 101 | 103 |
| 102 | 103 |
| 104 | 103 |
| 101 | 106 |
| 102 | 106 |
| 104 | 106 |
+-------+-------+
Q13. Find identifiers of all courses that the student with name Nathan took
RA:
πcid ((σname='Nathan'(Student))⋈Transcript)
πcid ((σname='Nathan'(Student))⋈sid=sid2(ρsid→sid2(Transcript)))
SQL:
Result:
+------+
| cid |
+------+
| 4333 |
| 6315 |
| 6333 |
+------+
Q14. Find titles of all courses that the student with name Nathan took
RA:
πtitle((σname='Nathan'(Student))⋈Transcript⋈Course)
SQL:
Result:
+-------------------+
| title |
+-------------------+
| Advanced Database |
| Applied Database |
| Database |
+-------------------+
Q15. Find course titles and professor names for courses that the student with name Nathan
took
RA:
SQL:
Result:
+-------------------+-------+
| title | name |
+-------------------+-------+
| Database | Artem |
| Applied Database | Artem |
| Advanced Database | Artem |
+-------------------+-------+
Q16. Find names of students who took a course with professor Artem
RA:
πname((πpid (σname='Artem'(Professor)))⋈Transcript⋈Student)
πname(Student⋈(πpid (σname='Artem'(Professor)))⋈Transcript)
πname(Student⋈Transcript⋈(πpid (σname='Artem'(Professor))))
πname((πpid (σname='Artem'(Professor)))⋈Student⋈Transcript)
πname(Transcript⋈(πpid (σname='Artem'(Professor)))⋈Student)
πname(Transcript⋈Student⋈(πpid (σname='Artem'(Professor))))
SQL:
Result:
+--------+
| name |
+--------+
| Nathan |
| Wendy |
+--------+
RA:
πname(Professor⋈Transcript)
SQL:
Result:
+-----------+
| name |
+-----------+
| Artem |
| John |
| Pearl |
| Christine |
+-----------+
r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 13/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples
+-----------+
Q18. Find names of professors and titles of courses they taught (if any); a professor must be in
the result even if she or he did not teach any courses
RA:
πname, title(Professor⟕(Transcript⋈Course))
πname, title((Transcript⋈Course)⟖Professor)
SQL:
Result:
+-----------+-------------------+
| name | title |
+-----------+-------------------+
| Artem | Database |
| Artem | Applied Database |
| Artem | Advanced Database |
| Virgil | NULL |
| John | Database |
| John | CS I |
| John | Applied Database |
| Pearl | Java |
| Christine | Comp literacy |
+-----------+-------------------+
Q19. Find all departments and students who major in these departments (the CS, CE, and IT
majors are all offered by the CS department); students with undeclared majors and
departments with no students must also be listed
RA:
πdepartment,
sid ((πdepartment(Professor))⟗department=major ∨ (department='CS' ∧ major='CE') ∨ (department='CS' ∧ major='IT')Student)
SQL:
Result:
+------------+------+
| department | sid |
+------------+------+
| CS | 101 |
| CS | 102 |
| CS | 103 |
| CS | 104 |
| CS | 105 |
| CS | 106 |
| MATH | NULL |
| NULL | 107 |
+------------+------+
RA:
πsid ((σmajor='CS'(Student))⋃(σmajor='IT'(Student)))
SQL:
SELECT sid
FROM Student
WHERE major = 'CS'
UNION
SELECT sid
FROM Student
WHERE major = 'IT';
SELECT sid
FROM Student
WHERE major = 'CS' OR major = 'IT';
Result:
+-----+
| sid |
+-----+
| 101 |
| 102 |
| 104 |
| 105 |
+-----+
RA:
πname(Professor⋈Transcript⋈((σarea='INTRO'(Course))⋃(σarea='DB'(Course))))
πname(Professor⋈Transcript⋈(σarea='INTRO' ∨ area='DB'(Course)))
SQL:
SELECT name
FROM Professor P, Transcript T, Course C
WHERE area = 'INTRO' AND P.pid = T.pid AND T.cid = C.cid
UNION
SELECT name
FROM Professor P, Transcript T, Course C
WHERE area = 'DB' AND P.pid = T.pid AND T.cid = C.cid;
SELECT name
FROM Professor NATURAL JOIN Transcript NATURAL JOIN Course
WHERE area = 'INTRO'
UNION
SELECT name
FROM Professor NATURAL JOIN Transcript NATURAL JOIN Course
WHERE area = 'DB';
Result:
+-----------+
| name |
+-----------+
| John |
| Christine |
| Artem |
+-----------+
RA:
πname(Professor⋈((σyear='2009'(Transcript))⋃(σyear='2010'(Transcript))))
πname(Professor⋈(σyear='2009' ∨ year='2010'(Transcript)))
SQL:
SELECT name
FROM Professor P, Transcript T
WHERE year = '2009' AND P.pid = T.pid
UNION
SELECT name
FROM Professor P, Transcript T
WHERE year = '2010' AND P.pid = T.pid;
SELECT name
FROM Professor NATURAL JOIN Transcript
WHERE year = '2009'
UNION
SELECT name
FROM Professor NATURAL JOIN Transcript
WHERE year = '2010';
Result:
+-----------+
| name |
+-----------+
| Artem |
| Christine |
| John |
+-----------+
Q23. Find names of professors who taught both INTRO and DB courses
RA:
SQL:
Result:
+------+
| name |
+------+
| John |
+------+
Q24. Find names of professors who taught in both 2009 and 2010
RA:
SQL:
Result:
+------+
| name |
+------+
+------+
Q25. Find names of professors who taught both Database and Advanced Database courses in
2009
RA:
SQL:
(SELECT pid
FROM Transcript NATURAL JOIN Course
WHERE year = '2009' AND title = 'Database') P1
NATURAL JOIN
(SELECT pid
FROM Transcript NATURAL JOIN Course
WHERE year = '2009' AND title = 'Advanced Database') P2;
Result:
+-------+
| name |
+-------+
| Artem |
+-------+
Q26. Find titles of courses that were never taught by the professor with id 204
RA:
SQL:
SELECT title
FROM Course
WHERE cid NOT IN
(
SELECT C.cid
FROM Course C, Transcript T
WHERE T.pid = 204 AND C.cid = T.cid
);
SELECT title
FROM Course
WHERE cid NOT IN
(
SELECT cid
FROM Course NATURAL JOIN Transcript
WHERE pid = 204
);
Result:
+-------------------+
| title |
+-------------------+
r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 21/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples
| Advanced Database |
| Applied Database |
| Comp literacy |
| CS I |
| Database |
+-------------------+
Q27. Find names of professors who did not teach any course taken by an IT student
RA:
SQL:
Result:
+-----------+
| name |
+-----------+
| Artem |
| Virgil |
| Christine |
+-----------+
RA:
SQL:
SELECT cid
FROM Course
WHERE cid NOT IN
(
SELECT cid
FROM Transcript T
WHERE semester = 'Fall' AND year = '2009'
);
Result:
+------+
| cid |
+------+
| 1370 |
| 3326 |
+------+
RA:
SQL:
SELECT P.pid
FROM Professor P
WHERE NOT EXISTS
(
SELECT C.cid
FROM Course C
WHERE C.area = 'DB'
AND C.cid NOT IN
(
SELECT T.cid
FROM Transcript T
WHERE T.pid = P.pid
)
);
Result:
+-----+
| pid |
+-----+
| 201 |
+-----+
Q30. Find names of students who took every course in the INTRO area
RA:
SQL:
Result:
+-------+
| name |
+-------+
| Maria |
| Mike |
+-------+
RA:
SQL:
SELECT P.pid
FROM Professor P
WHERE NOT EXISTS
(
SELECT C.cid
FROM Course C
WHERE C.cid NOT IN
(
SELECT T.cid
FROM Transcript T
WHERE T.pid = P.pid
)
);
Result:
+-----+
| pid |
+-----+
+-----+
Q32. Find professors who taught at least two (>=2) different courses
RA:
SQL:
SELECT pid
FROM Transcript
GROUP BY pid
HAVING COUNT(DISTINCT cid) >=2;
Result:
+-----+
| pid |
+-----+
| 203 |
| 201 |
+-----+
Q33. Find professors who taught at most two (0, 1, or 2) different courses
RA:
SQL:
SELECT pid
FROM Professor
WHERE pid NOT IN
(
SELECT T1.pid
FROM Transcript T1, Transcript T2, Transcript T3
WHERE T1.pid = T2.pid AND T2.pid = T3.pid AND
T1.cid <> T2.cid AND T1.cid <> T3.cid AND T2.cid <> T3.cid
);
SELECT pid
FROM Professor
WHERE pid NOT IN
(
SELECT T1.pid
FROM Transcript T1 JOIN Transcript T2 JOIN Transcript T3
ON (T1.pid = T2.pid AND T2.pid = T3.pid AND
T1.cid <> T2.cid AND T1.cid <> T3.cid AND T2.cid <> T3.cid)
);
SELECT pid
FROM Professor
WHERE pid NOT IN
(
SELECT pid
FROM Transcript
GROUP BY pid
HAVING COUNT(DISTINCT cid) >=3
);
Result:
+-----+
| pid |
+-----+
| 202 |
| 204 |
| 205 |
+-----+
Q34. Find professors who taught exactly two (2) different courses
RA:
SQL:
SELECT pid
FROM Transcript
GROUP BY pid
HAVING COUNT(DISTINCT cid) = 2;
Result:
+-----+
| pid |
+-----+
+-----+
SQL:
Result:
+----------+
| COUNT(*) |
+----------+
| 4|
+----------+
Q36. Find the total number of courses offered in each semester-year; sort the result in
descending-year and ascending-semester order
SQL:
Result:
+-----------+------+-----------------+
| semester | year | NumberOfCourses |
+-----------+------+-----------------+
| Summer I | 2010 | 1|
| Summer II | 2010 | 1|
| Fall | 2009 | 4|
| Spring | 2009 | 1|
| Fall | 2008 | 1|
| Spring | 2008 | 1|
| Spring | 2001 | 1|
+-----------+------+-----------------+
Q37. Find the total number of credits completed by student with id 101
SQL:
SELECT SUM(credits)
FROM Transcript NATURAL JOIN Course
WHERE sid = 101;
Result:
+--------------+
| SUM(credits) |
+--------------+
| 9|
+--------------+
Q38. Find the total number of credits completed per student (output student names); sort the
result in descending-credits order
SQL:
Result:
+---------+------------------+
| name | CompletedCredits |
+---------+------------------+
| Nathan | 9 |
| Jose |6 |
| Maria | 6 |
| Hussein | 6 |
| Mike |6 |
| Wendy | 3 |
| Lily |0 |
+---------+------------------+
Q39. Find the student with the largest number of completed credits
SQL:
Result:
+-----+----------------+
| sid | LargestCredits |
+-----+----------------+
| 101 | 9|
+-----+----------------+
Q40. Find the student with the second largest number of completed credits
SQL:
Result:
+-----+----------------------+
| sid | SecondLargestCredits |
+-----+----------------------+
| 103 | 6|
| 104 | 6|
| 105 | 6|
| 106 | 6|
+-----+----------------------+
6 Report an Error
Please report any typos and errors to the author - Artem Chebotko; artem [at] cs [dot] panam [dot]
edu.