Category Notation/Symbol Meaning
Relational Algebra π (Projection) Selects specific attributes (columns) from a relation.
σ (Selection) Filters tuples (rows) that satisfy a given condition.
⨝ (Join) Combines two relations based on a common attribute.
⋈ (Natural Join) Joins relations by automatically matching attributes with the same name.
− (Set Difference) Finds tuples present in one relation but not in another.
∪ (Union) Combines tuples from two relations, removing duplicates.
∩ (Intersection) Finds common tuples in both relations.
γ (Aggregation) Groups tuples and applies aggregate functions like COUNT, SUM, AVG.
Relational Calculus ∃ (Existential Quantifier) - "There exists" Specifies that at least one tuple must satisfy the condition.
∀ (Universal Quantifier) - "For all" Specifies that all tuples must satisfy the condition.
{ T | Condition } - "Set notation" Defines a set of tuples satisfying a condition.
→ (Implication) - "Logical Implication" If the first condition is true, then the second condition must also be true.
¬ (Negation) - "Logical NOT" Represents logical NOT (negation of a condition).
∨ (Disjunction) - "Logical OR" Represents logical OR (at least one condition must be true).

SQL, Relational Algebra, and Relational Calculus Solutions

Schema

Questions and Solutions

1. Retrieve all student names.

SQL:

SELECT sname FROM Students;

Relational Algebra:

π_sname(Students)

Relational Calculus:

{ S.sname | Students(S) }
2. Find all courses offered.

SQL:

SELECT cname FROM Courses;

Relational Algebra:

π_cname(Courses)

Relational Calculus:

{ C.cname | Courses(C) }
3. List all students who scored more than 80.

SQL:

SELECT DISTINCT sname FROM Students S JOIN Enrollments E ON S.sid = E.sid WHERE E.grade > 80;

Relational Algebra:

π_sname(σ_grade>80 (Students ⨝ Enrollments))

Relational Calculus:

{ S.sname | Students(S) ∧ (∃E)(Enrollments(E) ∧ S.sid = E.sid ∧ E.grade > 80) }
4. Retrieve students enrolled in 'Database Systems'.

SQL:

SELECT DISTINCT S.sname FROM Students S JOIN Enrollments E ON S.sid = E.sid JOIN Courses C ON E.cid = C.cid WHERE C.cname = 'Database Systems';

Relational Algebra:

π_sname(σ_cname='Database Systems' (Students ⨝ Enrollments ⨝ Courses))

Relational Calculus:

{ S.sname | Students(S) ∧ (∃E)(Enrollments(E) ∧ S.sid = E.sid ∧ (∃C)(Courses(C) ∧ E.cid = C.cid ∧ C.cname = 'Database Systems')) }
5. Find students who have not enrolled in any course.

SQL:

SELECT sname FROM Students WHERE sid NOT IN (SELECT DISTINCT sid FROM Enrollments);

Relational Algebra:

π_sname(Students) - π_sname(Students ⨝ Enrollments)

Relational Calculus:

{ S.sname | Students(S) ∧ ¬(∃E)(Enrollments(E) ∧ S.sid = E.sid) }
6. Retrieve students older than 21.

SQL:

SELECT sname FROM Students WHERE age > 21;

Relational Algebra:

π_sname(σ_age>21(Students))

Relational Calculus:

{ S.sname | Students(S) ∧ S.age > 21 }
7. Retrieve students who have a GPA greater than 3.5.

SQL:

SELECT sname FROM Students WHERE gpa > 3.5;

Relational Algebra:

π_sname(σ_gpa>3.5(Students))

Relational Calculus:

{ S.sname | Students(S) ∧ S.gpa > 3.5 }
8. Find students who have taken more than one course.

SQL:

SELECT sname FROM Students S WHERE (SELECT COUNT(*) FROM Enrollments E WHERE S.sid = E.sid) > 1;

Relational Algebra:

π_sname(σ_count(cid)>1(Enrollments ⨝ Students))

Relational Calculus:

{ S.sname | Students(S) ∧ (∃E1)(∃E2)(Enrollments(E1) ∧ Enrollments(E2) ∧ E1.sid = S.sid ∧ E2.sid = S.sid ∧ E1.cid ≠ E2.cid) }
9. Retrieve student names who have enrolled in at least one course.

SQL:

SELECT DISTINCT S.sname FROM Students S JOIN Enrollments E ON S.sid = E.sid;

Relational Algebra:

π_sname(Students ⨝ Enrollments)

Relational Calculus:

{ S.sname | Students(S) ∧ (∃E)(Enrollments(E) ∧ S.sid = E.sid) }
10. Find the courses that have at least one student enrolled.

SQL:

SELECT DISTINCT C.cname FROM Courses C JOIN Enrollments E ON C.cid = E.cid;

Relational Algebra:

π_cname(Courses ⨝ Enrollments)

Relational Calculus:

{ C.cname | Courses(C) ∧ (∃E)(Enrollments(E) ∧ C.cid = E.cid) }
11. Retrieve the names of students who have not enrolled in any course.

SQL:

SELECT sname FROM Students S WHERE NOT EXISTS (SELECT * FROM Enrollments E WHERE S.sid = E.sid);

Relational Algebra:

π_sname(Students) - π_sname(Students ⨝ Enrollments)

Relational Calculus:

