Unit_III_Lect
Unit_III_Lect
or equivalently
Ai represents an attribute
Ri represents a relation
P is a predicate.
The result of a SQL query is a relation.
SQL Queries
Basic Query Structure :
Find the names and average salaries of all departments whose average salary is
greater than 42000
Note: predicates in the having clause are applied after the formation of groups
whereas predicates in the where clause are applied before forming groups
SQL Queries
Nested Subqueries:
SQL provides a mechanism for the nesting of subqueries. A subquery is a
select-from-where expression that is nested within another query.
The nesting can be done in the following SQL query
as follows:
From clause: ri can be replaced by any valid subquery
Where clause: P can be replaced with an expression of the form:
B <operation> (subquery)
B is an attribute and <operation> to be defined later.
Select clause:
Ai can be replaced be a subquery that generates a single value.
SQL Queries
Nested Subqueries: Set Membership
Find the names of all instructors whose salary is greater than the salary
of all instructors in the Biology department.
select name
from instructor
where salary > all (select salary
from instructor
where dept name = 'Biology');
SQL Queries
Nested Subqueries: Test for Empty Relations
The exists construct returns the value true if the argument subquery is
nonempty.
exists r r Ø
not exists r r = Ø
SQL Queries
Nested Subqueries: Use of “exists” Clause
Yet another way of specifying the query “Find all courses taught in both
the Fall 2017 semester and in the Spring 2018 semester”
select course_id
from section as S
where semester = 'Fall' and year = 2017 and
exists (select *
from section as T
where semester = 'Spring' and year= 2018
and S.course_id = T.course_id);
repeat
sequence of statements ;
until boolean expression
end repeat
PL/SQL Queries
Language Constructs for Procedures & Functions
For loop
Permits iteration over all results of a query
Example: Find the budget of all departments
OPENING the cursor. The previously declared cursor can now be opened; memory
is allotted.
FETCHING the cursor. The previously declared and opened cursor can now
retrieve data; this is the process of fetching the cursor.
CLOSING the cursor. The previously declared, opened, and fetched cursor must
now be closed to release memory allocation.
PL/SQL Queries
Cursors
PL/SQL Queries
Cursors
DECLARE
CURSOR c_zip IS
SELECT *
FROM zipcode;
vr_zip c_zip%ROWTYPE;
BEGIN
OPEN c_zip;
LOOP
FETCH c_zip INTO vr_zip;
EXIT WHEN c_zip%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(vr_zip.zipcode|| ‘ ’||vr_zip.city||‘
’||vr_zip.state);
END LOOP;
Thanks