0% found this document useful (0 votes)
2 views14 pages

Unit-3 SQL - Practical Demonstration

The document outlines the schema and SQL operations for managing Employee and Department tables, including attributes, constraints, and various SQL commands such as CREATE, INSERT, DELETE, UPDATE, and SELECT. It also covers advanced SQL concepts like JOINs, GROUP BY, and subqueries, providing examples for each operation. Additionally, it includes problem statements with solutions related to employee data management.

Uploaded by

Punam Sindhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
2 views14 pages

Unit-3 SQL - Practical Demonstration

The document outlines the schema and SQL operations for managing Employee and Department tables, including attributes, constraints, and various SQL commands such as CREATE, INSERT, DELETE, UPDATE, and SELECT. It also covers advanced SQL concepts like JOINs, GROUP BY, and subqueries, providing examples for each operation. Additionally, it includes problem statements with solutions related to employee data management.

Uploaded by

Punam Sindhu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 14

Schema 1:

Employee

Attribute Datatype Constraint

Empid Varchar2(5) Primary key

Empname Varchar2(15)

Manager_id Varchar2(5) Foreign key emp(Empid)

City Varchar2
Dept_id Varchar2(5) Foreign key DeptDept_id)

DOB Date

Gender
Salary Number (10) Check >0

Department
Attribute Data Type Constraints
Dept_id Varchar2(5) Primary Key
Dept_name Varchar2(35) Not NULL
Dept_loc Varchar2(35) Not NULL
23/09/2020 – Create Table and Insert Data -

Create table Department


(
Dept_id varchar2(5) PRIMARY KEY,
Dept_name varchar2(35) Not Null,
Dept_Loc varchar2(35) not null
)
insert into Department values('DL_01', 'Finance', 'Delhi');
insert into Department values('MU_02', 'Finance', 'Mumbai');
insert into Department values('Cal_03', 'IT', 'Calcutta');
insert into Department values('HYD_04', 'HR', 'Hydrabad');

CREATE TABLE Employee


(
Emp_id varchar2(5) PRIMARY KEY,
E_name VARCHAR2(35),
Manager_id varchar2(7),
City VARCHAR2(20),
Dob date,
Gender VARCHAR(1),
Salary NUMBER(10),
Dept_id varchar2(7),
CONSTRAINT FK_mid FOREIGN KEY(Manager_id)REFERENCES
Employee(Emp_id),
CONSTRAINT FK_did FOREIGN KEY(Dept_id)REFERENCES
Department(Dept_id));