{ S.sname | Students(S) ∧ ¬(∃E)(Enrollments(E) ∧ S.sid = E.sid) }
12. Find all courses that have no students enrolled.

SQL:

SELECT cname FROM Courses C WHERE NOT EXISTS (SELECT * FROM Enrollments E WHERE C.cid = E.cid);

Relational Algebra:

π_cname(Courses) - π_cname(Enrollments ⨝ Courses)

Relational Calculus:

{ C.cname | Courses(C) ∧ ¬(∃E)(Enrollments(E) ∧ C.cid = E.cid) }
13. Retrieve student names along with the courses they are enrolled in.

SQL:

SELECT S.sname, C.cname FROM Students S JOIN Enrollments E ON S.sid = E.sid JOIN Courses C ON E.cid = C.cid;

Relational Algebra:

π_sname, cname(Students ⨝ Enrollments ⨝ Courses)

Relational Calculus:

{ (S.sname, C.cname) | Students(S) ∧ Enrollments(E) ∧ Courses(C) ∧ S.sid = E.sid ∧ E.cid = C.cid }
14. Find the number of students enrolled in each course.

SQL:

SELECT C.cname, COUNT(E.sid) FROM Courses C LEFT JOIN Enrollments E ON C.cid = E.cid GROUP BY C.cname;

Relational Algebra:

γ_cname, COUNT(sid)(Enrollments ⨝ Courses)

Relational Calculus:

{ (C.cname, count(S.sid)) | Courses(C) ∧ (COUNT S.sid | Students(S) ∧ Enrollments(E) ∧ S.sid = E.sid ∧ E.cid = C.cid) }
15. Retrieve students who are enrolled in all available courses.

SQL:

SELECT S.sname FROM Students S WHERE NOT EXISTS (SELECT * FROM Courses C WHERE NOT EXISTS (SELECT * FROM Enrollments E WHERE S.sid = E.sid AND C.cid = E.cid));

Relational Algebra:

π_sname(Students) - π_sname(σ_¬∃(Courses - π_cid(Enrollments ⨝ Students)))

Relational Calculus:

{ S.sname | Students(S) ∧ ¬(∃C)(Courses(C) ∧ ¬(∃E)(Enrollments(E) ∧ S.sid = E.sid ∧ C.cid = E.cid)) }
16. Find all students who are taking at least two courses.

SQL:

SELECT S.sname FROM Students S JOIN Enrollments E ON S.sid = E.sid GROUP BY S.sname HAVING COUNT(E.cid) >= 2;

Relational Algebra:

π_sname(σ_COUNT(cid)≥2(γ_sname, COUNT(cid)(Enrollments ⨝ Students)))

Relational Calculus:

{ S.sname | Students(S) ∧ (∃E1)(∃E2)(Enrollments(E1) ∧ Enrollments(E2) ∧ S.sid = E1.sid ∧ S.sid = E2.sid ∧ E1.cid ≠ E2.cid) }
17. Retrieve students who are enrolled in a specific course, e.g., 'Database Systems'.

SQL:

SELECT S.sname FROM Students S JOIN Enrollments E ON S.sid = E.sid JOIN Courses C ON E.cid = C.cid WHERE C.cname = 'Database Systems';

Relational Algebra:

π_sname(σ_cname='Database Systems'(Enrollments ⨝ Courses ⨝ Students))

Relational Calculus:

{ S.sname | Students(S) ∧ (∃E)(∃C)(Enrollments(E) ∧ Courses(C) ∧ S.sid = E.sid ∧ E.cid = C.cid ∧ C.cname = 'Database Systems') }
18. List all students who have enrolled in courses taught by a specific instructor.

SQL:

SELECT DISTINCT S.sname FROM Students S JOIN Enrollments E ON S.sid = E.sid JOIN Courses C ON E.cid = C.cid JOIN Instructors I ON C.instructor_id = I.iid WHERE I.iname = 'Dr. Smith';

Relational Algebra:

π_sname(σ_iname='Dr. Smith'(Students ⨝ Enrollments ⨝ Courses ⨝ Instructors))

Relational Calculus:

{ S.sname | Students(S) ∧ (∃E)(∃C)(∃I)(Enrollments(E) ∧ Courses(C) ∧ Instructors(I) ∧ S.sid = E.sid ∧ E.cid = C.cid ∧ C.instructor_id = I.iid ∧ I.iname = 'Dr. Smith') }
19. Find the total number of courses each instructor teaches.

SQL:

SELECT I.iname, COUNT(C.cid) FROM Instructors I LEFT JOIN Courses C ON I.iid = C.instructor_id GROUP BY I.iname;

Relational Algebra:

γ_iname, COUNT(cid)(Courses ⨝ Instructors)

Relational Calculus:

{ (I.iname, count(C.cid)) | Instructors(I) ∧ (COUNT C.cid | Courses(C) ∧ I.iid = C.instructor_id) }
20. Retrieve instructors who do not teach any course.

SQL:

SELECT I.iname FROM Instructors I WHERE NOT EXISTS (SELECT * FROM Courses C WHERE I.iid = C.instructor_id);

Relational Algebra:

π_iname(Instructors) - π_iname(Instructors ⨝ Courses)

Relational Calculus:

{ I.iname | Instructors(I) ∧ ¬(∃C)(Courses(C) ∧ I.iid = C.instructor_id) }