0% found this document useful (0 votes)
160 views

18bit0166 Ayush Kanaujia

The document describes a database schema with tables for Employees, Departments, and Projects. It includes the table structures and sample data. A series of SQL queries and commands are provided to demonstrate data definition, data manipulation, and other database operations on the sample data. These include queries to find department counts, minimum/maximum salaries, averages, counts of senior employees, listings grouped by department and name, and subqueries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

18bit0166 Ayush Kanaujia

The document describes a database schema with tables for Employees, Departments, and Projects. It includes the table structures and sample data. A series of SQL queries and commands are provided to demonstrate data definition, data manipulation, and other database operations on the sample data. These include queries to find department counts, minimum/maximum salaries, averages, counts of senior employees, listings grouped by department and name, and subqueries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Database Management System Lab Digital Assignment-2

18BIT0166 Ayush Kanaujia

Aim: To study Data Definition and Data Manipulation commands.


Consider the following schema:
Table Name: Employee
Attribute Data Type
First Name VARCHAR(15)
Mid Name CHAR(2)
Last Name VARCHAR(15)
SSN Number CHAR(9)
Birthday DATE
Address VARCHAR(50)
Sex CHAR(1)
Salary NUMBER (7)
Supervisor SSN CHAR(9)
Department Number NUMBER (5)

Table Name: Department

Attribute Data Type


Department Name Varchar(15)
Department Number Number(5)
ManagerSSN CHAR(9)
ManageStartDate DATE

Table Name: Project


Attribute Data Type
Project Name VARCHAR(15)
Project Number NUMBER(5)
Project Location VARCHAR(15)
Department Number NUMBER(5)
Data For Employee Table
Mini
FName Lname SSN Bdate Address Sex Salary SuperSSN DepNo
t
11 S 59 E, Salt
Doug E Gilbert 554433221 09-JUN-60 M 80000 NULL 3
Lake City, UT
35 S 18 E, Salt
Joyce PAN 543216789 07-FEB-78 F 70000 NULL 2
Lake City, UT
08-DEC- 638 Voss,
Frankin T Wong 333445555 M 40000 554433221 5
45 Houston, TX
291 Berry,
Jennifer S Wallace 987654321 20-JUN-31 F 43000 554433221 4
Bellaire, TX
731 Fondren,
John B Smith 123456789 09-JAN-55 M 30000 333445555 5
Houston, TX
975 Fire Oak,
Ramesh K Narayan 666884444 15-SEP-52 M 38000 333445555 5
Humble, TX
5631 Rice,
Joyce A English 453453453 31-JUL-62 F 25000 333445555 5
Houston, TX
10-NOV- 450 Stone,
James E Borg 888665555 M 55000 543216789 1
27 Houston, TX
3321 Castle,
Alicia J Zelaya 999887777 19-JUL-58 F 25000 987654321 4
Spring, TX
29-MAR- 980 Dallas,
Ahmad V Jabbar 987987987 M 25000 987654321 4
59 Houston, TX

Data For Department table


Dname DepN MgrSSN MgrStartDat
o e
Manufacture 1 888665555 19-JUN-71
Administration 2 543216789 04-JAN-99
Headquarter 3 554433221 22-SEP-55
Finance 4 987654321 01-JAN-85
Research 5 333445555 22-MAY-78

Data For Project


Pname Pnumber Plocation DepNo
ProjectA 3388 Houston 1
ProjectB 1945 Salt Lake City 3
ProjectC 6688 Houston 5
ProjectD 2423 Bellaire 4
ProjectE 7745 Sugarland 5
ProjectF 1566 Salt Lake City 3
Project 1234 New York 2
G
ProjectH 3467 Stafford 4
ProjectI 4345 Chicago 1
ProjectJ 2212 San Francisco 2
Exercise: IV
Group Functions
1. How many different departments are there in the ‘employee’ table
2. For each department display the minimum and maximum employee salaries
3. Print the average annual salary.
4. Count the number of employees over 30 age.
5. Print the Department name and average salary of each department.
6. Display the department name which contains more than 30 employees.
7. Calculate the average salary of employees by department and age
8. Count separately the number of employees in the finance and research department.
9. List out the employees based on their seniority.
10. List out the employees who works in ‘manufacture’ department group by first
name

