You are on page 1of 17

Database Management System(DBMS)

Lab Assignment-2
~Soumajit Pal
21BCE7214
1)Create Table Employee with attributes FirstName, LastName,
SSN, Address , Salary, Birthday, Sex, SupervisorSSN,
DepartmentNo.
Query-
CREATE TABLE Employee (
FirstName varchar,
LastName varchar,
SSN bigint,
Address varchar,
Salary bigint,
Birthday date,
Sex varchar,
SupervisorSSN bigint,
DepartmentNo int
);
2) Create a Table Department with attributes DNo, DNAME,
ManagerSSN, MgrStartdate.
Query-
create table Department(
DNo int,
DNAME varchar,
ManagerSSN bigint,
MgrStartDate varchar
);

3) Insert the data given above in both employee and


department tables.
Query(Table-Employee)-
insert into Employee values("Ryan","Taylor",925820850,"New
York",50000,"1990-12-05","Male",554433221,3);
insert into Employee
values("Trevor","Smith",200945020,"Boston",75000,"1980-05-
23","Male",924585380,4);
insert into Employee values("Alicia","Paul",804822349,"Salt
Lake",60000,"1959-03-29","Female",345702220,2);
insert into Employee
values("Roger","Jackson",785012053,"Chicago",27000,"2000-
11-21","Male",333445555,5);
insert into Employee
values("Joyce","Bradley",875252050,"Philadelphia",100000,"19
75-12-25","Male",503099283,1);
Query(Table-Department)-
insert into Department values(2,"Marketing",54433221,"06-
JUN-2000");
insert into Department values(1,"Sales",333445555,"09-APR-
2010");
insert into Department values(5,"Finance",928223413,"28-
NOV-2005");
insert into Department values(4,"IT",570250202,"18-SEP-
2007");
insert into Department
values(3,"Administration",347580852,"15-FEB-2002");

4)Display all the employees’ information.


Query-
select*from Employee
Output-
5) Display Employee name along with his SSN and Supervisor
SSN.
Query-
select FirstName,LastName,SSN,SupervisorSSN
from Employee

Output-

6)Display the employee names whose bdate is 29-MAR-1959.


Query-
select FirstName,LastName
from Employee
where Birthday="1959-03-29"
Output-
7) Display salary of the employees without duplications.
Query-
select distinct Salary
from Employee

Output-

8) Display the MgrSSN,MgrStartDate of the manager of


‘Finance’ department.
Query-
select ManagerSSN,MgrStartDate
from Department
where DNAME="Finance"
Output-

9) Modify the department number of an employee having fname


as ‘Joyce’ to 5.
Query-
update Employee
set DepartmentNo=5
where FirstName="Joyce";

select*from Employee
Output-
10) Alter Table department add column DepartmentPhoneNum
of NUMBER data type and insert values into this column only.
Query-
alter table Department
add DepartmentPhoneNum int;

insert into Department values(2,"Marketing",54433221,"06-


JUN-2000",9731691919);
insert into Department values(1,"Sales",333445555,"09-APR-
2010",9834812020);
insert into Department values(5,"Finance",928223413,"28-
NOV-2005",8914100101);
insert into Department values(4,"IT",570250202,"18-SEP-
2007",8284918419);
insert into Department
values(3,"Administration",347580852,"15-FEB-
2002",9491491849);

select*from Department

Output-
11) Alter table department to modify the size of
DepartmentPhoneNum.
Query-
alter table Department
alter column DepartmentPhoneNum bigintint;

12) Modify the field name DepartmentPhoneNum of


departments table to PhNo.
Query-
alter table Department
rename column DepartmentPhoneNum to PhNo;
Output-

13) Rename Table Department as DEPT.


Query-
alter table Department
rename to DEPT;
14) Alter Table department remove column PhNo.
Query-
alter table DEPT
drop column PhNo;

Output-

15) Create a table COPYOFDEPT as a copy of the table DEPT.


Query-
create table COPYOFDEPT
as select*from DEPT;
Output-
16) Delete all the rows from COPYOF DEPT table.
Query-
delete from COPYOFDEPT;
select*from COPYOFDEPT

Output-

17) Remove COPYOF DEPT table.


Query-
drop table COPYOFDEPT;
select*from COPYOFDEPT

Output-
18) Add Foreign Keys using Alter Table.
Query-
alter table Employee
add foreign key(SupervisorSSN)
references DEPT(ManagerSSN)

19) Drop Foreign key defined on SuperSSN and add it using


Alter table command.
Query-
alter table Employee
drop foreign key (SupervisorSSN);

alter table Employee


add foreign key (SupervisorSSN)
references DEPT(ManagerSSN)

20) Find the employee names having salary greater than


Rs.25000.
Query-
select FirstName,LastName
from Employee
where Salary>25000
Output-

21) Find the employee names whose salary lies in the range
between 30000 and 70000.
Query-
select FirstName,LastName
from Employee
where Salary>30000 and Salary<70000;

Output-
22) Find the employees who have no supervisor.
Query-
select FirstName,LastName
from Employee
where SupervisorSSN is null;

Output-

23) Display the bdate of all employees in the format


‘DDthMonthYYYY’.

24) Display the employee names whose bdate is on or before


1978.
Query-
select FirstName,LastName
from Employee
where Birthday<='1978-12-31';
Output-
25) Display the employee names having ‘Salt Lake’ in their
address.
Query-
select FirstName,LastName
from Employee
where Address="Salt Lake";

Output-

26) Display the department name that starts with ’M’.


Query-
select DNAME
from DEPT
where DNAME like 'M%';
Output-
27) Display the department names’ that ends with ‘E’.
Query-
select DNAME
from DEPT
where DNAME like '%E';

Output-

28) Display the names of all the employees having supervisor


with any of the followingSSN 554433221, 333445555.
Query-
select FirstName,LastName
from Employee
where SupervisorSSN= 554433221 or
SupervisorSSN=333445555;
Output-
29) Display all the department names in upper case and lower
case.
Query-
select upper(DNAME),lower(DNAME)
from DEPT;
Output-

30) Display the first four characters and last four of the
department names using substringfunction.
Query-
select substring(DNAME,1,4),substring(DNAME,-4)
from DEPT;
Output-

You might also like