0% found this document useful (0 votes)
101 views

Group by - Having Clause - Stored Procedures

The document discusses GROUP BY and HAVING clauses, stored procedures, triggers, and NULL values in SQL. It provides examples of using GROUP BY to group tuples and aggregate functions, the HAVING clause to filter groups, stored procedures to save and reuse SQL code, and triggers to automatically perform actions when events occur. It also covers three-valued logic for comparisons involving NULL values and defining constraints using assertions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Group by - Having Clause - Stored Procedures

The document discusses GROUP BY and HAVING clauses, stored procedures, triggers, and NULL values in SQL. It provides examples of using GROUP BY to group tuples and aggregate functions, the HAVING clause to filter groups, stored procedures to save and reuse SQL code, and triggers to automatically perform actions when events occur. It also covers three-valued logic for comparisons involving NULL values and defining constraints using assertions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Group By, Having clause,

Stored Procedures and


Triggers
The GROUP BY and HAVING Clauses

• Partition relation into subsets of tuples

• Based on grouping attribute(s)


• Apply function to each such group independently
• GROUP BY clause: Specifies grouping attributes

• If NULLs exist in grouping attribute

• Separate group created for all tuples with


a NULL value in grouping attribute
• HAVING clause

• Provides a condition on the summary


information
Example

• For each department, retrieve the department number, the


number of employees in the department, and their
average salary.

Query:

SELECT Dno, COUNT (*), AVG (Salary)


SELECT clause includes only the
FROM EMPLOYEE grouping attribute and the aggregate
functions to be applied on each group
GROUP BY Dno; of tuples.
Result
Example

• For each project, retrieve the project number, the project


name and the number of employees who work on that
project.

Query:

SELECT Pnumber, Pname, COUNT (*)

FROM PROJECT, WORKS_ON

WHERE Pnumber=Pno

GROUP BY Pnumber, Pname;


Example

• For each project on which more than two employees work, retrieve
the project number, the project name, and the number of employees
who work on the project.

Query:

SELECT Pnumber, Pname, COUNT (*)

FROM PROJECT, WORKS_ON

WHERE Pnumber=Pno WHERE clause limit the tuples to


which functions are applied, the
GROUP BY Pnumber, Pname HAVING clause serves to choose
whole groups
HAVING COUNT (*) > 2;
Result
These groups
are not
selected by
the HAVING
condition
Result (Cont.)
Example

• For each project, retrieve the project number, the project


name and the number of employees from department 5
who work on the project.

Query:

SELECT Pnumber, Pname, COUNT (*)

FROM PROJECT, WORKS_ON, EMPLOYEE

WHERE Pnumber=Pno AND Ssn=Essn AND Dno=5

GROUP BY Pnumber, Pname;


Example
• Count the total number of employees whose salaries exceed $40,000 in each
department, but only for departments where more than five employees work.

It will select only departments that have


more than five employees who each earn
Query: more than $40,000. The tuples are
already restricted to employees who earn
SELECT Dname, COUNT (*) more than $40,000 before the function in
the HAVING clause is applied.
FROM DEPARTMENT, EMPLOYEE

WHERE Dnumber=Dno AND Salary>40000

GROUP BY Dname

HAVING COUNT (*) > 5;


Example (Cont.)

SELECT Dnumber, COUNT (*)

FROM DEPARTMENT, EMPLOYEE

WHERE Dnumber=Dno AND Salary>40000 AND

( SELECT Dno For each department that


has more than five
FROM EMPLOYEE employees, retrieve the
department number and
the number of its
GROUP BY Dno employees who are making
more than $40,000.
HAVING COUNT (*) > 5)
5. SELECT <attribute and function list>

1. FROM <table list>

2. [ WHERE <condition> ]

3. [ GROUP BY <grouping attribute(s)> ]

4. [ HAVING <group condition> ]

6. [ ORDER BY <attribute list> ];


Comparisons Involving NULL and
Three-Valued Logic

NULL

Unavailable or Not applicable


Unknown value
withheld value attribute

• Each individual NULL value considered to be different from every other


NULL value

• SQL uses a three-valued logic: TRUE, FALSE, and UNKNOWN


Logical Connectives in Three –
Valued Logic
COMPANY Database
Comparisons Involving NULL
and Three-Valued Logic (cont’d.)
• SQL allows queries that check whether an attribute value is NULL: IS or IS
NOT NULL

• Example: Retrieve the names of all employees who do not have


supervisors.

• Query:

SELECT Fname, Lname

FROM EMPLOYEE

