0% found this document useful (0 votes)
15 views9 pages

Exam

Uploaded by

samira abdullah
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)
15 views9 pages

Exam

Uploaded by

samira abdullah
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/ 9

--1- Display the full data about all the dependence

associated with the name of the


--employee they depend on him/her.

/*
SELECT
d.Dependent_name,e.Fname + ' ' +e.Lname as 'full name',
d.Sex,
d.Bdate
from Dependent d
join Employee e ON d.ESSN = e.SSN
*/

--2- Retrieve the names of all employees and the names of


the projects they are working
--on, sorted by the project name.

/*
select
e.Fname + ' ' +e.Lname as 'full name',
p.Pname
from Works_for w
join Project p on w.Pno=p.Pnumber
join Employee e on w.ESSn=e.SSN
order by p.Pname
*/
--3- Select max two salaries in employee table.

/*
select DISTINCT top 2 Salary
from Employee
order by Salary Desc
*/

--4- Select employee name and his salary but if there is no


salary display
--instructor value ‘3000’. ‚use one of coalesce Function‛

/*
SELECT
Fname,
COALESCE(Salary, 3000) AS Salary
--,ISNULL(salary,3000)
FROM
Employee;
*/

--5- Select employee first name and the data of his


supervisor

/*
select DISTINCT
e1.Fname ,
e.*
from Employee e
join Employee e1 on e1.Superssn=e.SSN

*/
--6- Display the data of the second highest-salary employee
without duplicate

/*
WITH SalaryRank AS (
SELECT
Salary,
RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM
Employee
)
SELECT
Salary
FROM
SalaryRank
WHERE
Rank = 2;

*/

--7- Display any Project with a Name starting with the


letter B

/*
select
p.Pname

from Project p
where p.Pname like('B%')
*/
--8- Create a rule on Salary column in employee to ensure
that it is less than 6000
/*
alter table employee
add constraint ch_s check (Salary <6000)
*/

--9- Add Constrain address column can only contain values


that are either 'alex',
--'mansoura', or 'cairo'.‛ .
/*
ALTER TABLE Employee
ADD CONSTRAINT CHK_Address CHECK (Address IN ('alex',
'mansoura', 'cairo'));

*/
--10- Create a scalar function that takes Employee SSN and
returns a message
--to user
--a. If first name and Last name are null then display
'First name &
--last name are null'
--b. If First name is null then display 'first name is null'
--c. If Last name is null then display 'last name is null'
--d. Else display 'First name & last name are not null'.

/*
CREATE FUNCTION CheckNames (@SSN INT)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @Message VARCHAR(50);
IF (SELECT FName FROM Employee WHERE SSN = @SSN) IS NULL
AND (SELECT LName FROM Employee WHERE SSN = @SSN) IS
NULL
SET @Message = 'First name & last name are null';
ELSE IF (SELECT FName FROM Employee WHERE SSN = @SSN) IS
NULL
SET @Message = 'First name is null';
ELSE IF (SELECT LName FROM Employee WHERE SSN = @SSN) IS
NULL
SET @Message = 'Last name is null';
ELSE
SET @Message = 'First name & last name are not
null';
RETURN @Message;
END;
*/
--11- Create multi-statements table-valued function that
takes a string
--If string='first name' returns employees first name
--If string='last name' returns employees last name
--If string='full name' returns Full Name from employees
table
--Note: Use ‚ISNULL‛ function

/*
CREATE FUNCTION GetEmployeeNames (@Type NVARCHAR(20))
RETURNS @Result TABLE (Name NVARCHAR(50))
AS
BEGIN
IF @Type = 'first name'
INSERT INTO @Result
SELECT ISNULL(FName, '') AS Name FROM Employee
ELSE IF @Type = 'last name'
INSERT INTO @Result
SELECT ISNULL(LName, '') AS Name FROM Employee
ELSE IF @Type = 'full name'
INSERT INTO @Result
SELECT CONCAT(ISNULL(FName, ''), ' ', ISNULL(LName,
'')) AS Name FROM Employee
RETURN;
END;
*/
--12- Create a view that will display the project name and
the number of
--employees work on it.

/*
CREATE VIEW ProjectEmployeeCount AS
SELECT
Project.Pname,
COUNT(Works_for.ESSn) AS EmployeeCount
FROM
Project
LEFT JOIN
Works_for ON Project.Pnumber = Works_for.Pno
GROUP BY
Project.PName;

*/

-- 13-
--1) Create view that will display the emp#(ssn) and emp
lastname who works on dept# is ‘d2’

/*
CREATE VIEW DeptD2Employees AS
SELECT
Employee.SSN AS SSN,
Employee.LName
FROM
Employee
INNER JOIN
Departments ON Employee.Dno = Departments.Dnum
WHERE
Departments.Dname = 'd2';
*/
--2) Display the employee lastname that contains letter
‚J‛
--Use the previous view created in Q#1
/*
SELECT
LName
FROM
DeptD2Employees
WHERE
LName LIKE '%J%';

*/

-- 14- Create a stored procedure that will be used in case


there is an old
--employee has left the project and a new one become instead
of him. The
--procedure should take 3 parameters (old Emp. number, new
Emp. number and
--the project number) and it will be used to update works on
table. And return
--‚Employee updated successfully‛.

/*

CREATE PROCEDURE UpdateProjectEmployee


@OldEmpSSN INT,
@NewEmpSSN INT,
@ProjectID INT
AS
BEGIN
UPDATE Works_for
SET ESSn = @NewEmpSSN
WHERE ESSN = @OldEmpSSN AND Pno = @ProjectID;
PRINT 'Employee updated successfully';

END;
*/
----15-
--1- Create table Employee Audit. (name string, date, string
note).

/*
CREATE TABLE EmployeeAudit (
Name NVARCHAR(50),
Date DATETIME,
Note NVARCHAR(255)
);
*/

--2- Create a trigger on Employee table instead of delete to


add Row in
--Employee Audit table (Server User Name, Date, Note) where
note will
--be ‚try to delete Row with Key of row = [Key Value]‛

/*
CREATE TRIGGER PreventDeleteEmployee
ON Employee
INSTEAD OF DELETE
AS
BEGIN
INSERT INTO EmployeeAudit (Name, Date, Note)
SELECT
SUSER_NAME(),
GETDATE(),
'Try to delete Row with Key of row = ' +
CAST(DELETED.SSN AS NVARCHAR(50))
FROM
DELETED;
PRINT 'Delete operation is not allowed';
END;
*/

You might also like