|
|
|
Join Queries
Joins
A join is a
query that combines rows from two or more tables, views, or materialized views.
Oracle performs a join whenever multiple tables appear in the query's
Join Conditions
Most join queries contain
EquijoinsAn equijoin is a join with a join condition containing an equality operator ( = ). An equijoin combines rows that have equivalent values for the specified columns. For example the following query returns empno,name,sal,deptno and department name and city from department table. select emp.empno,emp.ename,emp.sal,emp.deptno,dept.dname,dept.city from emp,dept where emp.deptno=dept.deptno; The above query can also be written like, using aliases, given below. select e.empno, e.ename, e.sal, e.deptno, d.dname, d.city from emp e, dept d where emp.deptno=dept.deptno; The above query can also be written like given below without using table qualifiers. select empno,ename,sal,dname,city from emp,dept where emp.deptno=dept.deptno; And if you want to see all the columns of both tables then the query can be written like this. select * from emp,dept where emp.deptno=dept.deptno;
Non Equi Joins.Non equi joins is used to return result from two or more tables where exact join is not possible. For example we have emp table and salgrade table. The salgrade table contains grade and their low salary and high salary. Suppose you want to find the grade of employees based on their salaries then you can use NON EQUI join. select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.lowsal and s.hisal Self Joins
A self join
is a join of a table to itself. This table appears twice in the
For example the following query returns employee names and their manager names for whom they are working. Select e.empno, e.ename, m.ename “Manager” from emp e, emp m where e.mgrid=m.empno Inner JoinAn inner join (sometimes called a "simple join") is a join of two or more tables that returns only those rows that satisfy the join condition. Outer JoinsAn outer join extends the result of a simple join. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other satisfy the join condition.
For example the following query returns all the employees and department names and even those department names where no employee is working. select e.empno,e.ename,e.sal,e.deptno,d.dname,d.city from emp e, dept d where e.deptno(+)=d.deptno; That is specify the (+) sign to the column which is lacking values. Cartesian ProductsIf two tables in a join query have no join condition, Oracle returns their Cartesian product. Oracle combines each row of one table with each row of the other. A Cartesian product always generates many rows and is rarely useful. For example, the Cartesian product of two tables, each with 100 rows, has 10,000 rows. Always include a join condition unless you specifically need a Cartesian product. |
|
|