(BCSC0802) DBMS Lab Practical Assignment #5
(BCSC0802) DBMS Lab Practical Assignment #5
Theory:
Notice that each SELECT statement within the UNION must have the same number of
columns. The columns must also have similar data types. Also, the columns in each SELECT
statement must be in the same order.
OUTPUT:
SID SNAME
----- ----------
654 Amy
876 Irene
456 Doris
123 Amy
345 Craig
543 Craig
789 Gary
Q: Find the students name and sid whose GPA is equal to GPA of Amy & Craig but result
should not include Amy and Craig?
A: Select sID, sName
From Student
Where GPA IN ( Select GPA
From Student
Where sName IN ('Amy', 'Craig'))
And sName NOT IN ('Amy', 'Craig ');
Q: Find the name of student with their GPA and Sid whose high school size is not equal to
high school size of Doris?
A: select sID, sName, GPA
from student
where sizeHS NOT IN( select sizeHS
from student
where sName='Doris');
OUTPUT: (Write any four only)
SID SNAME GPA
---------- ---------- ----------------
234 Bob 3.6
345 Craig 3.5
567 Edward 2.9
678 Fay 3.8
789 Gary 3.4
987 Helen 3.7
876 Irene 3.9
765 Jay 2.9
543 Craig 3.4
Q: Find the name and Sid of student who apply to same college where Irene has applied ?
A: select sID, sName from student
where sID in(select sID from apply where Cname in(select Cname
from apply where sID in(select sID from student where sName='Irene')));
OUTPUT:
SID SNAME
---------- ----------
765 Jay
876 Irene
987 Helen
678 Fay
123 Amy
543 Craig
345 Craig
Q: Give the names of colleges who receive same number of applications or more than
Stanford had received?
A: select cName from apply group by cName
having count(sID)>=(select count(sID) from apply
where cName='Stanford')
OUTPUT:
CNAME
----------
Cornell
Stanford
Q: Create a table having two columns college name and number of applications they receive?
A:
Create table collegeinfo
(cName varchar(10), no_of_app number(5));
Q: Update Decision to N for all students who applied to same major as sID 876 have applied.
A: update APPLY
set decision='N'
WHERE major IN ( SELECT major
FROM APPLY
WHERE sID = 876);
Q: Delete the Application of student who have applied to same college as of ‘Irene’
A: delete from Apply
where cName IN (Select cName from Apply
where sID IN (Select sID from Student
where sName = 'Irene'));
Q:Find IDs and names of student who share same name with another student.
A: Select *
From Student S1
Where exists ( Select * From student S2
Where S1.sName=S2.sName)
Running above query will give WRONG result as every student of outer query will match (or
each S1 student having same name as S2), we need ensure we are talking about different
person. So,
Select *
From Student S1
Where exists ( Select * From student S2
Where S1.sName=S2.sName
and S1.sID <> S2.sID)
Q: Find IDs of students that applied to all colleges.
Insert these to get student who applied to all college.
insert into Apply values (123, 'MIT', 'CSE', 'N');
insert into Apply values (123, 'Harvard', 'CSE', 'N');
Harvard MA 50040
For next three queries you are expected to solve using without joins and IN
operators (Use only SET Theoretic Functions)
19. Find sid of student who have not applied to Stanford.
20. Find sid of Student that applied to both Stanford and Berkeley.
21. Give list of all names including all names of colleges and students.