insert into Employee values('E_001','Komal','E_001','Mumbai','10-OCT-


99','F',40000,'HYD_04')
insert into Employee values('E_002','Monika','E_001','Mumbai','20-oct-
99','F',40000,'HYD_04')
insert into Employee values('E_003','Ajay','E_001','Delhi','20-sep-
99','M',30000,'HYD_04')
__________________________________________________________________
_________________END 23 sept
__________________________________________________________________

24/09/2020 – Delete, Insert, Update, Select


statements

Delete from Department where Dept_id='DL_01'


insert into Department values('DL_01', 'Finance', 'Delhi')
insert into Department values('MU_02', 'Finance', 'Mumbai')
insert into Department values('Cal_03', 'IT', 'Calcutta')
insert into Department values('HYD_04', 'HR', 'Hydrabad')

insert into Employee values('E_001','Komal','E_001','Mumbai','10-OCT-


99','F',40000,'HYD_04');
insert into Employee values('E_002','Monika','E_001','Mumbai','20-oct-
99','F',40000,'HYD_04');
insert into Employee values('E_003','Ajay','E_001','Delhi','20-sep-
99','M',30000,'HYD_04');
insert into Employee
values('E_004','Soham','E_001','Delhi',TO_DATE('17/12/2001',
'DD/MM/YYYY'),'M',30000,'Cal_03');

insert into Employee


values('E_006','Shruti','E_001','Delhi',TO_DATE('05/5/2002',
'DD/MM/YYYY'),'' ,30000,'Cal_03');
insert into Employee values('E_001','Komal','E_001','Mumbai','10-OCT-
99','F',40000,'HYD_04');
insert into Employee values('E_002','Monika','E_001','Mumbai','20-oct-
99','F',40000,'HYD_04');
insert into Employee values('E_003','Ajay','E_001','Delhi','20-sep-
99','M',30000,'HYD_04');
insert into Employee
values('E_004','Soham','E_001','Delhi',TO_DATE('17/12/2001',
'DD/MM/YYYY'),'M',30000,'Cal_03');
insert into Employee
values('E_005','Shruti','E_001','Delhi',TO_DATE('05/5/2002',
'DD/MM/YYYY'),'M' ,30000,'Cal_03');

insert into Employee


values('E_006','Shruti','E_001','Delhi',TO_DATE('08/2/2002',
'DD/MM/YYYY'),'M' ,40000,'HYD_04');
insert into Employee
values('E_007','Shruti','E_001','Delhi',TO_DATE('08/2/2002',
'DD/MM/YYYY'),'M' ,40000,'HYD_04');
insert into Employee
values('E_008','Sa','E_001','Jaipur',TO_DATE('12/2/2000',
'DD/MM/YYYY'),'M' ,40000,'HYD_04');
insert into Employee
values('E_009','Sruti','E_001','Jaipur',TO_DATE('12/2/2000',
'DD/MM/YYYY'),'M' ,40000,'HYD_04');

update Employee set city='Jaipur' where city='Delhi'


update Employee set gender='F' where Emp_id='E_006'
update Employee set salary=Salary+(salary*.1) where
city='Mumbai'
Delete from Employee where city='Delhi'
select Distinct E_name,salary from Employee
select * from Employee where city NOT in ('Mumbai')
Select * from Employee where Dob NOT between '9-OCT-99' and '17-
DEC-2001'
select * from Employee order by DOB

_______________________END 24/09/2020

30/09/2020 – Functions – SYSDATE, SUBSTR, ROUND,


NEXT_DAY, LPAD for Left padding, RPAD for right padding
Concatenation operator i.e pipe symbol - ||
Select sysdate,salary as updated_salary from Employee
Select SUBSTR(city,1,3) as substring_city from Employee
select E_name||TO_CHAR(Dob,‘DD/MM/YYYY’) from Employee
select SUBSTR(E_name,1,2)||'-'||substr(TO_CHAR(Dob),1,2) as
name_dob from Employee
select LPAD(salary,7,'0') from Employee
select Round(MONTHS_BETWEEN(sysdate,Dob)/12,0) from
Employee
select NEXT_DAY(sysdate,'Wed') from Employee

//END 30/9/2020//
06/10/2020 - JOINs

Select E_name, City, Salary,Employee.Dept_id, dept_name from


Employee JOIN Department on Employee.Dept_id=Department.Dept_id
SELECT Salary,E_name, city, Employee.Dept_id as
emp_Dept,Department.Dept_id as Dept_Deptid,Dept_name, Dept_Loc
FROM Employee JOIN Department ON
Employee.Dept_id=department.dept_id order by salary desc
Self Join:-
1. select E_name, City, Salary,
Employee.Dept_id,Department.Dept_id from Employee
JOIN Department on
Employee.Dept_id=Department.Dept_id

2. SELECT DISTINCT e1.E_name, e1.City, e1.Salary FROM


Employee e1 JOIN Employee e2 ON (e1.Dept_id =
e2.Dept_id) AND (e1.city = e2.city)
3. select e1.E_name as Employee_name, e2.E_name as
Manager_name from Employee e1 Inner join Employee e2
on e2.emp_ide1.emp_id
4. select e1.E_name as Employee_name, e2.E_name as
Manager_name from Employee e1 Inner join Employee e2
on e2.Manager_id=e1.Manager_id and
e2.emp_id<>e1.emp_id
5. SELECT distinct e1.E_Name AS EmployeeName1,
e2.E_Name AS EmployeeName2, e1.City as city1, e2.City
as city2 FROM Employee e1, Employee e2 WHERE
e1.Emp_id <> e2.Emp_id AND e1.City = e2.City ORDER
BY e1.City;
6. SELECT distinct e1.E_Name AS EmployeeName1,
e2.E_Name AS EmployeeName2, e1.City as city1, e2.City
as city2 FROM Employee e1, Employee e2 WHERE
e1.Emp_id <> e2.Emp_id AND e1.City = e2.City ORDER
BY e1.City;

update employee set salary=25000 where Emp_id='E_009'


select count(Distinct E_NAME) from employee
select * from employee
select Round(sum(salary)) from employee

Group BY--
1. select count(Emp_id), Dept_id from Employee group by
dept_id
2. SELECT d.Dept_Name, COUNT (emp_id) FROM department d
INNER JOIN Employee e ON d.dept_id=e.dept_id GROUP BY
d.Dept_name
3. SELECT last_name, department_name FROM employees e LEFT
OUTER JOIN departments d ON (e.department_id = d.department_id);

4. SELECT last_name, department_name FROM employees e RIGHT


OUTER JOIN departments d ON(e.department_id = d.department_id);

SELECT last_name, department_name FROM employees e FULL OUTER


JOIN departments d ON (e.department_id = d.department_id);
8/10/2020
Group by:
1. Group the data in employee table based on gender
and show the count in each group.
Sol:- select count(emp_id), Gender from Employee
where salary>=30000 group by gender having
gender='F'
2. Find the no of employees under each manager
Sol: select count(emp_id), MANAGER_ID from
Employee group by MANAGER_ID
3. Find the no of employees in each department
Sol: select count(emp_id), DEPT_ID from Employee
group by DEPT_ID
4. Find the no of employees in each city
Sol: select count(emp_id), city from Employee group
by city
5. Find department wise average salary
Sol: select round(avg(salary)), Dept_id from
Employee group by dept_id
6. Find maximum salary in each department
Sol: select round(max(salary)), Dept_id from
Employee group by dept_id
7. Write a select statement that group the result set by dept_id and display dept_id and average
total of those Departments where average salary more than 39000

Sol: SELECT
DEPT_ID,count(EMP_ID),round(avg(SALARY)) FROM
Employee GROUP BY dept_id having
avg(salary)>39000
8. Find the name and salary of employee who is
drawing maximum salary
Sol : select E_NAME, salary from employee where
salary =(select Max(salary) from employee)

9. Find employee name who is drawing second highest


salary.
Sol- select e_name from employee where
salary=(select max(salary) from employee where
salary<(select max(salary) from employee))

10. Find the employees name whose salary is more


than a particular employee in a department.
Sol - select E_NAME, salary from employee where
salary>(select salary from employee where e_name =
'Komal') and dept_id = (select dept_id from employee
where e_name = 'Komal')
11. Display department id which is having a
maximum average salary
Sol- SELECT dept_id, avg(salary) FROM employee
GROUP BY dept_id
HAVING avg(salary) in (SELECT max(avg(salary))
FROM employee GROUP BY dept_id);

Subqueries
12. Select * from Employee where DEPT_ID IN (select
DEPT_ID from Department where DEPT_loc = 'Delhi')
13. SELECT d.Dept_name, COUNT (emp_id) FROM
department d INNER JOIN Employee e ON d.dept_id=e.dept_id
GROUP BY d.dept_name HAVING COUNT (emp_id)>2;
14. SELECT d.Dept_name, emp_id FROM department d INNER
JOIN Employee e ON d.dept_id=e.dept_id GROUP BY
d.dept_name HAVING COUNT (emp_id)>2;

SELECT first_name, last_name, salary FROM employees WHERE salary >


SELECT salary FROM employees WHERE job_id = 'VICE-PRESIDENT');
SELECT first_name, last_name, salary FROM employees WHERE salary =
SELECT salary FROM employees WHERE job_id = 'VICE-PRESIDENT');
SELECT first_name, last_name, salary FROM employees WHERE job_id
= 'VICE-PRESIDENT');

VIEWS
Problem Statements and solution:

1. Display the total number of employees working in each department.


Sol - select count(*) as NoOfEmp, deptno from emp group by deptno;

2. List all the employees those who are getting the same salary.
SOL - select e.deptno, e.sal as sal1, e1.sal as sal2, e.ename as ename1, e1.ename as ename2
from emp e join emp e1 on e.sal=e1.sal and e.ename<>e1.ename

3. Display the name of the employee who gets the second highest salary in dept no 5.
SQL - Select max(sal) from emp where sal<(Select max(sal) from emp where deptno=20);

4. list the no of employees in each job. Include only those jobs with more than 3 employees
SQL -SELECT COUNT(empno), job FROM emp GROUP BY job HAVING COUNT(empno) > 3;

5. Display the name of each employee who draws the maximum salary in their respective
department.
SOL- select ename,deptno, sal from emp where sal in (select max(sal) from emp group by
deptno)

6. write a select statement that display records of employees working in the same department as
“James”

Sol- select ename,deptno, sal from emp where sal in (select max(sal) from emp group by
deptno)

You might also like