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

Unit Iv

Uploaded by

Rauank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit Iv

Uploaded by

Rauank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

JOINS AND ORACLE

FUNCTIONS
UNIT-IV
GROUP FUNCTIONS
• These functions work on multiple records of a single column
and returns a single value.
• Example min(),max(),avg(),sum(),count().
To find sum of salary for all employees.
Ans:-select sum(sal) from emp;
COUNT() FUNCTION
• This function ignores null values of any column.
• While count(*) counts total number of record for that column
including null values.
Example:-
Select count(comm)from emp;
See the result.
Select Count(*)from emp;
See the result.
You will find the difference.
MIN(),MAX()& AVG() FUNCTION
• Example for these functions

• To find minimum, maximum,average salary for all


employees.
• Select min(sal),max(sal),avg(sal) from emp;
• See result for above statement.
GROUP BY CLAUSE
• This clause is used to group records having similar
value for a particular column so that group function
calculations can be done on those records.
• Example
• 1:-To know numbers of persons, working in different
department.
• Select deptno, count(*)from emp group by deptno;
• To find departmentwise sum and average of salary.
• Select deptno,sum(sal), avg(sal) from emp group by
deptno;
HAVING CLAUSE
• It is used to filter the result of group by clause. It is always
used with group functions.
• Having is similar to where clause but the difference is it filters
record before group by while having filter after group by.
Example
• Where deptno in(10,30) group by deptno having
count(empno)>3;
• We can not use group function directly, in where clause
because where clause filter record from tables.
SUB QUERIES
 It is a form of Sql statement that appears inside another sql statement.
 It is also termed as nested query.
 The statement containing sub query is called parent query.
Sub query can be used in following situations.
1) To insert record in a target table.
2) To create a table and insert record in table created.
3) To update record of a target table.
4) To create views.
5) To provide the value for the condition in where or having or in clause
used with select, update and delete command.
EXAMPLE OF SUB QUERY
Display all record of department which gives
highest salary.
Select * from emp where deptno=(select deptno from
emp where sal=(select max(sal) from emp));

Display name, salary ,comm. of all employee of


department in which SMITH is working.
Select ename, sal, comm from emp where
deptno=(select deptno from emp where
ename=‘SMITH’);
EXAMPLE OF SUB QUERY

Display all record of departments in which SMITH is not


working.
Select * from emp where deptno!=(select deptno from emp
where ename=‘SMITH’);
Display name, salary, comm. all employees of department
which having minimum salary earner employees.
Select ename,sal,comm from emp where deptno=(select
deptno from emp where sal=(select min(sal) from emp));
Select ename,sal,mgr from emp where comm=(select
min(comm) from emp);
SOME MORE EXAMPLES ON SUB QUERY
• Find maximum salary from emp table.
• Select max(sal) from emp;
• Find name and job of employee who is getting highest salary
for emp table.
• Select ename,job from emp where sal=(select max(sal) from
emp);
• Find name of the employee/employees who is getting second
highest salary.
• select ename from emp where sal=(select max(sal) from emp
where sal <> (select max(sal)from emp));
JOINS
JOINS
• Join conditions is used to retrieve record
from more than one table on the basis of
some condition.
in the scope of study we have following
joins
Equi join
self join
outer join
EQUI JOINS
• Using equi join we can get records from more than one table
on basis of equal to “=“ operators.
• Every record from one table gets compared with every
record from other table.
• Pre condition is columns data type and size must be same in
both tables on which we are going to join tables.
Example:- Display list of employee with their id, name, name
of department and location.
SQL>Select empno,ename,sal,dname,loc from emp,dept
where emp.deptno=dept.deptno;
SELF JOIN
With self join we can join a table to itself to
get required result.
Example:-
Display list of employees name with name of
their managers.
SQL> select e.ename"employee", m.ename
"manager" from emp e,emp m
where m.empno =e.mgr;

employee manager
---------- ----------
SMITH FORD
ALLEN BLAKE
WARD BLAKE
JONES KING
MARTIN BLAKE
BLAKE KING
CLARK KING
SCOTT JONES
TURNER BLAKE
ADAMS SCOTT
JAMES BLAKE
FORD JONES
MILLER CLARK
OUTER JOIN
• It is used to fetch those record which
don’t satisfy the equi join condition.
• This can be performed by outer join
operator(+).
• Select ename, job,dname,loc from
emp,dept where
emp.deptno(+)=dept.deptno;

You might also like