EduDB Query Part3
EduDB Query Part3
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.
1
24/04/2024
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
https://www.postgresql.org/docs/13/functions.html
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
https://www.postgresql.org/docs/13/functions.html
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) à 00:00:00
https://www.postgresql.org/docs/13/functions.html
4
24/04/2024
Scalar functions
https://www.postgresql.org/docs/13/functions.html
Scalar functions
select *, lower(subject_id)
from subject
where lower(subject_id) = 'it3090';
https://www.postgresql.org/docs/13/functions.html
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.
https://www.postgresql.org/docs/13/functions-aggregate.html
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';
https://www.postgresql.org/docs/13/functions-aggregate.html
12
6
24/04/2024
https://www.postgresql.org/docs/13/functions.html
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