Unit-3 SQL - Practical Demonstration
Unit-3 SQL - Practical Demonstration
Employee
Empname Varchar2(15)
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 -
_______________________END 24/09/2020
//END 30/9/2020//
06/10/2020 - JOINs
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);
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)
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;
VIEWS
Problem Statements and solution:
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)