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

Dbmsexperiment 5

The document describes an experiment involving SQL queries to create tables, insert data, and retrieve data. It includes queries to: 1) Create department and employee tables and insert sample data. 2) Retrieve specific employee details using filters on employee ID, department, location, and salary. 3) Update employee and department records, including sorting, aggregating, and adding a new column.
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)
40 views9 pages

Dbmsexperiment 5

The document describes an experiment involving SQL queries to create tables, insert data, and retrieve data. It includes queries to: 1) Create department and employee tables and insert sample data. 2) Retrieve specific employee details using filters on employee ID, department, location, and salary. 3) Update employee and department records, including sorting, aggregating, and adding a new column.
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

EXPERIMENT 5

Objective:Solving problems using sub queries and aggregation

Software Used:Oracle 10g

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));

insert into DEPARTMENT_HUZAIFA values(2, ‘IT’,’Mumbai’);

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;

d)Write sql query to fetch employee details whose deptLocation is Pune.


Ans:
Query: select * from EMPLOYEE_HUZAIFA where DEPT_ID in (select DEPT_ID from
DEPARTMENT_HUZAIFA where DEPT_LOCATION=’Pune’);
e) Write sql query to fetch employee details whose salary is greater than 60000.
Ans:
Query: select * from EMPLOYEE_HUZAIFA where EMP_SALARY>60000;

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);

b) Write sql query to update deptLocation to Delhi where deptName is Marketing.


Ans:
Query: update DEPARTMENT_HUZAIFA set DEPT_LOCATION=’Dehli’ where DEPT_NAME=’Marketing’;
c) Write sql query to sort the employee in ascending order as per their salary.
Ans :
Query: select * from EMPLOYEE_HUZAIFA order by EMP_SALARY ASC;

d)Write sql query to fetch employee details having maximum salary.


Ans
Query: select max(EMP_SALARY) from EMPLOYEE_HUZAIFA;
e) Write a sql query to retrieve the average salary of an employee.
Ans
Query: select avg(EMP_SALARY) from EMPLOYEE_HUZAIFA;

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’);

You might also like