DBMS Lab Practical Assignment
DBMS Lab Practical Assignment
4
Environment: Microsoft Windows
Tools/ Language: Oracle
Group Functions:
Group functions operate on set of rows, result is based on group of rows rather than one
result per row as returned by single row functions.
Example Queries:
Average GPA
-----------------
3.56666667
Distinct GPA
------------
3.54285714
Minimum GPA
-----------------
2.9
COUNT(*)
----------
12
COUNT(SNAME)
------------
12
COUNT(DISTINCTSNAME)
-------------------------------------
10
MAX(GPA)
---------
3.9
SUM(GPA)
----------
42.8
Q.Find average GPA without using Average function.
Average
----------
3.56666667
There are circumstances where we would like to apply the aggregate function not only to
a single set of tuples, but also to a group of sets of tuples, we specify this wish in SQL
using the group by clause. The attribute or attributes given in the group by clause are used
to form group. Tuples with the same value on all attributes in the group by clause are
placed in one group.
Syntax:
SELECT columnname, columnname
FROM tablename
GROUP BY columnname;
HAVING clause:
The HAVING clause can be used in conjunction with the GROUP BY clause. HAVING
imposes a condition on the GROUP BY clause, this further filters the groups created by
the GROUP BY clause.
HAVING clause can be used to find duplicates in a relation or in other words find unique
values in the situations where DISTINCT cannot apply.
NOTE: Each column specifications specified in HAVING clause must occur in the list of
columns named in the GROUP BY clause.
Example
1.select state, count(cname) from college GROUP BY state;
STATE COUNT(CNAME)
---------- ------------
CA 2
MA 1
NY 1
STATE COUNT(CNAME)
---------- ------------------------
CA 2
Practical Assignment - 4
commit;
drop table Apply;
drop table College;
drop table Student;
create table College(cName varchar2(10) primary key, state
varchar2(10), enrollment int);
create table Student(sID int primary key, sName varchar2(10), GPA
real, sizeHS int);
create table Apply(sID int, cName varchar2(10), major
varchar2(20), decision char(1), primary key(sID, major, cName),
constraint sID_fk Foreign key(sID) references Student, constraint
cName_fk Foreign key(cName) references College);
delete from Student;
delete from College;
delete from Apply;
insert into Student values (123, 'Amy', 3.9, 1000);
insert into Student values (234, 'Bob', 3.6, 1500);
insert into Student values (345, 'Craig', 3.5, 500);
insert into Student values (456, 'Doris', 3.9, 1000);
insert into Student values (567, 'Edward', 2.9, 2000);
insert into Student values (678, 'Fay', 3.8, 200);
insert into Student values (789, 'Gary', 3.4, 800);
insert into Student values (987, 'Helen', 3.7, 800);
insert into Student values (876, 'Irene', 3.9, 400);
insert into Student values (765, 'Jay', 2.9, 1500);
insert into Student values (654, 'Amy', 3.9, 1000);
insert into Student values (543, 'Craig', 3.4, 2000);
insert into College values ('Stanford', 'CA', 15000);
insert into College values ('Berkeley', 'CA', 36000);
insert into College values ('MIT', 'MA', 10000);
insert into College values ('Cornell', 'NY', 21000);
insert into College values ('Harvard', 'MA', 50040);
insert into Apply values (123, 'Stanford', 'CS', 'Y');
insert into Apply values (123, 'Stanford', 'EE', 'N');
insert into Apply values (123, 'Berkeley', 'CS', 'Y');
insert into Apply values (123, 'Cornell', 'EE', 'Y');
insert into Apply values (234, 'Berkeley', 'biology', 'N');
insert into Apply values (345, 'MIT', 'bioengineering', 'Y');
insert into Apply values (345, 'Cornell', 'bioengineering', 'N');
insert into Apply values (345, 'Cornell', 'CS', 'Y');
insert into Apply values (345, 'Cornell', 'EE', 'N');
insert into Apply values (678, 'Stanford', 'history', 'Y');
insert into Apply values (987, 'Stanford', 'CS', 'Y');
insert into Apply values (987, 'Berkeley', 'CS', 'Y');
insert into Apply values (876, 'Stanford', 'CS', 'N');
insert into Apply values (876, 'MIT', 'biology', 'Y');
insert into Apply values (876, 'MIT', 'marine biology', 'N');
insert into Apply values (765, 'Stanford', 'history', 'Y');
insert into Apply values (765, 'Cornell', 'history', 'N');
insert into Apply values (765, 'Cornell', 'psychology', 'Y');
insert into Apply values (543, 'MIT', 'CS', 'N');
commit;
Student Apply
sID sName GPA sizeHS sID cName major decision
123 Amy 3.9 1000 123 Stanford CS Y
234 Bob 3.6 1500 123 Stanford EE N
345 Craig 3.5 500 123 Berkeley CS Y
456 Doris 3.9 1000 123 Cornell EE Y
567 Edward 2.9 2000 234 Berkeley biology N
678 Fay 3.8 200 345 MIT bioengineering Y
789 Gary 3.4 800 345 Cornell bioengineering N
987 Helen 3.7 800 345 Cornell CS Y
876 Irene 3.9 400 345 Cornell EE N
765 Jay 2.9 1500 678 Stanford history Y
654 Amy 3.9 1000 987 Stanford CS Y
987 Berkeley CS Y
543 Craig 3.4 2000
876 Stanford CS N
College 876 MIT biology Y
cName state enrollment 876 MIT marine biology N
Stanford CA 15000 765 Stanford history Y
Berkeley CA 36000 765 Cornell history N
MIT MA 10000 765 Cornell psychology Y
Cornell NY 21000 543 MIT CS N
Harvard MA 50040
Solve the following:
Q1. Count the total number of Students.
Q2. Calculate the average GPA of all Student.
Q3. Determine the minimum and maximum GPA. Rename the titles as ‘max_GPA’
and ‘min_GPA’ respectively.
Q4. Count the number of students having GPA greater than or equal to 3.7.
Q5. Find Maximum, Average, Minimum, total GPA of all student.
Q6. Find total number of colleges in our Application Database.
Q7. Find how many different majors student had applied in.
Q8. Find total no. of Applications in our Application System’s Database.
Q9. Find average of all distinct GPA.
Q10. Display the total number of application accepted.
Q11. Find number of students having GPA>3.4 and coming from high school having
size>1000.
Q12. Find how many student applied to ‘marine biology’.
Q13. Find how many applications were rejected and accepted by the colleges.
Q14. Find how many students applied to a particular major. (show count(sid) as
No_of_applications).
Q15. Find number of applications received by particular college.
Q16. Find number of applications received in a particular major at a particular college.
Q17. Give the college name and major, where number of applications received are
greater than or equal to 2.
Q18. Give the name and no of applications of all those colleges which receives
applications from 3 or more students.
Q19. Give state and number of colleges of a state that has more than 1 college.
Q20. Find the name of students that are duplicate.
Q21. Find how many applications are filed by each student. [Hint: use left join as we
need information about all 12 students here. If they applied nowhere than show
zero in front of them]
Q22. Provide name of students that file 3 or more applications.
Q23. Provide name of student who have not applied to any college.
Q24. Find maximum GPA, Average GPA, and minimum GPA among applicants of
each college. (i.e. say sID 123, 324 and 987 had applied to Berkley then compute
and display max GPA among these three)
Q25. Find how many student have same GPA among all students. (provide this
frequency in two column table as GPA 3.9 is 4 times, GPA 2.9 is 2 times )
Q26. Find how many application of each major are rejected and accepted.
Q27. Find out the acceptance rate for each college. (Acceptance Rate is percentage of
number application accepted w. r. t. number of application received)