Group by - Having Clause - Stored Procedures
Group by - Having Clause - Stored Procedures
Query:
Query:
WHERE Pnumber=Pno
• 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:
Query:
GROUP BY Dname
2. [ WHERE <condition> ]
NULL
• Query:
FROM EMPLOYEE
2. Retrieve the name of each employee who works on all the projects
controlled by department number 5
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
FROM EMPLOYEE
FROM WORKS_ON B
Query:
CREATE ASSERTION SALARY_CONSTRAINT
• A stored procedure is a SQL code that can be saved and reused again and
again.
• Syntax:
CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
• Execution:
EXEC procedure_name;
Example – Stored Procedures
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
• 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