I. Page To Be Omitted: SQL Queries

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

SQL Queries

I.

PAGE TO BE OMITTED

a)

Software Development through Oracle

KR

SQL Queries

II.

JOINS

What are Joins ? A join is a query that combines rows from two or more tables, views, etc. (Eg) emp.deptno = dept.deptno Oracle performs a join whenever multiple tables appear in the query's FROM clause. The query's select list can select any columns from any of these tables specified in the FROM clause. Most join queries contain WHERE clause conditions that compare two columns, each from a different table. Such a condition is called a join condition. The different types of joins that can be performed are : Equi Join Non-Equi Join Outer Join Self Join 1. Equi Joins

Demonstrates Equi-join . Selects the department names along with the other details for every employee. Joins emp and dept tables.
a) Software Development through Oracle KR

SQL Queries

Query
SELECT empno, ename, emp.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno;

Query Analysis FROM emp,dept selects the specified column details from tables emp, dept. WHERE emp.deptno = dept.deptno is the join condition. Join condition contains an equality operator, hence termed as Equi join.
The equi join combines rows from emp and dept tables, that have

equivalent values for the specified column (deptno) in the join condition.

The rows that do not satisfy the join condition are not selected.
Common column is deptno.

The common column deptno is prefixed with tablename to avoid ambiguity.


The column in the join condition need not appear in the SELECT list.

Equi join is useful in querying highly normalized tables.


In addition to join conditions, the WHERE clause of a join query can also

contain other conditions that refer to columns of only one table.

These conditions can further restrict the rows returned by the join query. The following example proves this. 1. Non-equi joins

Illustrates Non-Equi Join. The query selects the name,department number and department names of those employees whose salary is greater than 3000.

a)

Software Development through Oracle

KR

SQL Queries

Query
SELECT ename, emp.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno AND sal > 3000;

Query Analysis WHERE emp.deptno = dept.deptno picks up the corresponding rows from emp and dept tables. sal > 3000, non equal condition (>) determines what rows have to be retrieved. An Equi join with a non-equal condition is termed as Non-Equi Join. 1. Outer Join

Illustrates Outer Join. Returns rows even when a join condition fails. Query
SELECT empno, ename, dept.deptno, dname FROM emp, dept WHERE emp.deptno(+) = dept.deptno;

Query Analysis WHERE emp.deptno (+)= dept.deptno is the outer join condition. (+) is the outer join operator. It indicates that the preceding column (emp.deptno) is the outer join column in a join. The query selects the number and name of those departments also, which do not have related records in emp table.
a) Software Development through Oracle KR

SQL Queries

In this outer join, Oracle returns a row containing the OPERATIONS department even though no employees work in this department. Returns NULL in the empno and ename columns for this row. Thus the query picks up the rows which otherwise, are eliminated by Equi joins. Value Add-ons The outer join extends the result of a simple join. Outer joins are used to find out the non-matching columns. The outer join operator (+) retrieves records even if one side of the join condition fails. The location of the operator is important for the query to succeed. It is placed on the side of the expression which may fail or may not exist. This causes the expression to retrieve all the records from the opposite table. (Eg) SELECT ename,dept.deptno,dname FROM emp,dept WHERE emp.deptno (+)= dept.deptno; The above query would select those records from dept table even if they do not have matching records in emp table. Any column in the SELECT list that fail the join condition will return NULL. Thus the above example would display null values for empno and ename for the non-matching record selected from dept table. The (+) operator can only appear in the WHERE clause, not in the select list. It can be applied only to a column of a table or view. If tables A and B are joined by multiple join conditions, the (+) operator

a)

Software Development through Oracle

KR

SQL Queries

must be used in all of these conditions. A condition containing the (+) operator cannot be combined with another condition using the OR logical operator. (Eg) SELECT empno, ename, dept.deptno, dname FROM emp, dept WHERE emp.deptno(+) = dept.deptno or dept.deptno=40; produces the error message, outer join operator (+) not allowed in operand of OR or IN. Cartesian Products If 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. This is termed as Cartesian Product. A Cartesian product always generates many rows and is rarely useful. For example, the Cartesian product of two tables each with a hundred rows generates ten thousand rows (100 * 100). In other words every row of a table is joined with every other (all) rows of the other table(s). Always include a join condition unless you specifically need a Cartesian product. The following example returns the cartesian product of the emp and dept tables. (Eg) SELECT ename,emp.deptno,dname FROM emp,dept; (no join condition) 1. Self Join

Illustrates the way to a join a table to itself. Displays every employee's number, name and his/her manager's number

a)

Software Development through Oracle

KR

SQL Queries

and name. In order to query an employee and his/her manager the emp table is joined to itself. Uses table aliases to represent the same table as two different tables. Query
SELECT x.empno "EmpNo", x.ename "EmpName",y.empno "MgrNo",y.ename "MgrName" FROM emp x, emp y WHERE x.mgr = y.empno;

Query Analysis Before we analyze the program, let us look at the following illustration which joins two tables X and Y which are exact copies of emp. SELECT X.ename,Y.ename FROM X,Y WHERE X.mgr = Y.empno; We know that the mgr column contains values that are present in the empno column since a non-existing employee cannot be a manager. X.mgr = Y.empno is an equi join condition. Thus X.ename returns the name of the employee from the X table and Y.ename returns the name of the employee from the Y table. The name returned by Y.ename would be the manager name due to the join condition. The following figure explains this concept.

X table X table

Y table

a)

Software Development through Oracle

KR

SQL Queries

Empno 7369 7499 7902 7521

Ename Smith Allen Ford Ward

Mgr 7902 7698 7839 7698

