0% found this document useful (0 votes)
109 views

RAML in Action - Relational Algebra and SQL Examples PDF

This document presents examples of relational algebra and SQL queries on a sample database of students, professors, courses, and transcripts. It includes the database schema with four tables - Student, Professor, Course, and Transcript. The schema defines the attributes and foreign key relationships. Sample data is inserted into the tables. The document then provides 40 relational algebra and SQL queries to demonstrate various operations like projection, selection, joins, aggregation etc. It is intended to help students learn relational databases and SQL.

Uploaded by

Gautam Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

RAML in Action - Relational Algebra and SQL Examples PDF

This document presents examples of relational algebra and SQL queries on a sample database of students, professors, courses, and transcripts. It includes the database schema with four tables - Student, Professor, Course, and Transcript. The schema defines the attributes and foreign key relationships. Sample data is inserted into the tables. The document then provides 40 relational algebra and SQL queries to demonstrate various operations like projection, selection, joins, aggregation etc. It is intended to help students learn relational databases and SQL.

Uploaded by

Gautam Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

RAML in Action:
Relational Algebra and SQL Examples
Artem Chebotko, Ph.D.

Status of this document: Public Release


Date Updated: September 4, 2012

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

5 Note to Instructors and Trainers


6 Report an Error

1 Database Schema
Relation schemas:

Student (sid: INT, name: VARCHAR(20), address: VARCHAR(20), major: CHAR(2))


Professor (pid: INT, name: VARCHAR(20), department: VARCHAR(10))
Course (cid: INT, title: VARCHAR(20), credits: INT, area: VARCHAR(5))

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 1/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Transcript (sid: INT, cid: INT, pid: INT, semester: VARCHAR(9), year: YEAR, grade: CHAR(1))

Foreign key constraints:

Transcript (sid) references Student (sid)


Transcript (cid) references Course (cid)
Transcript (pid) references Professor (pid)

SQL DDL:

CREATE TABLE Student (


sid INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
address VARCHAR(20) NOT NULL,
major CHAR(2)
);

CREATE TABLE Professor (


pid INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
department VARCHAR(10) NOT NULL
);

CREATE TABLE Course (


cid INT PRIMARY KEY,
title VARCHAR(20) NOT NULL UNIQUE,
credits INT NOT NULL,
area VARCHAR(5) NOT NULL
);

CREATE TABLE Transcript (


sid INT,
cid INT,
pid INT,
semester VARCHAR(9),
year YEAR,
grade CHAR(1) NOT NULL,
PRIMARY KEY (sid, cid, semester, year),
FOREIGN KEY (sid) REFERENCES Student (sid),
FOREIGN KEY (cid) REFERENCES Course (cid),
FOREIGN KEY (pid) REFERENCES Professor (pid)
);

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

3 SQL DDL Scripts


MySQL: mysqluniversity.sql
Oracle: oracleuniversity.sql (Note that the SQL queries were not tested in Oracle - coming soon)

4 Relational Queries
Q1. Find all information about courses

RA:

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 3/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Course

πcid, title, credits, area(Course)

SQL:

SELECT *
FROM Course;

SELECT cid, title, credits, area


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 |
+------+-------------------+---------+-------+

Q2. Find titles of all courses

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

+-------------------+

Q3. Find all areas in which courses are offered

RA:

πarea(Course)

SQL:

SELECT DISTINCT area


FROM Course;

Result:

+-------+
| area |
+-------+
| INTRO |
| PL |
| DB |
+-------+

Q4. Find all CS students

RA:

πsid (σmajor='CS'(Student))

SQL:

SELECT sid
FROM Student
WHERE major = 'CS';

Result:

+-----+
| sid |
+-----+
| 101 |
| 102 |
| 104 |
+-----+

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 5/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Q5. Find all CS or CE students

RA:

πsid (σmajor='CS' ∨ major='CE'(Student))

SQL:

SELECT sid
FROM Student
WHERE major = 'CS' OR major = 'CE';

Result:

+-----+
| sid |
+-----+
| 101 |
| 102 |
| 103 |
| 104 |
| 106 |
+-----+

Q6. Find all CS students who live in Edinburg

RA:

πsid (σmajor='CS' ∧ address='Edinburg'(Student))

SQL:

SELECT sid
FROM Student
WHERE major = 'CS' AND address = 'Edinburg';

Result:

+-----+
| sid |
+-----+
| 101 |
+-----+

Q7. Find all students whose majors are undeclared

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

πsid (σmajor is null(Student))

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:

SELECT cid AS CourseNumber


FROM Course;

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:

ρcid→CourseNumber, area→CourseGroup (πcid, area(Course))

πCourseNumber, CourseGroup (ρcid→CourseNumber, area→CourseGroup (Course))

SQL:

SELECT cid AS CourseNumber, area AS CourseGroup


FROM Course;

Result:

+--------------+-------------+
| CourseNumber | CourseGroup |
+--------------+-------------+
| 1201 | INTRO |
| 1370 | INTRO |
| 3326 | PL |
| 4333 | DB |
| 6315 | DB |
| 6333 | DB |
+--------------+-------------+

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 8/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Q11. Find all possible pairs (combinations) of students and professors

RA:

πsid, pid (Student×Professor)

(πsid (Student))×(πpid (Professor))

πsid, pid (Student⋈trueProfessor)

(πsid (Student))⋈true(πpid (Professor))

SQL:

SELECT sid, pid


FROM Student, Professor;

SELECT sid, pid


FROM Student, Professor
WHERE true;

SELECT sid, pid


FROM Student JOIN Professor;

SELECT sid, pid


FROM Student JOIN Professor ON (true);

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 |
+-----+-----+

Q12. Find all possible pairs (combinations) of CS and CE students

RA:

(ρsid→cssid (πsid (σmajor='CS'(Student))))×(ρsid→cesid (πsid (σmajor='CE'(Student))))

SQL:

SELECT S1.sid AS cssid, S2.sid AS cesid


FROM Student S1, Student S2
WHERE S1.major = 'CS' AND S2.major = 'CE';

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)))

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 10/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

SQL:

SELECT DISTINCT cid


FROM Student S, Transcript T
WHERE S.name = 'Nathan' AND S.sid = T.sid;

SELECT DISTINCT cid


FROM Student NATURAL JOIN Transcript
WHERE name = 'Nathan';

SELECT DISTINCT cid


FROM Student S JOIN Transcript T ON (S.sid = T.sid)
WHERE S.name = 'Nathan';

SELECT DISTINCT cid


FROM Student S JOIN Transcript T ON (S.sid = T.sid AND S.name = 'Nathan');

SELECT DISTINCT cid


FROM Student JOIN Transcript USING (sid)
WHERE name = 'Nathan';

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:

SELECT DISTINCT title


FROM Student S, Transcript T, Course C
WHERE S.name = 'Nathan' AND S.sid = T.sid AND T.cid = C.cid;

SELECT DISTINCT title


FROM Student NATURAL JOIN Transcript NATURAL JOIN Course
WHERE name = 'Nathan';

Result:

+-------------------+
| title |

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 11/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

+-------------------+
| Advanced Database |
| Applied Database |
| Database |
+-------------------+

Q15. Find course titles and professor names for courses that the student with name Nathan
took

RA:

πtitle, name((πsid (σname='Nathan'(Student)))⋈Transcript⋈Course⋈Professor)

SQL:

SELECT DISTINCT C.title, P.name


FROM Student S, Transcript T, Course C, Professor P
WHERE S.name = 'Nathan' AND S.sid = T.sid AND T.cid = C.cid AND T.pid = P.pid;

SELECT DISTINCT title, Professor.name


FROM Student NATURAL JOIN Transcript NATURAL JOIN Course JOIN Professor USING (pid)
WHERE Student.name = 'Nathan';

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)

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 12/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

πname(Transcript⋈(πpid (σname='Artem'(Professor)))⋈Student)

πname(Transcript⋈Student⋈(πpid (σname='Artem'(Professor))))

SQL:

SELECT DISTINCT S.name


FROM Student S, Transcript T, Professor P
WHERE P.name = 'Artem' AND S.sid = T.sid AND T.pid = P.pid;

SELECT DISTINCT Student.name


FROM Student NATURAL JOIN Transcript JOIN Professor USING (pid)
WHERE Professor.name = 'Artem';

Result:

+--------+
| name |
+--------+
| Nathan |
| Wendy |
+--------+

Q17. Find names of professors who taught a course

RA:

