You are on page 1of 4

create database StudentDB

create table student


(
studentid char(2),
studentname varchar(50),
address varchar(50)
constraint PK_stduent primary key(studentid)
)
insert into student
values('1','Ryan','Jakarta')
insert into student
values('2','Abi Hirzan','Cibinong')
insert into student
values('3','Zachira','Palu')
select *from grade
create table subject
(
subjectid char(5),
subjectname varchar(30),
credits int
constraint PK_subject primary key(subjectid)
)

insert into subject


values('IT101','Calculus','3')
insert into subject
values('IT201','Programming_Concept','4')
create table grade
(
studentid char(2),
subjectid char(5),
score int,
grade char(2)
constraint PK_grade primary key(studentid,subjectid),
constraint FK_grade foreign key(studentid)references student,
constraint FK_grade_2 foreign key(subjectid)references subject
)
insert into grade
values('1','IT101','62','C')
insert into grade
values('1','IT201','75','B')
insert into grade
values('2','IT101','72','B')
insert into grade
values('2','IT201','90','A')
insert into grade
values('3','IT101','95','A')
drop problem 1
select studentname, subjectname, score, grade
from student, subject, grade
where student.studentid = grade.studentid and subject.subjectid = grade.subjecti
d
select studentname, grade
from student join grade on student.studentid = grade.studentid
select studentname, subjectname, grade
from (student join grade on student.studentid = grade.studentid) join subject on
subject.subjectid = grade.subjectid
drop problem 2
select subjectname
from subject
where subjectid = (select subjectid
from grade
where studentid = (select studentid
from student
where studentname = 'Abi Hirzan' ) and gra
de = 'A')
select subjectname
from student, subject, grade
where student.studentid = grade.studentid and subject.subjectid = grade.subjecti
d and student.studentname = 'Abi Hirzan' and grade.grade = 'A'
select subjectname
from (student join grade on student.studentid = grade.studentid)join subject on
subject.subjectid = grade.subjectid
where student.studentname = 'Abi Hirzan' and grade.grade = 'A'
drop problem 3
select studentname, subjectname, grade
from (student join grade on student.studentid = grade.studentid) join subject on
subject.subjectid = grade.subjectid
where subjectname = 'Programming_Concept' and (grade = 'A' or grade = 'B')
select studentname, subjectname, score, grade
from student, subject, grade
where student.studentid = grade.studentid and subject.subjectid = grade.subjecti
d and subjectname = 'Programming_Concept' and (grade = 'A' or grade = 'B')
problem 4
select studentname, Max(score) as Maximum
from grade, student
where student.studentid = grade.studentid
group by studentname
________________________________________________________________________________
___________________________________________________________________________

Aggregat Function
1. Min
2. Max
3. Avg
4. Count
5. Sum
!!! Remember !!!
if we have aggregat function in select, then you can't have other column name in
select,
unless the column name is appear in Groupby
select min(score) as minimum
from grade
select avg(score) as Avarage
from grade
select sum(score) as result
from grade
select min(score) as minimum, sum(score) as result
from grade
select min(score) as minimum, studentid
from grade
group by studentid

GROUP BY syntax for grouping based on a certain column


select x1, x2, x3
from x
group by x1, x2, x3
select x1, x2, x3
from x
group by x2, x1, x3
select x1, x2, x3
from x
group by x3, x2, x1
!!! REMEMBER !!!
every column names that appear after select must appear also in Group by
HAVING SYNTAX
The SQL HAVING clause is used to restrict conditionally the output of a SQL stat
ement,
by a SQL aggregate function used in your SELECT list of columns.
You can't specify criteria in a SQL WHERE clause against a column in the SELECT
list for which SQL aggregate function is used.
For example the following SQL statement will generate an error:

SELECT Employee, SUM (Hours)


FROM EmployeeHours
WHERE SUM (Hours) > 24
GROUP BY Employee
The SQL HAVING clause is used to do exactly this, to specify a condition for an
aggregate function which is used in your query:

SELECT Employee, SUM (Hours)


FROM EmployeeHours
GROUP BY Employee
HAVING SUM (Hours) > 24
The above SQL statement will select all employees and the sum of their respectiv
e hours, as long as this sum is greater than 24.
The result of the SQL HAVING clause can be seen below:
Employee Hours
John Smith 25
Tina Crown 27

You might also like