Difference between revisions of "Oracle:Joining tables"

From mi-linux
Jump to navigationJump to search
Line 1: Line 1:
 +
[[Main Page]] >> [[Oracle|Oracle and SQL]] >> [[Oracle_Workbook|Workbook]] >> Joining tables
 +
 
== Joining Tables Introduction ==
 
== Joining Tables Introduction ==
  
Line 66: Line 68:
 
  FROM EMP E, DEPT D  
 
  FROM EMP E, DEPT D  
 
  WHERE ENAME = 'ALLEN' AND E.DEPTNO = D.DEPTNO;
 
  WHERE ENAME = 'ALLEN' AND E.DEPTNO = D.DEPTNO;
 +
 +
Note: when two columns from different tables have the same name (i.e., DEPTNO in this case), you must use the table name prefixes to clarify exactly which columns you mean. Here, aliases have been used for the table names, e.g., E represents EMP and D DEPT. In this case the aliases work as a shorthand notation for the relevant tables. In other cases, for example, when comparing a table to itself (see later), they are vital.
 +
 +
To list information about all the employees in Chicago (without aliases):
 +
 +
SELECT DEPT.DEPTNO, DNAME, LOC, ENAME, JOB 
 +
FROM EMP, DEPT 
 +
WHERE EMP.DEPTNO = DEPT.DEPTNO AND LOC = 'CHICAGO';
 +
 +
The same query using table abbreviations/aliases:
 +
 +
SELECT D.*, ENAME, JOB
 +
FROM EMP E, DEPT D
 +
WHERE E.DEPTNO = D.DEPTNO AND LOC = 'CHICAGO';
 +
 +
Note that the abbreviations are defined in the FROM clause. Once defined they must be used in place of the table they are representing.
 +
 +
Columns can also be given aliases with the <code>AS new_column_name</code> clause. This should directly follow the column name in the SELECT clause. The new column name can not contains spaces, unless you use double quotes and can be a maximum of 10 characters.

Revision as of 16:43, 2 March 2016

Main Page >> Oracle and SQL >> Workbook >> Joining tables

Joining Tables Introduction

Sometimes information needs to be retrieved from more than one table. The relationships between rows in one table and rows in another are established by the values in certain corresponding columns (foreign key).

For example:

EMP   DEPT
EMPNO ENAME JOB ..... DEPTNO   DEPTNO DNAME ......
7499 ALLEN SALES ..... 30   30 SALES ......
  join attributes  

The table must have matching values in the join attributes to enable a join to take place. If there are no matching values, the tables will not join! For example, if 30 was missing from the DEPT table, then the employees from department 30 would not appear in any output that joins tables DEPT and EMP together. (A consequence of violating referential integrity!)


Basic SQL Structure For Joining Tables

The format when joining tables is:

SELECT some columns

FROM two or more tables

WHERE table1.col1 = table2.col2 [ AND table3.col3 = table4.col4 [AND ....]]

Note, two, or more tables can be joined in a SQL statement, but each join condition specifies the link between two tables only. If, for example, three tables appear in the FROM clause, there should normally be two join conditions.


Join Operators

Operator Description
= Equal
<> Not equal
> Greater than
>= Greater than, or equal to
< Less than
<= Less than, or equal to
BETWEEN lower-value AND higher-value A value between lower and higher
LIKE Pattern matching

Example Join Queries

To find ALLEN's location:

SELECT ENAME, LOC  
FROM EMP E, DEPT D 
WHERE ENAME = 'ALLEN' AND E.DEPTNO = D.DEPTNO;

Note: when two columns from different tables have the same name (i.e., DEPTNO in this case), you must use the table name prefixes to clarify exactly which columns you mean. Here, aliases have been used for the table names, e.g., E represents EMP and D DEPT. In this case the aliases work as a shorthand notation for the relevant tables. In other cases, for example, when comparing a table to itself (see later), they are vital.

To list information about all the employees in Chicago (without aliases):

SELECT DEPT.DEPTNO, DNAME, LOC, ENAME, JOB  
FROM EMP, DEPT  
WHERE EMP.DEPTNO = DEPT.DEPTNO AND LOC = 'CHICAGO';

The same query using table abbreviations/aliases:

SELECT D.*, ENAME, JOB 
FROM EMP E, DEPT D 
WHERE E.DEPTNO = D.DEPTNO AND LOC = 'CHICAGO';

Note that the abbreviations are defined in the FROM clause. Once defined they must be used in place of the table they are representing.

Columns can also be given aliases with the AS new_column_name clause. This should directly follow the column name in the SELECT clause. The new column name can not contains spaces, unless you use double quotes and can be a maximum of 10 characters.