0% found this document useful (0 votes)
7 views11 pages

Dbms Lab#4 Updated Final Faiza

The document outlines Lab #4 for a Database Management Systems course, focusing on multiple rows, views, and stored procedures. It covers group functions, the use of the GROUP BY and HAVING clauses, and the creation and updating of views and stored procedures. Additionally, it includes practical lab tasks for students to implement these concepts using SQL.

Uploaded by

yusrassuet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Dbms Lab#4 Updated Final Faiza

The document outlines Lab #4 for a Database Management Systems course, focusing on multiple rows, views, and stored procedures. It covers group functions, the use of the GROUP BY and HAVING clauses, and the creation and updating of views and stored procedures. Additionally, it includes practical lab tasks for students to implement these concepts using SQL.

Uploaded by

yusrassuet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CE-301L: Database Management Systems

LAB#4
MULTIPLE ROWS, VIEWS AND STORED
PROCEDURES
OBJECTIVE:
 To learn and implement group functions
 To learn and implement ‘GROUP BY’ and ‘HAVING’ clause.
 To learn and implement views
 To learn and implement predefined and user-defined stored procedures with input
parameters.

THEORY:
SINGLE ROW FUNCTIONS
Single row functions are the one who work on single row and return one output per row. For
example, length and case conversion functions are single row functions.
MULTIPLE ROW FUNCTIONS
Multiple row functions work upon group of rows and return one result for the complete set of
rows. They are also known as Group Functions.
WHAT ARE GROUP FUNCTIONS?
Group functions operate on sets of rows to give one result per group.
CE-301L: Database Management Systems

TYPES OF GROUP FUNCTIONS


• AVG
• COUNT
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE

SYNTAX FOR GROUP FUNCTIONS


CE-301L: Database Management Systems

EXAMPLE:

CREATING GROUPS OF DATA


CE-301L: Database Management Systems

GROUP BY Clause Syntax

You can divide rows in a table into smaller groups by using the GROUP BY clause. All columns
in the SELECT list that are not in group functions must be in the GROUP BY clause.

GROUPING BY MORE THAN ONE COLUMN


CE-301L: Database Management Systems

NESTING GROUP FUNCTIONS


Display the maximum average salary
CE-301L: Database Management Systems

Restricting Group Results with the HAVING Clause


When you use the HAVING clause, the SQL server restricts groups as follows:
1. Rows are grouped.
2. The group function is applied.
3. Groups matching the HAVING clause are displayed.
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
Example:

VIEWS IN SQL:
A view is, at its core, nothing more than a stored query. What’s great is that you can mix and
match your data from base tables (or other views) to create what will, in most respects, function
just like an ordinary base table. You can create a simple query that selects from only one table
and leaves some rows or columns out, or you can create a complex query that joins several tables
and makes them appear as one.
CREATING A VIEW
Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
CE-301L: Database Management Systems

Example#1:
In the following example, we have created a view which displays the employees’ names and
their departments by applying join operations on two tables.

We can query the above view as:

Output:

Example#2
In the following query, we have created a view by using the previous view. Also, we have added
a condition for department location by using where clause.

We can query the above view as follows:

Output:

UPDATING A VIEW
A view can be updated with the ALTER VIEW statement.
CE-301L: Database Management Systems

Syntax:
ALTER VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:

Query the above view:

Output:

STORED PROCEDURES
A Stored Procedure is precompiled collection of T-SQL statements. Stored procedures are
named and processed as a single unit.
SYSTEM STORED PROCEDURES
SQL Server provides some readily available stored procedures form managing SQL Server and
for users and database information retrieval. These can only be executed. The following table list
some system stored procedures.
CE-301L: Database Management Systems

CREATING USER DEFINED STORED PROCEDURES:


Users can create their customized stored procedures using the CREATE PROCEDURE
statement which are stored in the current database.
Syntax:
CREATE PROC procedure_name AS T-SQL statement(s)
Example#1:
This is the basic example in which we have created a procedure which displays all the
information of all employees.

Executing the procedure

Output:
CE-301L: Database Management Systems

Example#2
In the following example, we have created a procedure for the JOIN statements. To execute these
kind of statements, use BEGIN and END keyword.

Executing the procedure

Output:

Example#3 (PASSING THE PARAMETERS)


Parameters can be passed to a stored procedure from the EXECUTE statement.
Syntax
CREATE PROCEDURE procedure_name @parameter_name AS T-SQL Statement(s).

EXECUTION
CE-301L: Database Management Systems

OUTPUT

LAB TASKS:
1. Create a table named Students’ GPAs with the following schema. (Student id, batch,
semester #, GPA)
Now Display the average GPA for each batch. List in order of decreasing batches.
2. Create a view which displays those student ids whose average GPAs are greater than or
equal to 3.
3. Create a procedure to display the maximum, minimum and average GPA of each student.
4. Consider the following schema:
Sailors (sid(PK), sname, rating, age)
Boats (bid(PK), bname, color)
Reserves (sid (FK), bid(FK), day(date)). Create a view that calculates the average age of
sailors for each rating level.
5. Create a view that shows the sailor names and the days they made reservations for
sailboats (boats with the color 'Blue').
6. Create a procedure for task#6 where the boat color will be passed as a parameter.
7. Create a table Projects with the following schema. (P_id, Pro_name, Dep_id).
Now write a query to show which projects have been assigned to the ‘HR’ department.
Wrap the above query in the procedure. Also pass the department name as parameter.
8. Answer the following questions:
 What have you learned from the lab task?
 What was the most challenging task and how did you overcome that challenge?

You might also like