1. How many different departments are there in the ‘employee’ table.

SQL COMMAND:
select COUNT(distinct DepNo) from employee;

OUTPUT:
COUNT(DISTINCTDEPNO)
--------------------
5

2. For each department display the minimum and maximum employee salaries.

SQL COMMAND:
select min(salary),max(salary) from employee group by DepNo order by DepNo;

OUTPUT:
MIN(SALARY) MAX(SALARY)
----------- -----------
55000 55000
70000 70000
80000 80000
25000 43000
25000 40000

3. Print the average annual salary.

SQL COMMAND:
select avg(salary) from employee;

OUTPUT:
AVG(SALARY)
-----------
43100

4. Count the number of employees over 30 age.

SQL COMMAND:
SELECT count(ROUND((SYSDATE - TO_DATE(BDATE))/365.25, 2)) from employee where
ROUND((SYSDATE - TO_DATE(BDATE))/365.25, 2)>30;

OUTPUT:
COUNT(ROUND((SYSDATE-TO_DATE(BDATE))/365.25,2))
-----------------------------------------------
7
5. Print the Department name and average salary of each department.

SQL COMMAND:
select avg(employee.salary),Department.DName from employee join department on
employee.DepNo=Department.DepNo group by Department.DName;

OUTPUT:
AVG(EMPLOYEE.SALARY) DNAME
-------------------- --------------------
70000 Administration
33250 Research
80000 Headquarter
55000 Manufacture
31000 Finance

6. Display the department name which contains more than 30 employees.

SQL COMMAND:
select count(*),Department.DName from employee join department on
employee.depno=department.depno group by department.dname having
count(dname)>30;

OUTPUT:
no rows selected

7. Calculate the average salary of employees by department and age.


SQL COMMAND:
OUTPUT:
8. Count separately the number of employees in the finance and research department.

SQL COMMAND:
select count(*),Department.DName from employee join Department on
employee.DepNo=Department.DepNo where DNAME='Finance' or DNAME='Research'
group by Department.DNAME;

OUTPUT:
COUNT(*) DNAME
-------------- --------------------
4 Research
3 Finance

9. List out the employees based on their seniority.


SQL COMMAND:
SELECT FName,BDATE from employee order by BDATE;
OUTPUT:
FNAME BDATE
---------- ---------
Ramesh 15-SEP-52
John 20-JAN-55
Alicia 19-JUL-58
Ahmad 29-MAR-59
Doug 09-JUN-60
Joyce 31-JUL-62
Joyce 07-FEB-78
James 10-NOV-27
Jennifer 20-JUN-31
FrankliN 08-DEC-45

10. List out the employees who works in ‘manufacture’ department group by first name.

SQL COMMAND:
select employee.FName, Department.DName from employee join department on
employee.DepNo = Department.DepNo where Department.DName='Manufacture';

OUTPUT:
FNAME DNAME
---------- --------------------
James Manufacture

Sub Query and View

1. Find the employee who is getting highest salary in the department research.

SQL>
SELECT FNAME,LNAME FROM employee WHERE SALARY=(SELECT MAX(SALARY)
FROM employee LEFT JOIN department ON employee.DepNo=department.DepNo WHERE
department.DName='Research');

OUTPUT :-
FNAME LNAME
---------- ----------
Frankin Wong

2. Find the employees who earn the same salary as the minimum salary for each
Department.

SQL> SELECT FNAME,LNAME,SALARY FROM employee WHERE SALARY = ANY


