EduDB Query Part2 Suggestion
EduDB Query Part2 Suggestion
Complexe SELECT
Given a designed database that contains the following tables. The description in
detail is found in Database description_Final_edudb_v2.doc file.
https://www.postgresql.org/docs/13/functions.html
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
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
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
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
Inner join vs. left join vs. right join
They are different
They are not always interchangeable
Exemple: Query N.16, N.17
https://www.postgresql.org/docs/13/functions.html
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
WITH clause
Exemple: query N.18
WITH tmp AS
( a sub-query )
SELECT *
FROM tmp ;