πname(Professor⋈Transcript)

SQL:

SELECT DISTINCT P.name


FROM Professor P, Transcript T
WHERE P.pid = T.pid;

SELECT DISTINCT name


FROM Professor NATURAL JOIN Transcript;

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:

SELECT DISTINCT name, title


FROM Professor NATURAL LEFT OUTER JOIN (Transcript NATURAL JOIN Course);

SELECT DISTINCT name, title


FROM Professor LEFT OUTER JOIN (Transcript NATURAL JOIN Course) USING (pid);

SELECT DISTINCT name, title


FROM (Transcript NATURAL JOIN Course) NATURAL RIGHT OUTER JOIN Professor;

SELECT DISTINCT name, title


FROM (Transcript NATURAL JOIN Course) RIGHT OUTER JOIN Professor USING (pid);

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)

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 14/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

SQL:

SELECT DISTINCT department, sid


FROM Professor FULL OUTER JOIN Student
ON ( department = major OR
(department = 'CS' AND major='CE') OR
(department='CS' AND major='IT')
); -- MySQL does not support FULL OUTER JOIN

SELECT department, sid


FROM Professor LEFT OUTER JOIN Student
ON ( department = major OR
(department = 'CS' AND major='CE') OR
(department='CS' AND major='IT')
)
UNION
SELECT department, sid
FROM Professor RIGHT OUTER JOIN Student
ON ( department = major OR
(department = 'CS' AND major='CE') OR
(department='CS' AND major='IT')
);

Result:

+------------+------+
| department | sid |
+------------+------+
| CS | 101 |
| CS | 102 |
| CS | 103 |
| CS | 104 |
| CS | 105 |
| CS | 106 |
| MATH | NULL |
| NULL | 107 |
+------------+------+

Q20. Find all CS and all IT students

RA:

πsid ((σmajor='CS'(Student))⋃(σmajor='IT'(Student)))

πsid (σmajor='CS' ∨ major='IT'(Student))

SQL:

SELECT sid
FROM Student
WHERE major = 'CS'

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 15/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

UNION
SELECT sid
FROM Student
WHERE major = 'IT';

SELECT sid
FROM Student
WHERE major = 'CS' OR major = 'IT';

Result:

+-----+
| sid |
+-----+
| 101 |
| 102 |
| 104 |
| 105 |
+-----+

Q21. Find names of professors who taught INTRO or DB courses

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';

SELECT DISTINCT name


FROM Professor P, Transcript T, Course C
WHERE (area = 'INTRO' OR area = 'DB') AND P.pid = T.pid AND T.cid = C.cid;

SELECT DISTINCT name


FROM Professor NATURAL JOIN Transcript NATURAL JOIN Course
WHERE area = 'INTRO' OR area = 'DB';

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 16/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Result:

+-----------+
| name |
+-----------+
| John |
| Christine |
| Artem |
+-----------+

Q22. Find names of professors who taught in 2009 or 2010

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';

SELECT DISTINCT name


FROM Professor P, Transcript T
WHERE (year = '2009' OR year = '2010') AND P.pid = T.pid;

SELECT DISTINCT name


FROM Professor NATURAL JOIN Transcript
WHERE year = '2009' OR year = '2010';

Result:

+-----------+
| name |
+-----------+
| Artem |
| Christine |

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 17/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

| John |
+-----------+

Q23. Find names of professors who taught both INTRO and DB courses

RA:

πname(Professor⋈((πpid (Transcript⋈(σarea='INTRO'(Course))))⋂(πpid (Transcript⋈(σarea='DB'(Course))))))

SQL:

SELECT DISTINCT name


FROM Professor P, Transcript T, Course C
WHERE area = 'INTRO' AND P.pid = T.pid AND T.cid = C.cid
AND P.pid IN
(
SELECT T2.pid
FROM Transcript T2, Course C2
WHERE C2.area = 'DB' AND T2.cid = C2.cid
);

SELECT DISTINCT name


FROM Professor NATURAL JOIN Transcript NATURAL JOIN Course
WHERE area = 'INTRO'
AND pid IN
(
SELECT pid
FROM Transcript NATURAL JOIN Course
WHERE area = 'DB'
);

SELECT DISTINCT name


