0% found this document useful (0 votes)
5 views4 pages

homework assignment 10

The document outlines a homework assignment involving SQL procedures and functions. It includes the creation of a stored procedure to retrieve employee project details, a function to calculate average project budgets by department, and queries to find employees working on high-budget projects and departments with above-average budgets. The assignment emphasizes the use of joins and aggregate functions in SQL.

Uploaded by

Dhir Thacker
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)
5 views4 pages

homework assignment 10

The document outlines a homework assignment involving SQL procedures and functions. It includes the creation of a stored procedure to retrieve employee project details, a function to calculate average project budgets by department, and queries to find employees working on high-budget projects and departments with above-average budgets. The assignment emphasizes the use of joins and aggregate functions in SQL.

Uploaded by

Dhir Thacker
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/ 4

HOMEWORK ASSIGNMENT 10

DHIR THACKER
002819144

/* 1. Create GetEmployeeDetails stored procedure return result set of the total


number of projects each employee is working on and the total budget for these
projects. This will involve joining multiple tables and using aggregate functions.
All employees must be returned even if no projects were assigned. */

CREATE PROCEDURE GetEmployeeDetails AS


BEGIN
SELECT e.emp_no, e.emp_fname, e.emp_lname, d.dept_name,
COUNT(p.project_no) as TotalProjects,
SUM(p.budget) AS TotalBudget
FROM employee e
LEFT JOIN works_on w ON e.emp_no = w.emp_no
LEFT JOIN department d on e.dept_no = d.dept_no
LEFT JOIN project p on w.project_no = p.project_no
GROUP BY e.emp_no, e.emp_fname, e.emp_lname, d.dept_name
ORDER BY e.emp_fname, e.emp_lname
END

exec GetEmployeeDetails

/* 2. Create CalculateTotalBudget function that takes dept_no as input parameter


and returns -1 if the department does not exist. Also, make the function calculate
the average budget per project for the specified department. */

-- function definition
CREATE FUNCTION CalculateTotalBudget(@dept_no char(4))
RETURNS FLOAT
BEGIN
DECLARE @average_budget FLOAT;
IF NOT EXISTS (SELECT 1 FROM department WHERE dept_no = @dept_no)
BEGIN
RETURN -1;
END
SELECT @average_budget = AVG(p.budget)
FROM project p
JOIN works_on w ON p.project_no = w.project_no
WHERE w.emp_no IN (
SELECT emp_no
FROM employee
WHERE dept_no = @dept_no);
RETURN @average_budget;
END;

DECLARE @dept_no_param CHAR(4);


DECLARE @result FLOAT;

SET @result = dbo.CalculateTotalBudget('D1');


SELECT @result AS Average_Budget_dept_D1;

SET @result = dbo.CalculateTotalBudget('D2');


SELECT @result AS Average_Budget_dept_D2;

SET @result = dbo.CalculateTotalBudget('D3');


SELECT @result AS Average_Budget_dept_D3;

SET @result = dbo.CalculateTotalBudget('D4');


SELECT @result AS Average_Budget_dept_D4;

SET @result = dbo.CalculateTotalBudget('D6');


SELECT @result AS Average_Budget_dept_D6;

-- department D6 does not exist, hence, -1 is returned.


/* 3. Formulate a query using a subquery to find the names of all employees who
work on projects with a budget greater than the average budget of all projects in
the 'IT' department. */

SELECT e.emp_fname, e.emp_lname


FROM employee e
JOIN works_on w ON e.emp_no = w.emp_no
JOIN project p ON w.project_no = p.project_no
-- joining employee with works_on ON emp_no and project on project_no so we can
narrow down employees to alloted projects
WHERE p.budget > (
SELECT AVG(budget)
FROM project p
JOIN works_on w ON p.project_no = w.project_no
JOIN employee e ON e.emp_no = w.emp_no
WHERE e.dept_no = 'D3'
/* dept_no = 'D3' WHERE dept_name = 'IT'.
-> Directly using dept_no = 'D3' indicating, the specified department is IT */

);
/* 4. Construct a query that shows the department names with average budget greater
than $176,250 */

SELECT d.dept_name AS 'Department Name'


FROM department d
-- joining employee and dept on dept_no, employee and works_on on emp_no and
project and works_on on project_no
JOIN employee e ON e.dept_no = d.dept_no
JOIN works_on w ON w.emp_no = e.emp_no
JOIN project p ON p.project_no = w.project_no
GROUP BY d.dept_name
HAVING AVG(p.budget) > 176250;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy