Dbmsexperiment 5
Dbmsexperiment 5
Q1: Write sql query to Create department table and employee table and insert the data as given below:
Note: deptId is the primary key attribute in the Department table and foreign key attribute in employee table.
Department Table
Dept-Id Dept-Name Dept-Location
2 IT Mumbai
3 HR Pune
4 Production Pune
5 Marketing Mumbai
Employee Table
Emp-Id Emp-Name Emp-Salary Dept-Id
101 Aanand 50000 2
102 Prateek 45000 3
103 Suhail 60000 5
104 Sushmitha 70000 3
105 Mehleka 65000 5
106 Aslam 74000 4
Ans:
Query: create table DEPARTMENT_HUZAIFA (DEPT_ID numeric, DEPT_NAME char(20),DEPT_LOCATION
char(20),primary key (DEPT_ID));
EMPLOYEE_HUZAIFA
Query: create table EMPLOYEE_HUZAIFA(EMP_ID numeric, EMP_NAME char(20),EMP_SALARY
numeric,DEPT_ID numeric ,primary key (EMP_ID),constraint FK foreign key(DEPT_ID) references
DEPARTMENT_HUZAIFA (DEPT_ID));
insert into EMPLOYEE_HUZAIFA values(101,’Aanand’,50000,2);
Q2:
a)Write sql query to fetch employee details whose empId is 105.
Ans:
Query:select * from EMPLOYEE_HUZAIFA where EMP_ID=105;
b) Write a sql query to retrieve details of all employees who work in the HR department.
Ans:
Query : select * from EMPLOYEE_HUZAIFA where DEPT_ID=(select DEPT_ID from
DEPARTMENT_HUZAIFA where DEPT_NAME=’HR’);
c)Write sql query to fetch employee details whose deptId is 5.
Ans:
Query: select * from EMPLOYEE_HUZAIFA where DEPT_ID=5;
f) Write sql query to retrieve details of all employee who work in IT and HR.
Ans:
Query: select * from EMPLOYEE_HUZAIFA where DEPT_ID in ( select * from DEPARTMENT_HUZAIFA where
DEPT_ID=’IT’ ans DEPT_ID=’HR’);
g) Write a sql query to set the salary of Aanand equal to 60000.
Ans:
Query: update EMPLOYEE_HUZAIFA set EMP_SALARY=60000 where EMP_NAME=’Aanand’;
Q3.
a)Write sql query to retrieve details of all employees who work either in IT or HR or Marketing department.
Ans:
Query: select * from EMPLOYEE_HUZAIFA where DEPT_ID in ( select DEPT_ID from
DEPARTMENT_HUZAIFA where DEPT_NAME=’IT’ or DEPT_NAME=’HR’ or DEPT_NAME);
f)Write sql query to fetch details of all employees who do not work either in IT or HR.
Ans
Query: select * from EMPLOYEE_HUZAIFA where DEPT_ID not in (select DEPT_ID from
DEPARTMENT_HUZAIFA where DEPT_NAME='IT' or DEPT_NAME='HR');
g)Write a sql query to add a new column “HOD” in the department table.
Ans:
Query : update DEPARTMENT_HUZAIFA add HOD char(20);
h)Write a sql query to update the salary of all employees to 80000 who belong to the IT department.
Ans:
Query : update EMPLOYEE_HUZAIFA set EMPLOYEE_SALARY=80000 where DEPT_ID=(select DEPT_ID
from DEPARTMENT_HUZAIFA where DEPT_NAME=’IT’);