24/04/2024
Practice – part 3:
Complex queries using views
Given a designed database that contains the following tables. The description in detail is
found in Database description_Final_edudb_v2.doc file.
´ student(student_id, first_name, last_name, dob, gender, address, note, clazz_id)
´ subject(subject_id, name, credit, percentage_final_exam)
´ lecturer(lecturer_id, first_name, last_name, dob, gender, address, email)
´ teaching(subject_id, lecturer_id)
´ clazz(clazz_id, name, lecturer_id, monitor_id)
´ enrollment(student_id, subject_id, semester, midterm_score, final_score)
´ View: Create a view from eduDB, named class_infos, this view contains: class_id, class
name, number of students in this class.
1
24/04/2024
´ student(student_id, first_name, last_name, dob, gender, address, note, clazz_id)
´ subject(subject_id, name, credit, percentage_final_exam)
´ lecturer(lecturer_id, first_name, last_name, dob, gender, address, email)
´ teaching(subject_id, lecturer_id)
´ View:
´ Create a view from eduDB, named class_infos, this view contains: class_id, class name,
number of students in this class.
´ Create a view from eduDB, named enrollment_20171 for enrollment information in
semester 20171, this view contains student_id, subject_id,, midterm_score, final_score
1. List of subjects having 5 or more credits.
2. List of students in the class named "CNTT 2 K58".
3. List of students in classes whose name contains "CNTT"
4. Display a list of students who have enrolled in both "Lập trình Java" (Java Programming) and
"Lập trình nhúng" (Embedded Programming).
5. Display a list of students who have enrolled in ”Tin học đại cương" (Java Programming) or
”Cơ sở dữ liệu" (Object-oriented Programming).
6. Display subjects that have never been registered by any students
7. List of subjects (subject name and credit number corresponding) that student "Nguyễn Hoài
An" have enrolled in the semester '20171'.
8. Show the list of students who enrolled in 'Cơ sở dữ liệu' in semesters = '20171'). This list
contains student id, student name, midterm score, final exam score and subject score. Subject
score is calculated by the weighted average of midterm score and final exam score : subject
score = midterm score * (1- percentage_final_exam/100) + final score
*percentage_final_exam/100.
9. Display IDs of students who failed the subject with code 'IT1110' in semester '20171'. Note: a
student failed a subject if his midterm score or his final exam score is below 3 ; or his subject
score is below 4.
10. List of all students with their class name, monitor name.
2
24/04/2024
11. Students aged 25 and above. Given information: student name, age
12. Students were born in June 1999.
13. Display class name and number of students corresponding in each class. Sort the result
in descending order by the number of students.
14. Display the lowest, highest and average scores on the mid-term test of "Mạng máy tính"
in semester '20172'.
15. Give number of subjects that each lecturer can teach. List must contain: lecturer id,
lecturer's fullname, number of subjects.
16. List of subjects which have at least 2 lecturers in charge.
17. List of subjects which have less than 2 lecturers in charge.
18. List of students who obtained the highest score in subject whose id is 'IT3080', in the
semester '20172'.
Functions
´Aggregate functions operate against a collection of
values and return a single summarizing value.
´Scalar functions return a single value based on scalar
input arguments
[Link]
3
24/04/2024
Scalar functions
´Scalar functions return a single value based on scalar
input arguments
´Can be used in any clause
´Example:
upper('tom') → TOM
lower('TOM') → tom
substring('Thomas' for 2) → Th
[Link]
Scalar functions
´Scalar functions return a single value based on scalar
input arguments
´Example:
current_date à 2021-04-09
extract ( 'year' from current_date) à 2021
age(current_date) à [Link]
select current_date, age(current_date), extract ( 'year' from
current_date);
[Link]
4
24/04/2024
Scalar functions
select current_date, age(current_date), extract ( 'year' from
current_date);
select subject_id, lower(subject_id), midterm_score, round(midterm_score)
from enrollment
where student_id = '20170002';
[Link]
Scalar functions
select *, lower(subject_id)
from subject
where lower(subject_id) = 'it3090';
[Link]
10
5
24/04/2024
Aggregate functions
´ Aggregate functions compute a single result from a set of input
values
´ Some example: count(), avg(), max(), min (), sum(), …
MAX(): Computes the maximum of the non-null input values.
MIN(): Computes the minimum of the non-null input values.
AVG(): Computes the average (arithmetic mean) of all the non-null input values.
COUNT ( * ) : Computes the number of input rows. count ( "any" )
COUNT (attribute_name) : Computes the number of input rows in which the
input value is not null.
COUNT(DISTINCT attribute_name) returns the number of unique non-null values
in the attribute.
[Link]
11
Aggregate functions
´ Aggregate functions may be used in SELECT clause and HAVING
clause
´ An aggregation function can not be in WHERE clause, except it's
in a sub-query.
update enrollment set final_score = null
where semester = '20172' and subject_id = 'IT3090'
and student_id = '20170002';
select count(*), count(final_score), count(distinct final_score),
max(final_score), min(final_score), avg(final_score)
from enrollment
where semester = '20172' and subject_id = 'IT3090';
[Link]
12
6
24/04/2024
Inner join vs. left join vs. right join
´They are different
´They are not always interchangeable
Exemple: Query N.16, N.17
[Link]
13
WITH clause
´ WITH provides a way to write auxiliary statements for use in a larger
query
´ Each auxiliary statement in a WITH clause can be a SELECT,
INSERT, UPDATE, or DELETE;
´ All queries in the WITH list are computed ètemporary tables that
can be referenced in the FROM list. A WITH query that is
referenced more than oncein FROM is computed only once
14
7
24/04/2024
WITH clause
´ Exemple: query N.18
WITH tmp AS
( a sub-query )
SELECT *
FROM tmp ;
15