FROM Professor P,
(SELECT pid
FROM Transcript T1, Course C1
WHERE area = 'INTRO' AND T1.cid = C1.cid) P1,
(SELECT pid
FROM Transcript T2, Course C2
WHERE area = 'DB' AND T2.cid = C2.cid) P2
WHERE P.pid = P1.pid AND P.pid = P2.pid;

SELECT DISTINCT name


FROM Professor
NATURAL JOIN
(SELECT pid
FROM Transcript NATURAL JOIN Course
WHERE area = 'INTRO') P1
NATURAL JOIN
(SELECT pid
FROM Transcript NATURAL JOIN Course
WHERE area = 'DB') P2;

SELECT DISTINCT name


FROM Professor P, Transcript T1, Course C1, Transcript T2, Course C2
WHERE C1.area = 'INTRO' AND T1.cid = C1.cid AND C2.area = 'DB' AND T2.cid = C2.cid
AND P.pid = T1.pid AND P.pid = T2.pid;

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 18/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Result:

+------+
| name |
+------+
| John |
+------+

Q24. Find names of professors who taught in both 2009 and 2010

RA:

πname(Professor⋈((πpid (σyear='2009'(Transcript)))⋂(πpid (σyear='2010'(Transcript)))))

SQL:

SELECT DISTINCT name


FROM Professor P, Transcript T
WHERE year = '2009' AND P.pid = T.pid
AND P.pid IN
(
SELECT pid
FROM Transcript
WHERE year = '2010'
);

SELECT DISTINCT name


FROM Professor NATURAL JOIN Transcript
WHERE year = '2009'
AND pid IN
(
SELECT pid
FROM Transcript
WHERE year = '2010'
);

SELECT DISTINCT name


FROM Professor P,
(SELECT pid
FROM Transcript
WHERE year = '2009') P1,
(SELECT pid
FROM Transcript
WHERE year = '2010') P2
WHERE P.pid = P1.pid AND P.pid = P2.pid;

SELECT DISTINCT name


FROM Professor
NATURAL JOIN
(SELECT pid
FROM Transcript
WHERE year = '2009') P1
NATURAL JOIN
(SELECT pid
FROM Transcript
WHERE year = '2010') P2;

SELECT DISTINCT name


r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 19/ 31
1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

SELECT DISTINCT name


FROM Professor P, Transcript T1, Transcript T2
WHERE T1.year = '2009' AND T2.year = '2010'
AND P.pid = T1.pid AND P.pid = T2.pid;

Result:

+------+
| name |
+------+
+------+

Q25. Find names of professors who taught both Database and Advanced Database courses in
2009

RA:

