Beruflich Dokumente
Kultur Dokumente
This week also continue working on Operations on Relations in Algebra and SQL.
Important Note: Do it in Relational Algebra first, try to convert algebra expressions into SQL.
1. Considering acad scenario, write down queries in relational algebra and SQL for following
queriesa. List IDs of students who have taken courses in MT101 or have taken MT104
result
b. List IDs of students who have taken courses in MT101 and also have taken MT104
r1 studentid(courseno
r2 studentid(courseno
result r1 r2
= 'MT101'
= 'MT104'
(registers))
(registers))
c. List IDs of students who have taken courses in MT101 but have not taken MT104
r1 studentid(courseno
r2 studentid(courseno
result r1 - r2
= 'MT101'
= 'MT104'
(registers))
(registers))
d. List IDs of students who have taken courses in MT104 but have not taken MT101
e. List ID, and Name of students who have taken courses in MT101 and also have taken
MT104 of 2007 batch
r1 studentid(courseno = 'MT101' (registers))
r2 studentid(courseno = 'MT104' (registers))
r3 r1 r2
result studentid, name(batch = 2007( r3 * student ))
f. List name and dname of employee who does not work on any project
g. List student have scored AA in all courses in Semester Autumn 2008
r1 studentid(acadyear=2008
r2 studentid(acadyear=2008
r3 r1 - r2
and semester='Autumn'(registers))
and semester='Autumn' and grade <> 'AA'(registers))
j. List prog-ids and batches of students who have taken all courses offered by Prof P. M.
Jat from Autumn 2007 to Summer 2011 *
r1 registers * offers * instructor
r2 acadyr>=2007 and acadyr<=2011 and instructorname='PMJ'(r1)
r3 sid,course,acadyear,semester(r2)
r4 course,acadyear,semester(r2)
result r3 div r4
SELECT * FROM student AS s
WHERE NOT EXISTS (
SELECT distinct courseno, acadyear, semester FROM registers NATURAL
JOIN offers NATURAL JOIN instructor WHERE instructorname = 'P M Jat'
AND acadyear >= 2007 AND acadyear <= 2011
EXCEPT
SELECT distinct courseno, acadyear, semester FROM registers NATURAL
JOIN offers NATURAL JOIN instructor WHERE instructorname = 'P M Jat'
AND acadyear >= 2007 AND acadyear <= 2011 AND studentid =
s.studentid
);
k. List IDs of students of B.Tech.(CS) batch 2007 having SPI >= 6 in all semesters
r1 studentid(batch=2007 and progid='01'(student))
r2 studentid(spi<6.0(result))
r (r1 - r2) * sudent
SELECT * FROM student
WHERE batch=2007 and progid='01' and studentid NOT IN
(SELECT studentid FROM result WHERE spi < 6.0);
l. List IDs of students of B.Tech.(CS) batch 2007 not having any F grade
An instance of acad schema has been created on your postgresql server (Database name:
public and schema name acad). We are in the process of adding more data
2. Considering following set of relations from database of MyFaceBook user(uid:int, name:varchar, email:varchar, joindate:date, city:varchar)
a. List friends (name, email) of uid 12345, that are older than one year
r1 id='12345'
r2
name, email(
user u
(friend)
u.uid=r1.id
r1)
b. List common friends (name, email) of uid 12345 and uid 23456
c. Lids users (name, email) of user having all friends of uid 12345
d. Lids users (name, email) of friends of uid 12345 with whom 12345 had no
communication either way)
e. List friends (name, email) of friends of uid 12345, that are from same country as of uid
12345
r1 id='12345'(friend)
r2 user u
u.uid=r1.id
r1
f.uid=r1.fid
user f
f. List friends (name, email) of friends of uid 12345, that are not from same country as of
uid 12345
g. List users (name, email) that have no friends from outside of their own country
r1 user u
u.uid=uf.id
friend uf
f.uid=uf.fid
user f
email(user
u1
u1.uid=r2.uid
r2)