Professional Documents
Culture Documents
1 | Page
a)
i) Parsing: The system will first parse the query to understand its structure and identify the
tables and columns involved. In this case, the query involves the SeniorEmp view and selects
the 'sname' column.
Query Transformation: The system will then transform the query based on the view definition. The
SeniorEmp view selects 'ename,' 'age,' and 'salary' columns from the 'Emp' table where the age is
greater than 50. The query is effectively asking for 'sname' from SeniorEmp where the salary is greater
than $100,000.
Query Execution: The system will execute the transformed query by accessing the underlying data in the
'Emp' table, applying the condition 'age > 50,' and then filtering the results based on the 'salary >
100,000' condition.
ii) Give an example of a view on Emp that could be automatically updated by updating Emp
FROM Emp;
b) Construct a stored procedure, named usp_GetLastName, that accepts one input parameter
named EmployeeID and returns (using a SELECT Transact-SQL statement) the last name of the
employee.
@EmployeeID INT
2 | Page
AS
BEGIN
SELECT LastName
FROM YourEmployeeTable
END;
c)
● DELETE Operation:
If a row is deleted from the Employees table, the trigger will check if the Dept# and Salary are not
NULL.
It then updates the TotalSalary in the corresponding Departments row by subtracting the deleted
employee's salary.
● INSERT Operation:
If a row is inserted into the Employees table, the trigger will check if the Dept# and Salary are
not NULL.
It then updates the TotalSalary in the corresponding Departments row by adding the new
employee's salary.
● UPDATE Operation:
If an existing row is updated in the Employees table, the trigger will check if the Dept# and Salary
are not NULL.
If the Dept# is different between the old and new values, it adjusts the TotalSalary in both the
old and new departments accordingly.
If only the Salary changes for the same department, it updates the TotalSalary in that
department by subtracting the old salary and adding the new salary.
● The trigger is designed to maintain the consistency of the TotalSalary in the Departments table
whenever there are changes in the Employees table.
● Potential Output:
When a DELETE, INSERT, or UPDATE operation is executed on the Employees table, the trigger
will automatically adjust the TotalSalary in the corresponding Departments based on the logic
described in the trigger.
d) i.
SELECT * FROM product_tbl where p_id NOT IN (SELECT p_id FROM ProductSale_tbl);
3 | Page
iii) SELECT
PS.id,
PS.p_id,
P.Name,
P.Description
FROM
ProductSale_tbl PS
JOIN
product_tbl P ON PS.p_id = P.p_id
WHERE
PS.quantity_sold = (
SELECT MIN(quantity_sold)
FROM ProductSale_tbl
);
e) CREATE PROCEDURE spGetEmployeeByGenderAndDepartement
@Gender nvarchar(20),
@DepartmentId int
AS
BEGIN
SELECT Name, Gender, Department
FROM employeetble
WHERE Gender = @Gender AND DepartmentId = @DepartmentId;
END
GO
f)
i) INSERT INTO County (CountyId, County_name)
VALUES (48, 'Unknown County');
ALTER TABLE Employee
ADD CONSTRAINT DF_Employee_CountyId DEFAULT 48 FOR CountyId;
INSERT INTO Employee (EmpId, Emp_Name, Address)
VALUES (1, 'John Doe', '123 Main St');
ii)
INSERT INTO Employee (EmpId, Emp_Name, Address, CountyId)
VALUES (1, 'John Doe', '123 Main St', NULL);
Explanation
CountyId is not specified in the INSERT statement (passed as NULL), the default constraint
DF_Employee_CountyId will take effect, and the CountyId for this employee will be set to the default
4 | Page
value of 48 (Unknown County). The NULL value passed during the INSERT operation triggers the default
constraint, ensuring that there is a valid default value assigned to the CountyId column.
g) Write an SQL query, without using a with clause, to find all branches where the total account deposit
is less than the average total account deposit at all branches,
i) SELECT BranchId
FROM (
FROM ProductSale_tbl
GROUP BY BranchId
) AS BranchTotals
FROM ProductSale_tbl
GROUP BY BranchId
) AS AvgBranchTotals);
FROM ProductSale_tbl
GROUP BY BranchId
SELECT AVG(TotalDeposit)
FROM (
5 | Page
SELECT BranchId, SUM(Unit_Price * Quantity_Sold) AS TotalDeposit
FROM ProductSale_tbl
GROUP BY BranchId
) AS BranchTotals
);
BEGIN
THEN
END IF;
END;
6 | Page