(SELECT MIN(SALARY) FROM employee LEFT JOIN department ON
employee.DepNo=department.DepNo GROUP BY department.DName); OUTPUT :-
FNAME LNAME SALARY
---------- ---------- ----------
Doug Gilbert 80000
Joyce PAN 70000
Joyce English 25000
James Borg 55000
Alicia Zelaya 25000
Ahmad Jabbar 25000

6 rows selected.

3. Find the employee whose salary is greater than average salary of department 2.

SQL> SELECT FNAME,LNAME FROM employee WHERE SALARY>=(SELECT


AVG(SALARY)FROM employee WHERE DepNo=2);

OUTPUT:-
FNAME LNAME
---------- ----------
Doug Gilbert
Joyce PAN

4. List out all the department names with their individual employees strength.

SQL> SELECT DName,COUNT(employee.DepNo) FROM employee LEFT JOIN


department ON employee.DepNo=department.DepNo GROUP BY
DName,employee.DepNo;

OUTPUT:- Administration 1
Headquarter 1
Manufacture 1
Finance 3
Research 4

DNAME COUNT(EMPLOYEE.DEPNO)
------------------------- ---------------------

5. Find out the department name having highest employee strength.

SQL>
SELECT DName FROM department WHERE DepNo=(SELECT DepNo FROM (SELECT
DepNo,count(DepNo) FROM employee GROUP BY DepNo ORDER BY count(DepNo)
DESC) WHERE ROWNUM=1);
OUTPUT:-
DNAME
-------------------------
Research

6. List out all the departments and average salary drawn by their employees.

SQL> SELECT DName,AVG(SALARY) FROM employee LEFT JOIN department ON


employee.DepNo=department.DepNo GROUP BY DName;

OUTPUT:-
DNAME AVG(SALARY)
------------------------- -----------
Administration 70000
Research 33250
Headquarter 80000
Manufacture 55000
Finance 31000

7. Find maximum average salary for each department.

SQL> SELECT DName,AVG(SALARY) AS avg FROM employee LEFT JOIN department


ON employee.DepNo=department.DepNo GROUP BY DName ORDER BY avg DESC;

OUTPUT:-
DNAME AVG
------------------------- ----------
Headquarter 80000
Administration 70000
Manufacture 55000
Research 33250
Finance 31000
8. Create a view to display the employee details who is working in IT department.

SQL> CREATE VIEW emp AS SELECT e.FNAME,e.LNAME FROM employee e WHERE


e.DepNo=5; View created
SQL> SELECT * FROM emp;

OUTPUT :- FNAME
LNAME
---------- ----------
Frankin Wong
John Smith
Ramesh Narayan
Joyce English

9. Create a logical table to store employee details who is getting salary more than 10000.

SQL> CREATE OR REPLACE VIEW SAL AS SELECT FNAME,LNAME,SALARY,DepNo


FROM employee WHERE SALARY >
10000;
View created.
SQL> SELECT * FROM SAL;

OUTPUT :-
FNAME LNAME SALARY DEPNO
---------- ---------- ---------- ----------
Doug Gilbert 80000 3
Joyce PAN 70000 2
Frankin Wong 40000 5
Jennifer Wallace 43000 4
John Smith 30000 5 Ramesh
Narayan 38000 5
Joyce English 25000 5
James Borg 55000 1
Alicia Zelaya 25000 4 Ahmad
Jabbar 25000 4

10. Create a table to store the employees details based on the department no.

CREATE TABLE EmployeeNew(FName VARCHAR(10),


Minit VARCHAR(1),LName CHAR(10),SSN INT,BDate DATE,
Address CHAR(40),Sex CHAR(1),Salary INT,SuperSSN INT,
DepNo INT
);
JOINS:

1. Retrieve the names of all employees in department 5 who work more than 10 hours per
week on ProductX project.

SQL> SELECT DISTINCT FNAME,DepNo FROM employee LEFT JOIN Works_on ON