Empno 7369 7499 7902 7521

Ename Smith Allen Ford Ward

Mgr 7902 7698 7839 7698

X.empno 7369

X.ename Smith

Y.empno(X.mgr) Y.ename 7902 Ford

The query SELECT x.empno "EmpNo",x.ename "EmpName" , y.empno "MgrNo",y.ename "MgrName" works in a similar way. FROM emp x, emp y refers the single table emp as x and y for the purpose of the join.
The emp table appears twice in the FROM clause and is followed by table

aliases, that are used to qualify column names in the join condition.

x and y are termed as table aliases. WHERE x.mgr = y.empno creates a join between the mgr column of one emp table with the empno of another copy of emp table. This condition joins a table to itself. Hence termed as Self Join. 1. Another Illustration for Self Join

Selects the employees who get salary greater than their superiors. Use of Self Join and a non-equal condition. Query
SELECT x.ename "EmpName",x.sal "EmpSal",y.ename "MgrName",y.sal "MgrSal" FROM emp x,emp y WHERE x.mgr = y.empno AND x.sal > y.sal;

a)

Software Development through Oracle

KR

SQL Queries

Query Analysis WHERE x.mgr = y.empno joins the mgr column with the empno column. x.sal > y.sal compares the salary of each employee with his manager's salary. Thus the query selects those employees who get more salary than their managers. Set operators -UNION, INTERSECT, MINUS Set operators combine the results of two component queries into a single result. Queries containing set operators are called compound queries. UNION, INTERSECT,MINUS are the set operators which are used to create compound queries. Data of Emp and Un_emp table (used in the following examples) are tabulated below.

Emp Table
7369 7499 7566 7654 7698 7782 SMITH ALLEN JONES MARTIN BLAKE CLARK 20 30 20 30 30 10 7369 7499 7566 7654 7698 7782

Un_emp Table
SMITH ALLEN JONES MARTIN BLAKE CLARK 20 30 20 30 30 10

a)

Software Development through Oracle

KR

SQL Queries 7788 7839 7521 7844 7876 7900 7902 7934 1111 SCOTT KING WARD TURNER ADAMS JAMES FORD MILLER IN_EMP 20 10 30 30 20 30 20 10 10 7788 7839 7521 7844 7876 7900 7902 7934 SCOTT KING WARD TURNER ADAMS JAMES FORD MILLER IN_UNEMP 20 10 30 30 20 30 20 10 20

10

2222

1.

The UNION operator

Illustrates the use of UNION operator. Selects unique rows from two tables emp and un_emp. Query
SELECT empno, ename, deptno FROM emp UNION SELECT empno, ename, deptno FROM un_emp;

Query Analysis UNION selects distinct rows from either of the queries. Any records that are identical in both the tables are returned only once. i.e., it avoids duplication of rows. The columns forming the rows in one query should have similar meaning with respect to the corresponding columns from the other query. The corresponding column names can be different. But the datatypes of the columns should be the same.

a)

Software Development through Oracle

KR

SQL Queries

11

Value Add-ons A query with a UNION operator can have only one ORDER BY clause. The ORDER BY clause should be placed at the end of the query. Column position should be mentioned in the ORDER BY clause for ordering the rows. (Eg) SELECT empno, ename, deptno FROM emp UNION SELECT empno, ename, deptno FROM un_emp ORDER BY 1; (1 represents empno) 1. The UNION ALL operator

Selects all the rows from the specified tables. Query


SELECT empno, ename, deptno FROM emp UNION ALL SELECT empno, ename, deptno FROM un_emp;

Query Analysis UNION ALL selects all rows from emp and un_emp tables. Duplicate rows are also included. All the rows selected by either query including duplicates are displayed. 1. The INTERSECT operator

Illustrates the use of INTERSECT operator. Displays the rows which are common to both emp and un_emp tables.

a)

Software Development through Oracle

KR

SQL Queries

12

Query
SELECT empno, ename, deptno FROM emp INTERSECT SELECT empno, ename, deptno FROM un_emp;

Query Analysis INTERSECT selects the rows present in common within the emp and un_emp tables. Thus the operator selects only those rows returned by both the queries. 1. The MINUS operator

Illustrates the use of MINUS operator. Displays all distinct rows selected by the first query but not the second. Query
SELECT empno, ename, deptno FROM emp MINUS SELECT empno, ename, deptno FROM un_emp; SELECT empno, ename, deptno FROM un_emp MINUS SELECT empno, ename, deptno FROM emp;

Query Analysis SELECT ... FROM emp MINUS SELECT ... FROM un_emp displays the rows of emp which are not present in un_emp. SELECT ... FROM un_emp MINUS SELECT ... FROM emp displays the rows of un_emp which are not present in emp.

a)

Software Development through Oracle

KR

SQL Queries

13

Value Add-ons The number of columns and the datatype should be the same in a query using set operators. All set operators have equal precedence. If a SQL statement contains multiple set operators, Oracle evaluates them from the left to right if no parentheses explicitly specify another order. A future version of Oracle may give the INTERSECT operator greater precedence than the other set operators.

Table of Contents
Page to be omitted............................................................................................................1 JOINS.............................................................................................................................41 Equi Joins..............................................................................................................41 Non-equi joins.......................................................................................................42 Outer Join..............................................................................................................43 Self Join.................................................................................................................45 Another Illustration for Self Join...........................................................................47

a)

Software Development through Oracle

KR

SQL Queries

14

The UNION operator............................................................................................48 The UNION ALL operator ...................................................................................49 The INTERSECT operator...................................................................................50 The MINUS operator 50

a)

Software Development through Oracle

KR

You might also like