Sie sind auf Seite 1von 4

Lab 03

IT214 Database Management System


Autumn2012 [Week 4: 13/08/12 to 17/08/12]

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

studentid(courseno = 'MT101' OR courseno = 'MT104'(registers))

SELECT studentid FROM registers


WHERE courseno='MT101' OR courseno='MT104';

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

SELECT studentid FROM registers


WHERE courseno='MT101'
INTERSECT
SELECT studentid FROM registers
WHERE courseno='MT104'

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

SELECT studentid FROM registers


WHERE courseno='MT101'
EXCEPT
SELECT studentid FROM registers
WHERE courseno='MT104'

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

SELECT r1.studentid, name FROM


(SELECT studentid FROM registers
WHERE courseno='MT101'
INTERSECT
SELECT studentid FROM registers
WHERE courseno='MT104') AS r1
NATURAL JOIN student
WHERE batch=2007;

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

SELECT studentid FROM registers


WHERE acadyear=2008 AND semester = 'Autumn'
EXCEPT
SELECT studentid FROM registers
WHERE acadyear=2008 AND semester = 'Autumn' AND grade <> 'AA';

h. List student have scored AA or AB in all courses in Semester Autumn 2008


i. List all courses offered by Prof P. M. Jat from Autumn 2007 to Summer 2011

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)

-- represents all users/members registered with MyFaceBook


city(cityname: varchar, country: varchar)

-- based on assumption that city name is unique !


friend(id:int, fid:int, since:date, messages_in:int, messages_out:int)
-- Semantics of attributes: id has a set of friends identified by fid since since,
message_in and message_out are counts of incoming and outgoing
communications respectively, between id and fid.
--FKs: id and fid of friend refers to user(uid), and city of user refers to city(cityname)

a. List friends (name, email) of uid 12345, that are older than one year
r1 id='12345'
r2

and since-today >= 1year

name, email(

user u

(friend)

u.uid=r1.id

r1)

SELECT name, email FROM


(SELECT * FROM friend WHERE id='12345'
AND extract(year from age(since)) >= 1) AS r1
JOIN user AS u ON (r1.id=u.uid);
OR
SELECT name, email FROM
user JOIN friend ON (id=uid)
WHERE uid='12345' AND extract(year from age(since)) >= 1;

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

cu.cityname=u.city city cu cf.cityname=f.city city cf


result f.name, f.email(cu.country=cf.country(r2))
SELECT f.name, f.email FROM
(SELECT * FROM friend WHERE id='12345') AS r1
JOIN user AS u ON (r1.id=u.uid)
JOIN user AS f ON (r1.fid=f.uid)
JOIN city AS cu ON (u.city=cu.cityname)
JOIN city AS cf ON (f.city=cf.cityname)
WHERE cu.country=cf.country;

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

cu.cityname=u.city city cu cf.cityname=f.city city cf


r2 uid(user) - u.uid(cu.country <> cf.country(r1))
result name,

email(user

u1

u1.uid=r2.uid

r2)

SELECT name, email FROM user


WHERE uid NOT IN (
SELECT uid FROM friend AS fd
JOIN user AS u ON (fd.id=u.uid)
JOIN user AS f ON (fd.fid=f.uid)
JOIN city AS cu ON (u.city=cu.cityname)
JOIN city AS cf ON (f.city=cf.cityname)
WHERE cu.country <> cf.country);

Das könnte Ihnen auch gefallen