Lecture 4 - SQL Part II
Lecture 4 - SQL Part II
SQL Part II
Roi Yehoshua
Agenda
Subqueries
Common table expressions (CTEs)
Recursive queries
Query execution plans
Views
Stored procedures
Functions
Transactions
Triggers
Indexes
You cannot layer aggregate functions on top of each other in the same SELECT
Sets tot_cred to NULL for students who have not taken any course
Instead of SUM(credits) can use COALESCE(SUM(credits), 0)
This would return SUM(credits) if it is not NULL, and 0 otherwise
View names may appear in a query anywhere a table name may appear
35 Roi Yehoshua, 2024
Views
You can see all the available views in the database under the View folder:
For example, let’s call the get_courses_by_dept with Comp. Sci. as the argument:
REPEAT
statement_list
UNTIL boolean expression
END REPEAT;
Or you can set the result of the function into a session variable:
Drawbacks of indexes
Causes updates to the table to become slower (since the index also needs to be updated)
Storage space
A UNIQUE index creates a constraint such that all values in the index must be distinct
For example, let’s define an index on the name column in the instructor table:
CREATE INDEX idx_name
ON instructor (name);
You can see the new index under the Indexes folder of the table: