Oracle ALTER TABLE MODIFY COLUMN
Oracle NVL() Function with Examples
Oracle SUBSTR() function with Examples
Oracle TO_DATE() with complete examples
Oracle DECODE function
Oracle INSTR() function with examples
Oracle TO_CHAR() function
Oracle TO_TIMESTAMP
A query nested within a query is known as subquery.
For example, you want to see all the employees whose salary is above average salary. For this you have to first compute the average salary using AVG function and then compare employees salaries with this computed salary. This is possible using subquery. Here the sub query will first compute the average salary and then main query will execute.
Select * from emp where sal > (select avg(sal) from emp);
Similarly we want to see the name and empno of that employee whose salary is maximum.
Select * from emp where sal = (select max(sal) from emp);
To see second maximum salary
Select max(sal) from emp where
sal < (select max(sal) from emp);
Similarly to see the Third highest salary.
Select max(sal) from emp where
sal < (select max(sal) from
emp where
sal < (select max(sal) from emp));
We want to see how many employees are there whose salary is above average.
Select count(*) from emp where
sal > (select max(sal) from emp);
We want to see those employees who are working in Hyderabad. Remember emp and dept are joined on deptno and city column is in the dept table. Assuming that wherever the department is located the employee is working in that city.
Select * from emp where deptno
in (select deptno from dept where
city=’HYD’);
You can also use subquery in FROM clause of SELECT statement.
For example the following query returns the top 5 salaries from employees table.
Select sal from (select sal from emp order sal desc)
where rownum <= 5;
To see the sum salary deptwise you can give the following query.
Select sum(sal) from emp group by deptno;
Now to see the average total salary deptwise you can give a sub query in FROM clause.
select avg(depttotal) from (select sum(sal) as depttotal from emp group by deptno);
The above average total salary department wise can also be achieved from Oracle Version 9i using WITH clause given below
WITH
DEPTOT AS (select sum(sal) as dsal from emp
group by deptno)
select avg(dsal) from deptot;
You can group query results on some column values. When you give a SELECT statement without group by clause then all the resultant rows are treated as a single group.
For Example, we want to see the sum salary of all employees dept wise. Then the following query will achieved the result
Select deptno,sum(sal) from emp group by deptno;
Similarly we want to see the average salary dept wise
Select deptno,avg(sal) from emp group by deptno;
Similarly we want to see the maximum salary in each department.
Select deptno,max(sal) from emp group by deptno;
Similarly the minimum salary.
Select deptno,min(sal) from emp group by deptno;
Now we want to see the number of employees working in each department.
Select deptno,count(*) from emp group by deptno;
Now we want to see total salary department wise where the dept wise total salary is above 5000.
For this you have to use HAVING clause. Remember HAVING clause is used to filter groups and WHERE clause is used to filter rows. You cannot use WHERE clause to filter groups.
select deptno,sum(sal) from emp group by deptno
having sum(sal) >= 5000;
We want to see those departments and the number of employees working in them where the number of employees is more than 2.
Select deptno, count(*) from emp group by deptno
having count(*) >=2;
Instead of displaying deptno you can also display deptnames by using join conditions.
For example we want to see deptname and average salary of them.
Select dname,avg(sal) from emp,dept
where
emp.deptno=dept.deptno group by dname;
Similarly to see sum of sal.
Select dname,sum(sal) from emp,dept
where emp.deptno=dept.deptno group by dname;
We want to see the cities name and the no of employees working in each city. Remember emp and dept are joined on deptno and city column is in the dept table. Assuming that wherever the department is located the employee is working in that city.
Select dept.city,count(empno) from emp,dept
where emp.deptno=dept.deptno
Group by dept.city;
Interface Computers Academy © 2007-2017 All Rights Reserved