2 DBMS
2 DBMS
The GROUP BY clause groups a set of rows into a set of summary rows or groups. Then the
HAVING clause filters groups based on a specified condition. Note that the HAVING clause is
applied after GROUP BY clause, whereas the WHERE clause is applied before the GROUP
BY clause.
The ORDER BY clause must be the last clause that you specify in a query. If the query also
contains a GROUP BY clause, the clause first arranges the output rows into groups. The
ORDER BY clause then sorts the rows within each group.
Joined Relations
• Join operations take two relations and return as a result another relation.
• A join operation is a Cartesian product which requires that tuples in the
two relations match (under some condition). It also specifies the
attributes that are present in the result of the join
• The join operations are typically used as subquery expressions in the
from clause
• Join condition – defines which tuples in the two relations match, and
what attributes are present in the result of the join.
• Join type – defines how tuples in each relation that do not match any
tuple in the other relation (based on the join condition) are treated.
• Three types of joins:
• Natural join
• Inner join
• Outer join
Difference between Natural JOIN and INNER JOIN in SQL :
• Relation prereq
• Observe that
course information is missing CS-347
prereq information is missing CS-315
Outer Join Examples
• Relation course
Left Outer Join
• course natural right outer join prereq
• Relation prereq
• Relation prereq
• A view provides a mechanism to hide certain data from the view of certain
users.
• Any relation that is not of the conceptual model but is made visible to a user
as a “virtual relation” is called a view.
View Definition
• A view is defined using the create view statement which has the form
create view v as < query expression >
where <query expression> is any legal SQL expression. The view name is
represented by v.
• Once a view is defined, the view name can be used to refer to the virtual relation
that the view generates.
• View definition is not the same as creating a new relation by evaluating the query
expression
• Rather, a view definition causes the saving of an expression; the expression is
substituted into queries using the view.
Example Views
• A view of instructors without their salary
create view faculty as
select ID, name, dept_name
from instructor
• Find all instructors in the Biology department
select name
from faculty
where dept_name = ‘Biology’
• Create a view of department salary totals
create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;
Views Defined Using Other Views View Expansion
• create view physics_fall_2009 as Expand use of a view in a query/another view
select course.course_id, sec_id,
building, room_number create view physics_fall_2009_watson as
from course, section (select course_id, room_number
where course.course_id = from (select course.course_id, building,
section.course_id room_number
and course.dept_name = from course, section
’Physics’ where course.course_id = section.course_id
and section.semester = ’Fall’ and course.dept_name = ’Physics’
and section.year = ’2009’; and section.semester = ’Fall’
• create view physics_fall_2009_watson and section.year = ’2009’)
as where building= ’Watson’;
select course_id, room_number
from physics_fall_2009
where building= ’Watson’;
Update of a View
• Add a new tuple to faculty view which we defined earlier
insert into faculty values (’30765’, ’Green’, ’Music’);
This insertion must be represented by the insertion of the tuple
(’30765’, ’Green’, ’Music’, null)
into the instructor relation
Functions and Procedures
• Functions/procedures can be written in SQL itself, or in an external
programming language (e.g., C, Java).
• Functions written in an external languages are particularly useful with
specialized data types such as images and geometric objects.
• Example: functions to check if polygons overlap, or to compare images for similarity.
• Some database systems support table-valued functions, which can return a
relation as a result.
• also supports a rich set of imperative constructs, including
• Loops, if-then-else, assignment
• Many databases have proprietary procedural extensions to SQL that
differ.
SQL Functions
• Define a function that, given the name of a department, returns the count of the number of
instructors in that department.
create function dept_count (dept_name varchar(20))
returns integer
begin
declare d_count integer;
select count (* ) into d_count
from instructor
where instructor.dept_name = dept_name
return d_count;
end
• The function dept_count can be used to find the department names and budget of all
departments with more that 12 instructors.
select dept_name, budget
from department
where dept_count (dept_name ) > 12
SQL functions (Cont.)