WHERE Super_ssn IS NULL;


COMPANY Database
Practice Drill

1. List the names of managers who have at least one dependent


using EXISTS and NOT EXISTS functions

2. Retrieve the name of each employee who works on all the


projects controlled by department number 5 using EXISTS and
NOT EXISTS functions
Solution – Practice Drill

1. List the names of managers who have at least one dependent

SELECT Fname, Lname Selects all DEPENDENT


tuples related to an
FROM EMPLOYEE EMPLOYEE
WHERE EXISTS ( SELECT *
FROM DEPENDENT
Selects all DEPARTMENT
WHERE Ssn=Essn ) tuples managed by the
EMPLOYEE

AND EXISTS ( SELECT *


FROM DEPARTMENT WHERE
Ssn=Mgr_ssn );
Solution – Practice Drill

2. Retrieve the name of each employee who works on all the projects
controlled by department number 5

Selects all projects controlled by


department 5 (not correlated to
SELECT Fname, Lname FROM EMPLOYEE
outer query),
WHERE NOT EXISTS ( ( SELECT Pnumber FROM PROJECT
WHERE Dnum=5)
Selects all projects that the
particular employeeEXCEPT being( SELECT Pno
considered works on (correlated
to outer query)  
FROM WORKS_ON WHERE
Ssn=Essn) )

If the set difference of the first subquery result MINUS (EXCEPT) the second subquery result is
empty, it means that the employee works on all the projects and is therefore selected. 
Solution – Practice Drill

2. Retrieve the name of each employee who works on all the projects
controlled by department number 5

SELECT Lname, Fname

FROM EMPLOYEE

WHERE NOT EXISTS ( SELECT *

FROM WORKS_ON B

WHERE ( B.Pno IN ( SELECT Pnumber

FROM PROJECT WHERE Dnum=5 ) AND NOT EXISTS ( SELECT *

FROM WORKS_ON C WHERE C.Essn=Ssn AND C.Pno=B.Pno )));


Specifying Constraints as
Assertions
• CREATE ASSERTION

DDL command that specifies additional type of constraints


outside the scope of built-in relational model constraints

• Each assertion is given a constraint name and is


specified via a condition
Example - Assertions
• Specify the constraint that the salary of an employee
must not be greater than the salary of the manager of the
department that the employee works for.
Name of Constraint

Query:
CREATE ASSERTION SALARY_CONSTRAINT

CHECK ( NOT EXISTS ( SELECT *

FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D

WHERE E.Salary>M.Salary AND E.Dno=D.Dnumber AND D.Mgr_ssn=M.Ssn ) )


Stored procedure & Triggers
Stored Procedures

• A stored procedure is a SQL code that can be saved and reused again and
again.

• Parameters can be passed to a stored procedure, based on which the stored


procedure can act.

• Syntax:

CREATE PROCEDURE procedure_name
AS
sql_statement
GO;

• Execution:

EXEC procedure_name;
Example – Stored Procedures

• Create a stored procedure "SelectAllEmployees" that selects all


records from the “EMPLOYEE" table.

Query:

CREATE PROCEDURE SelectAllEmployees
AS
SELECT * FROM EMPLOYEE
GO;

Execution:

EXEC SelectAllEmployees;
Specifying Actions as Triggers

• CREATE TRIGGER
Specifies automatic actions that database system will perform when
certain events and conditions occur

• To monitor the database

• Typical trigger has three components:


• Event(s)
• Condition
• Action
Example - Triggers

• Check whenever an employee’s salary is greater than the salary of his or her direct
supervisor in the COMPANY database and in case of voilation call a stored procedure
INFORM_SUPERVISOR

Query: Event
CREATE TRIGGER SALARY_VIOLATION
BEFORE INSERT OR UPDATE OF SALARY, SUPERVISOR_SSN
ON EMPLOYEE
FOR EACH ROW Condition
WHEN ( NEW.SALARY > ( SELECT SALARY FROM EMPLOYEE
WHERE SSN = NEW.SUPERVISOR_SSN ) )

INFORM_SUPERVISOR(NEW.Supervisor_ssn,
Action );
NEW.Ssn
Stored Procedure v/s Triggers

Stored Procedure Triggers

• A stored procedure can be executed • A trigger can only be executed when


anytime. an event (insert, delete, and update)
is fired on a table.
• Triggers can’t take input parameters.
• Stored procedure can take input • A trigger cannot return a value.
parameters. • Triggers are used for auditing work.
• Stored procedures can return values.
• Stored procedures are used for
performing tasks.
Thanks

You might also like