Ujjwal Singh
0801IT211090
LabAssignment2
Q 1. Create the table describe below:
TableName:Employee
Attribute DataType Primary Foreign Constraint
First Name Varchar(15) NOT NULL
Mid Name Char(1)
Last Name Varchar(15) NOT NULL
SSN Number CHAR(9) Y NOT NULL
Birthday DATE
Address Varchar(50)
Sex Char(1) SexIN('M',
'F','m', 'f')
Salary DECIMAL(10,2) DEFAULT800
Supervision SSN Char(9) Employee(SSN)
ON DELETE
SETNULL
Department INT
Number
TableName:Department
Attribute DataType Primary Foreign Constraint
Depaertment Varchar(15) UNIQUE NOT NULL
Name
Department INT Y NOT NULL
Number
Manager SSN Char(9) Employee(SSN) NULL
ON DELETE
SETNULL
ManageStart DATE
Date
TableName:Dept_Location
Ujjwal Singh
0801IT211090
Attribute DataType Primary Foreign Constraint
Department INT Y Department(DepN NOT NULL
Number o) ON DELETE
CASCADE
Department VARCHAR(15) Y NOT NULL
Location
TableName:Project
Attribute DataType Primary Foreign Constraint
Project Name Varchar(15) UNIQUE NOT NULL
Project Number INT Y NOT NULL
Project Location Varchar(15)
Department INT Department(DepN
Number o) ON DELETE
SETNULL
TableName:Works_on
Attribute DataType Primary Foreign Constraint
Employee SSN CHAR(9) Y Employee NOT NULL
(SSN)ON
DELETE
CASCADE
Project Number INT Y Project NOT NULL
(Pnumber)ON
DELETE
CASCADE
Hours DECIMAL(3,1) NOT NULL
TableName:Dependent
Attribute DataType Primary Foreign Constraint
Employee SSN CHAR(9) Y Employee NOT NULL
(SSN)ON
DELETE
CASCADE
Ujjwal Singh
0801IT211090
Dependent Name Varchar(15) Y NOT NULL
Sex Char SexIN('M',
'F','m', 'f')
Birthday Date
Relationship Varchar(8)
CODE:
CREATE DATABASE UJJWALassignment2;
USE UJJWALassignment2;
CREATETABLEEmployee(
FirstNameVARCHAR(15)NOTNULL,
MidName CHAR(1),
LastNameVARCHAR(15)NOTNULL,
SSNNumberCHAR(9)PRIMARYKEYNOT NULL,
Birthday DATE,
Address VARCHAR(50),
SexCHAR(1)CHECK(SexIN('M','F','m','f')),
Salary DECIMAL(10,2) DEFAULT 800,
SupervisionSSNCHAR(9),
DepartmentNumber INT,
FOREIGNKEY(SupervisionSSN)REFERENCESEmployee(SSNNumber)
ON DELETE SET NULL
);
CREATETABLEDepartment(
DepartmentNameVARCHAR(15)UNIQUENOTNULL,
Ujjwal Singh
0801IT211090
DepartmentNumberINTPRIMARYKEYNOTNULL, ManagerSSN
CHAR(9),
ManagerStartDateDATE,
FOREIGNKEY(ManagerSSN)REFERENCESEmployee(SSNNumber)
ON DELETE SET NULL
);
CREATE TABLE Dpt_Location(
DepartmentNumber INT NOT NULL,
DepartmentLocationVARCHAR(15)NOTNULL,
PRIMARYKEY(DepartmentNumber,DepartmentLocation),
FOREIGNKEY(DepartmentNumber)REFERENCESDepartment(DepartmentNumber) ON
DELETE CASCADE
);
CREATETABLEProject(
ProjectNameVARCHAR(15)UNIQUENOTNULL,
ProjectNumberINTPRIMARYKEYNOTNULL,
ProjectLocation VARCHAR(15),
DepartmentNumber INT,
FOREIGNKEY(DepartmentNumber)REFERENCESDepartment(Departmentnumber) ON
DELETE SET NULL
);
Ujjwal Singh
0801IT211090
CREATETABLEWorks_on(
EmployeeSSN CHAR(9),
ProjectNumber INT,
Hours DECIMAL(3,1),
PRIMARYKEY(EmployeeSSN,ProjectNumber),
FOREIGNKEY(EmployeeSSN)REFERENCESEmployee(SSNNUMber)
ON DELETE cASCADE,
FOREIGNKEY(ProjectNumber)REFERENCESProject(projectnumber) ON
DELETE CASCADE
);
CREATETABLEDependent( ESSN
CHAR(9) NOT NULL,
Dependent_Name VARCHAR(15) NOT NULL,
SexCHAR(1)CHECK(SexIN('M','F','m','f')), BDate
DATE,
Relationship VARCHAR(8),
PRIMARYKEY(ESSN,Dependent_Name),
FOREIGNKEY(ESSN)REFERENCESEmployee(SSNnumber) ON
DELETE CASCADE);
Q 2. Insert following data into the corresponding table :
a. Insert data into Employee Table:
Ujjwal Singh
0801IT211090
Fname Mname Lname SSN Bdate Address Sex Salary SuperSSN DepNo
Doug E Gilbert 554433221 09-JUN-60 11S59E,Salt M 80000 NULL 3
LakeCity,UT
Joyce PAN 543216789 07-FEB-78 35S18E,Salt F 70000 NULL 2
LakeCity,UT
Frankin T Wong 333445555 08-DEC- 638,Voss,Houston, TX M 40000 554433221 5
45
Jennifer S Wallace 987654321 20-JUN-31 291,Berry,Bellaire,TX F 43000 554433221 4
John B Smith 123456789 09-JAN-55 731, M 30000 333445555 5
Fondren,
Houston,
TX
Ramesh K Narayan 666884444 15-SEP-52 975 Fire M 38000 333445555 5
Oak,
Humble,Tx
Joyce A English 453453453 31-JUL-62 5631Rice,Houston, TX F 25000 333445555 5
James E Borg 888665555 10-NOV- 450Stone,Houston, TX M 55000 543216789 1
27
Alicia J Zelaya 999887777 19-JUL-58 3321Castle,Spring,TX F 25000 987654321 4
Ahmad V Jabbar 987987987 29-MAR- 980Dallas,Houston,TX M 25000 987654321 4
59
b. Insert data into Department Table:
Dname DepNo MgrSSN MgrDate
Manufacture 1 888665555 19-JUN-71
Administration 2 543216789 04-JUL-99
Headquarter 3 554433221 22-SEP-55
Finance 4 987654321 01-JAN-85
Research 5 333445555 22-MAY-78
c. Insert data into Dept_Locations Table:
DepNo Dlocation
1 Houston
1 Chicago
2 NewYork
2 San Francisco
Ujjwal Singh
0801IT211090
3 Salt Lake City
4 Stafford
4 Ballaire
5 Sugarland
5 Houston
d. InsertdataintoProjectTable:
Pname Pnumber Plocation DepNo
ProjectA 3388 Houston 1
ProjectB 1945 Salt Lake City 3
ProjectC 6688 Houston 5
ProjectD 24 Bellaire 4
ProjectE 77 Sugarland 5
ProjectF 1 Salt Lake City 3
Project G 12 NewYork 2
ProjectH 34 Stafford 4
ProjectI 43 Chicago 1
ProjectJ 22 San Francisco 2
e. InsertdataintoWorks_ontable:
ESSN Pno Hours
123456789 3388 32.5
123456789 1945 7.5
666884444 3388 40.0
453453453 77 20.0
453453453 22 20.0
333445555 77 10.0
333445555 6688 10.0
Ujjwal Singh
0801IT211090
333445555 43 35.0
333445555 22 28.5
999887777 1 11.5
999887777 12 13.0
543216789 22 17.0
554433221 1945 21.5
f. InsertdataintoDependentTable:
ESSN Dependent_Name Sex BDate Relationship
333445555 Alice F 05-APR-76 Daughter
333445555 Theodore M 25-OCT-73 Son
333445555 Joy F 03-MAY-48 Spouse
987654321 Abner M 29-FAB-32 Spouse
123456789 Michael M 01-JAN-78 Son
123456789 Alice F 31-DEC-78 Daughter
123456789 Elizabeth F 05-MAY-57 Spouse
CODE:
INSERTINTOEmployee(Firstname,Midname,Lastname,SSNNumber,Birthday,Address,
Sex, Salary, SupervisionSSN, DepartmentNumber)
('Doug','E','Gilbert','554433221','1960-06-09','11S59E,SaltLakeCity,UT','M',80000,
NULL, 3),
('Joyce',NULL,'PAN','543216789','1978-02-07','35S18E,SaltLakeCity,UT','F', 70000,
NULL, 2),
('Frankin','T','Wong','333445555','1945-12-08','638,Voss,Houston,TX','M',40000,
'554433221', 5),
('Jennifer','S','Wallace','987654321','1931-06-20','291,Berry,Bellaire,TX','F',43000,
'554433221', 4),
('John', 'B', 'Smith', '123456789', '1955-01-09', '731, Fondren, Houston, TX', 'M', 30000,
'333445555', 5),
('Ramesh', 'K', 'Narayan', '666884444', '1952-09-15', '975 Fire Oak, Humble, Tx', 'M', 38000,
'333445555', 5),
('Joyce', 'A', 'English', '453453453', '1962-07-31', '5631 Rice, Houston, TX', 'F', 25000,
'333445555', 5),
('James','E','Borg','888665555','1927-11-10','450Stone,Houston,TX','M',55000,
Ujjwal Singh
0801IT211090
'543216789', 1),
('Alicia', 'J', 'Zelaya', '999887777', '1958-07-19', '3321 Castle, Spring, TX', 'F', 25000,
'987654321', 4),
('Ahmad', 'V', 'Jabbar', '987987987', '1959-03-29', '980 Dallas, Houston, TX', 'M', 25000,
'987654321', 4);
INSERT INTO Department (DEPARTMENTname, Departmentnumber,
ManagerSSN, ManagestartDate)
VALUES
('Manufacture', 1, '888665555', '1971-06-19'),
('Administration', 2, '543216789', '1999-07-04'),
('Headquarter', 3, '554433221', '1955-09-22'),
('Finance', 4, '987654321', '1985-01-01'),
('Research', 5, '333445555', '1978-05-22');
Ujjwal Singh
0801IT211090
INSERTINTODpt_Location(DepartmentNumber,Departmentlocation)
VALUES
(1, 'Houston'),
(1, 'Chicago'),
(2 ,'New York'),
(2,'San Francisco'),
(3,'Salt Lake City'),
(4,'Stafford'),
(4 ,'Ballaire '),
(5 ,'Sugarland '),
(5, 'Houston ');
INSERTINTOProject(Projectname,Projectnumber,Projectlocation,DepartmentNumber)
VALUES
('ProjectA', 3388, 'Houston', 1),
('ProjectB', 1945, 'Salt Lake City', 3),
('ProjectC', 6688, 'Houston', 5),
('ProjectD', 24, 'Bellaire', 4),
('ProjectE', 77, 'Sugarland', 5),
('ProjectF', 1, 'Salt Lake City', 3),
('ProjectG',12,'NewYork',2),
('ProjectH',34,'Stafford',4),
('ProjectI', 43, 'Chicago', 1),
('ProjectJ', 22, 'San Francisco', 2);
Ujjwal Singh
0801IT211090
INSERTINTOWorks_on(EmployeeSSN,Projectnumber,Hours)VALUES
('123456789', 3388, 32.5),
('123456789', 1945, 7.5),
('666884444', 3388, 40.0),
('453453453', 77, 20.0),
('453453453', 22, 20.0),
('333445555', 77, 10.0),
('333445555', 6688, 10.0),
('333445555', 43, 35.0),
('333445555', 22, 28.5),
('999887777', 1, 11.5),
('999887777', 12, 13.0),
('543216789', 22, 17.0),
('554433221', 1945, 21.5);
Ujjwal Singh
0801IT211090
INSERTINTODependent(ESSN,Dependent_Name,Sex,Bdate,Relationship) VALUES
('333445555', 'Alice', 'F', '1976-04-05', 'Daughter'),
('333445555', 'Theodore', 'M', '1973-10-25', 'Son'),
('333445555', 'Joy', 'F', '1948-05-03', 'Spouse'),
('987654321', 'Abner', 'M', '1932-02-29', 'Spouse'),
('123456789', 'Michael', 'M', '1978-01-01', 'Son'),
('123456789', 'Alice', 'F', '1978-12-31', 'Daughter'),
('123456789', 'Elizabeth','F', '1957-05-05','Spouse');
Ujjwal Singh
0801IT211090
Q 3. Answer the following query :
a. List the name of all employees who work in department 5.
CODE:
SELECT FirstName,
LastName,DepartmentNumberFROMEmployee
WHERE DepartmentNumber = 5;
b. Listname and salaries of all employee ordered by salary.
CODE:
SELECTFirstName,LastName,
Salary FROM Employee
ORDER BY Salary DESC;
c. List name of all employees whose salary is between 30000 and 50000.
CODE:
SELECTFirstName,LastName
FROM Employee
WHERE Salary BETWEEN
Ujjwal Singh
0801IT211090
30000 AND 50000;
d. List the name and address of employee who lives in Houston.
CODE:
SELECTFirstName,LastName,
Address FROM Employee
WHEREAddressLIKE
'%Houston%';
e. List the name of employees who doesn't has supervisor.
CODE:
SELECTFirstName,LastName
FROM Employee
WHERESupervisionSSNIS
NULL;
f. List department number and number of employees in each department, ordered by number of
Ujjwal Singh
0801IT211090
employees in each department.
CODE:
SELECTDepartmentNumber,COUNT(*)AS
EmployeeCount FROM Employee
GROUPBYDepartmentNumberORDERBY
EmployeeCount DESC;
g. Listdepartmentnumberandnumberofemployeesindepartmentsthathavemorethan2
employees, ordered by department number.
CODE:
SELECTDepartmentNumber,COUNT(*)AS
EmployeeCount FROM Employee
GROUPBYDepartmentNumberHAVINGEmployeeCount
>2
ORDER BY DepartmentNumber;
h. List the ESSN of employees who works on project 3388 or project 1945.
CODE:
SELECT EMPLOYEESSN
FROM Works_on
WHERE PROJECTnUMBER IN (3388, 1945);
Ujjwal Singh
0801IT211090
i. List the location of department 1, 3, and 5.
CODE:
SELECT departmentNAME, DEPARTMENTNUMBER
FROM Department
WHERE departmentNUMBER IN (1, 3, 5);
j. List the name of all female employees.
CODE:
SELECTFirstName,LastNameFROMEmployee
WHERE Sex = 'F';