πname(Professor⋈((πpid (σtitle='Database' ∧ year='2009'(Transcript⋈Course)))⋂(πpid (σtitle='Advanced


Database' ∧ year='2009'(Transcript⋈Course)))))

SQL:

SELECT DISTINCT name


FROM Professor P, Transcript T, Course C
WHERE T.year = '2009' AND C.title = 'Database' AND P.pid = T.pid AND T.cid = C.cid
AND P.pid IN
(
SELECT T2.pid
FROM Transcript T2, Course C2
WHERE T2.year = '2009' AND C2.title = 'Advanced Database' AND T2.cid = C2.cid
);

SELECT DISTINCT name


FROM Professor NATURAL JOIN Transcript NATURAL JOIN Course
WHERE year = '2009' AND title = 'Database'
AND pid IN
(
SELECT pid
FROM Transcript NATURAL JOIN Course
WHERE year = '2009' AND title = 'Advanced Database'
);

SELECT DISTINCT name


FROM Professor P,
(SELECT pid
FROM Transcript T1, Course C1
WHERE year = '2009' AND title = 'Database' AND T1.cid = C1.cid) P1,
(SELECT pid
FROM Transcript T2, Course C2
WHERE year = '2009' AND title = 'Advanced Database' AND T2.cid = C2.cid) P2
WHERE P.pid = P1.pid AND P.pid = P2.pid;

SELECT DISTINCT name


FROM Professor
NATURAL JOIN

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 20/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

(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;

SELECT DISTINCT name


FROM Professor P, Transcript T1, Course C1, Transcript T2, Course C2
WHERE T1.year = '2009' AND C1.title = 'Database' AND T1.cid = C1.cid AND
T2.year = '2009' AND C2.title = 'Advanced Database' AND T2.cid = C2.cid AND
P.pid = T1.pid AND P.pid = T2.pid;

Result:

+-------+
| name |
+-------+
| Artem |
+-------+

Q26. Find titles of courses that were never taught by the professor with id 204

RA:

πtitle(Course⋈((πcid (Course))−(πcid (σpid=204(Transcript)))))

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:

πname(Professor⋈((πpid (Professor))−(πpid (σmajor='IT'(Transcript⋈Student)))))

SQL:

SELECT DISTINCT name


FROM Professor
WHERE pid NOT IN
(
SELECT T.pid
FROM Student S, Transcript T
WHERE S.major = 'IT' AND S.sid = T.sid
);

SELECT DISTINCT name


FROM Professor
WHERE pid NOT IN
(
SELECT pid
FROM Student NATURAL JOIN Transcript
WHERE major = 'IT'
);

Result:

+-----------+
| name |
+-----------+
| Artem |
| Virgil |
| Christine |
+-----------+

Q28. Find courses that were not offered in Fall 2009

RA:

(πcid (Course))−(πcid (σsemester='Fall' ∧ year='2009'(Transcript)))

SQL:

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 22/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

SELECT cid
FROM Course
WHERE cid NOT IN
(
SELECT cid
FROM Transcript T
WHERE semester = 'Fall' AND year = '2009'
);

Result:

+------+
| cid |
+------+
| 1370 |
| 3326 |
+------+

Q29. Find professors who taught all DB courses

RA:

(πpid, cid (Transcript))÷(πcid (σarea='DB'(Course)))

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

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 23/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

RA:

πname(Student⋈((πsid, cid (Transcript))÷(πcid (σarea='INTRO'(Course)))))

SQL:

SELECT DISTINCT S.name


FROM Student S
WHERE NOT EXISTS
(
SELECT C.cid
FROM Course C
WHERE C.area = 'INTRO'
AND C.cid NOT IN
(
SELECT T.cid
FROM Transcript T
WHERE T.sid = S.sid
)
);

Result:

+-------+
| name |
+-------+
| Maria |
| Mike |
+-------+

Q31. Find professors who taught all courses

RA:

(πpid, cid (Transcript))÷(πcid (Course))

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
)
);

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 24/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Result:

+-----+
| pid |
+-----+
+-----+

Q32. Find professors who taught at least two (>=2) different courses

RA:

πpid (Transcript⋈pid=pid2 ∧ cid≠cid2(ρpid→pid2, cid→cid2(πpid, cid (Transcript))))

SQL:

SELECT DISTINCT T1.pid


FROM Transcript T1, Transcript T2
WHERE T1.pid = T2.pid AND T1.cid <> T2.cid;

SELECT DISTINCT T1.pid


FROM Transcript T1 JOIN Transcript T2
ON (T1.pid = T2.pid AND T1.cid <> T2.cid);

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:

(πpid (Professor))−(πpid (σcid≠cid2 ∧ cid≠cid3 ∧ cid2≠cid3(Transcript⋈(ρcid→cid2(πpid,


cid (Transcript)))⋈(ρcid→cid3(πpid, cid (Transcript))))))

SQL:

SELECT pid

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 25/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

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:

(πpid (Transcript⋈pid=pid2 ∧ cid≠cid2(ρpid→pid2, cid→cid2(πpid,


cid (Transcript)))))−(πpid (σcid≠cid2 ∧ cid≠cid3 ∧ cid2≠cid3(Transcript⋈(ρcid→cid2(πpid,
cid (Transcript)))⋈(ρcid→cid3(πpid, cid (Transcript))))))

SQL:

SELECT DISTINCT T1.pid


FROM Transcript T1, Transcript T2
WHERE T1.pid = T2.pid AND T1.cid <> T2.cid AND T1.pid NOT IN
(
SELECT T1.pid
FROM Transcript T1, Transcript T2, Transcript T3

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 26/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

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 DISTINCT T1.pid


FROM Transcript T1 JOIN Transcript T2
ON (T1.pid = T2.pid AND T1.cid <> T2.cid) AND T1.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 Transcript
GROUP BY pid
HAVING COUNT(DISTINCT cid) = 2;

Result:

+-----+
| pid |
+-----+
+-----+

Q35. Find the total number of courses offered in Fall 2009

SQL:

SELECT COUNT(DISTINCT cid)


FROM Transcript
WHERE semester = 'Fall' AND year = '2009';

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:

SELECT semester, year, COUNT(DISTINCT cid) AS NumberOfCourses


FROM Transcript
GROUP BY semester, year
ORDER BY year DESC, semester ASC;

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 27/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

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:

SELECT name, SUM(credits) AS CompletedCredits


FROM Transcript NATURAL JOIN Student NATURAL JOIN Course
GROUP BY sid, name
UNION
SELECT name, 0 AS CompletedCredits
FROM Student NATURAL LEFT OUTER JOIN Transcript
WHERE pid IS NULL
ORDER BY CompletedCredits DESC;

Result:

+---------+------------------+

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 28/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

| 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:

SELECT sid, MAX(CompletedCredits) AS LargestCredits


FROM (SELECT sid, SUM(credits) AS CompletedCredits
FROM Transcript NATURAL JOIN Student NATURAL JOIN Course
GROUP BY sid
UNION
SELECT sid, 0 AS CompletedCredits
FROM Student NATURAL LEFT OUTER JOIN Transcript
WHERE pid IS NULL
) CC;

SELECT sid, CompletedCredits AS LargestCredits


FROM (SELECT sid, SUM(credits) AS CompletedCredits
FROM Transcript NATURAL JOIN Student NATURAL JOIN Course
GROUP BY sid
UNION
SELECT sid, 0 AS CompletedCredits
FROM Student NATURAL LEFT OUTER JOIN Transcript
WHERE pid IS NULL
) CC1
WHERE 0 = (SELECT COUNT(DISTINCT CC3.CompletedCredits)
FROM (SELECT sid, CompletedCredits
FROM (SELECT sid, SUM(credits) AS CompletedCredits
FROM Transcript NATURAL JOIN Student NATURAL JOIN Course
GROUP BY sid
UNION
SELECT sid, 0 AS CompletedCredits
FROM Student NATURAL LEFT OUTER JOIN Transcript
WHERE pid IS NULL
) CC2
) CC3
WHERE CC3.CompletedCredits > CC1.CompletedCredits
);

Result:

+-----+----------------+
| sid | LargestCredits |
+-----+----------------+
| 101 | 9|
+-----+----------------+

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 29/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

Q40. Find the student with the second largest number of completed credits

SQL:

SELECT sid, CompletedCredits AS SecondLargestCredits


FROM (SELECT sid, SUM(credits) AS CompletedCredits
FROM Transcript NATURAL JOIN Student NATURAL JOIN Course
GROUP BY sid
UNION
SELECT sid, 0 AS CompletedCredits
FROM Student NATURAL LEFT OUTER JOIN Transcript
WHERE pid IS NULL
) CC1
WHERE 1 = (SELECT COUNT(DISTINCT CC3.CompletedCredits)
FROM (SELECT sid, CompletedCredits
FROM (SELECT sid, SUM(credits) AS CompletedCredits
FROM Transcript NATURAL JOIN Student NATURAL JOIN Course
GROUP BY sid
UNION
SELECT sid, 0 AS CompletedCredits
FROM Student NATURAL LEFT OUTER JOIN Transcript
WHERE pid IS NULL
) CC2
) CC3
WHERE CC3.CompletedCredits > CC1.CompletedCredits
);

Result:

+-----+----------------------+
| sid | SecondLargestCredits |
+-----+----------------------+
| 103 | 6|
| 104 | 6|
| 105 | 6|
| 106 | 6|
+-----+----------------------+

5 Note to Instructors and Trainers


This document can be used free of charge for educational purposes. If you are planning to use the
presented set of examples in a class, the author must be notified. Please send an email to artem [at] cs
[dot] panam [dot] edu with your name, title, course, department, and institution information. The author
reserves the right to post this information (email will not be posted) on this page. Any feedback on the
content of this document will be greatly appreciated.

6 Report an Error
Please report any typos and errors to the author - Artem Chebotko; artem [at] cs [dot] panam [dot]
edu.

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 30/ 31


1/ 24/ 13 RAM L in Act ion: Relat ional Algebr a and SQ L Exam ples

r at . cs. panam . edu/ in- act ion/ t ut or ial/ #Q 1 31/ 31

You might also like