employee.SSN=Works_on.ESSN WHERE Works_on.HOURS>10.0 AND DepNo=5;

OUTPUT :-
FNAME DEPNO
---------- ----------
Ramesh 5
Joyce 5
John 5
Frankin 5

2. List the names of all employees who have a dependent with the same first name as
themselves.

SQL> SELECT FNAME FROM employee LEFT JOIN dependent ON


employee.SSN=dependent.ESSN
WHERE employee.FNAME=dependent.Dependent_name;

OUTPUT :- no
rows selected

3. Find the names of all the employees who are directly supervised by ‘Franklin Wong’.

SQL> SELECT FNAME FROM employee WHERE SUPER_SSN=(SELECT SSN FROM


employee WHERE FNAME='Frankin' AND LNAME='Wong');

OUTPUT :-
FNAME
----------
John
Ramesh
Joyce

4. Retrieve the names of all who do not work on any project.

SQL> SELECT FNAME FROM employee LEFT JOIN Works_on ON


employee.SSN=Works_on.ESSN WHERE NOT
EXISTS(SELECT * FROM employee WHERE SSN=Works_on.ESSN);

OUTPUT :-
FNAME
---------- Jennifer
James
Ahmad

5. Find the names and addresses of all employees who work on at least one project located
in Houston but whose department has no location in Houston.

SQL> SELECT LNAME,FNAME,ADDRESS FROM EMPLOYEE


WHERE EXISTS (SELECT * FROM Works_on,Project WHERE SSN=ESSN AND
PNO=PNUMBER AND PLocation='Houston') AND NOT EXISTS (SELECT * FROM
Dept_Loc,Project WHERE DepNo=Dep_No AND D_Location='Houston');

OUTPUT :- no
rows selected

6. List the names of all managers who have no dependents.

SQL> SELECT FNAME FROM employee LEFT JOIN dependent ON


employee.SSN=dependent.ESSN WHERE NOT EXISTS(SELECT * FROM employee
WHERE SSN=dependent.ESSN);

OUTPUT :-
FNAME
----------
Doug
Ramesh
Alicia
Joyce
James
Joyce
Ahmad
7 rows selected.

7. List the employee’s names and the department names if they happen to manage a
department.

SQL> SELECT FNAME,DNAME FROM employee LEFT JOIN department ON


employee.DepNo=department.DepNo WHERE employee.SSN=department.MgrSSN;

OUTPUT :-
FNAME DNAME
---------- -------------------------
Doug Headquarter
Joyce Administration
Frankin Research
Jennifer Finance
James Manufacture

8. For each project retrieve the project number, project name and the number of
employees who work on that project.

SQL> SELECT PName,PNumber,COUNT(*) FROM project LEFT JOIN Works_on


ON project.PNumber=Works_on.Pno GROUP BY PName,PNumber;

OUTPUT :-

PNAME PNUMBER COUNT(*)


--------------- ---------- ----------
Project B 1945 2
Project I 4345 1
Project F 1566 1
Project J 2212 3
Project E 7745 2
Project D 2423 1
Project G 1234 1
Project C 6688 1
Project A 3388 2
Project H 3467 1
10 rows selected.

9. For each project, list the project name and the total hours per week (by all employees)
spent on that project.

SQL> SELECT PNAME, SUM (HOURS) FROM PROJECT, WORKS_ON


WHERE PNUMBER=PNO GROUP BY PNAME;

OUTPUT :-
PNAME SUM(HOURS)
--------------- ----------
Project B 29
Project C 10
Project J 57
Project E 30
Project A 72.5
Project I 35
6 rows selected.
10. Retrieve the names of the employees who have 2 or more dependents.

SQL> SELECT FNAME,LNAME FROM employee


WHERE (SELECT COUNT(*) FROM dependent WHERE SSN=ESSN)>=2;

OUTPUT :- FNAME
LNAME
---------- ----------
Frankin Wong
John Smith

You might also like