Lab 4 – Joining Two or More Tables and SQL Sub-queries
1 JOINING TWO OR MORE TALBES
All the previous query examples that you have worked on are related to only one table. The join feature of SQL lets you select data from two or more tables and combine the selected data into a single query result.
1.1 Relationships Between Tables
The relationships between rows in one table and rows in another table are represented by the values stored in fields of those rows. For example, the EMPLOYEE table and the DEPARTMENT table each have a column DEPTNO that contains department numbers. It is the department numbers stored in both these columns that allows you to relate rows in the department table to rows in the employee table.
1.2 Selecting Data From Two Or More Tables
Let¡¯s say you want to know the location of the employee named ALLEN. Note that the EMP table does not contain a location (LOC) column but the DEPT table does. By looking at the employee table, you can see that ALLEN works for department 30. By looking at the DEPT table you can see that the department 30 is located in CHICAGO. Thus, you were able to relate one row in the EMP table to another row in the DEPT table by using data in a column present in both tables: the DEPTNO column. Similarly, Oracle can ¡°look¡± at data values stored in tables, and use those values to relate or join rows in one table to a row in other tables. You list the tables to be joined in the FROM clause and the relationship between the tables in the WHERE clause.
Find Allen¡¯s name from the EMP table and location of Allen¡¯s department from the DEPT table.(join.1)
SQL > SELECT ENAME, LOC
2 FROM EMP, DEPT
3 WHERE ENAME = ¡®ALLEN¡¯
4 AND EMP.DEPTNO = DEPT.DEPTNO;
The WHERE clause says to retrieve only the rows for employee ALLEN. The WHERE clause also specifies that if the department number of a row in the employee table is equal to the department number of a row in the department table (EMP.DEPTNO = DEPT.DEPTNO), then join the rows together. That is, join the ALLEN row from the EMP table to the department 30 row from the DEPT table. The SELECT clause directs that only the ENAME field from the EMP table and the LOC field from the DEPT table are to be retrieved from the joined row.
1.3 Prefixing Column Names With Table Names
Notice that the DEPTNO column name is prefixed with the table name EMP or DEPT (EMP.DEPTNO = DEPT.DEPTNO). This is because both the EMP table and the DEPT table have a column named DEPTNO. If a column name is unique among the tables listed in the FROM clause (ENAME for example) the column name need not be prefixed.
1.4 Equi-join
Join the DEPT table to the EMP table.(join.2)
SQL > SELECT DEPT.DEPTNO,DNAME,JOB,ENAME
2 FROM DEPT, EMP
1
3 WHERE DEPT.DEPTNO = EMP.DEPTNO 4 ORDER BY DEPT.DEPTNO;
There is only one search-condition in the WHERE clause in the above example and it specifies the relationship between the EMP table and the DEPT table. This special type of search-condition is called a join-condition. Specifically, this join is called an equi-join because the comparison operator in the join-condition is equals. In addition to the equi-join, there are several types of join-conditions, but most joins that you will do will be either equi-joins or outer-joins.
1.5 Using Table Labels To Abbreviate Table Names
Join queries can become rather tedious to type when column names have to be prefixed with table names.
List the department name and all the fields from the employee table for employees that work in Chicago. (join.3)
SQL > SELECT DNAME, EMPNO, ENAME, JOB, MGR, HIREDATE, 2 SAL, COMM, EMP.DEPTNO
3 FROM EMP, DEPT
4 WHERE EMP.DEPTNO = DEPT.DEPTNO
5 AND LOC = ¡®CHICAGO¡¯
6 ORDER BY EMP.DEPTNO;
SQL allows you to define a temporary label in the FROM clause by placing the label after the table name separated by a blank. You may then use these labels in place of the full table names within the query.
Issue the same query as the last example but use temporary labels to abbreviate the table names. (join.4)
SQL > SELECT DNAME, E.*
2 FROM EMP E, DEPT D
3 WHERE E.DEPTNO = D.DEPTNO 4 AND LOC = ¡®CHICAGO¡¯
5 ORDER BY E.DEPTNO;
In the example above the letter E refers to the EMP table and the letter D refers to the DEPT table. Also notice the use of E.* in the SELECT clause to retrieve all of the columns of the EMP table.
1.6 Joining A Table To Itself
You can use a table label for more than just abbreviating a table name in a query. It also lets you ¡°join¡± a table to itself as thought it were two separate tables.
For each employee whose salary exceeds his manager¡¯s salary, list the employees¡¯ names and salary and the manager¡¯s name and salary. (join.5)
SQL > SELECT WORKER.ENAME, WORKER.SAL, MANAGER.ENAME, 2 MANAGER.SAL
3 FROM EMP WORKER, EMP MANAGER
4 WHERE WORKER.MGR = MANAGER.EMPNO 5 AND WORKER.SAL > MANAGER.SAL;
In the above query, the EMP table is treated as if it were two separated tables named WORKER and MANAGER. Firstly all the WORKERs are joined to their
2
MANAGERs using the WORKER¡¯s manager¡¯s employee number (WORKER.MGR) and the MANAGER¡¯s employee number (MANAGER.EMPNO). For example, SCOTT¡¯s manager is JONES because SCOTT has a MGR column with a value of 7566 and JONES¡¯ EMPNO is 7566. The WHERE clause then eliminates all WORKER MANAGER pairs except those where the WORKER earn more than the manager (WORKER.SAL > MANAGER.SAL).
Note that the EMP table is not physically duplicated into two separate tables during the query processing.
1.7 Other Join Operator
All the examples so far show tables being joined using the equal comparison operator (including outer-join). As we said earlier, equal is by far the most common join- condition, but tables can be joined to another using any comparison operator including:
= Equal to
!= Not equal to
> Greater than
>= Greater than or equal to < Less than
<= Less than or equal to BETWEEN
LIKE
Find all the employees that earn more then JONES.(join.6)
SQL > SELECT X.ENAME, X.SAL, X.JOB, Y.ENAME, Y.SAL, Y.JOB
2 FROM EMP X, EMP Y
3 WHERE X.SAL > Y.SAL
4 AND Y.ENAME = ¡®JONES¡¯;
In the example above, the EMP table is joined to itself using the comparison operator greater than (>).
To demonstrate a BETWEEN join we will use a new table that contain salary grades. List the SALGRADE table. (join.7)
SQL > SELECT * FROM SALGRADE;
Now find the salary grade of each employee by joining the EMP table to the
SALGRADE table.(join.8)
SQL > SELECT GRADE, JOB, ENAME, SAL
2 FROM EMP, SALGRADE
3 WHERE SAL BETWEEN LOSAL AND HISAL 4 ORDER BY GRADE, JOB;
In the BETWEEN join, the SAL field of each row of the employee table is ¡°tested¡± to make sure it is both greater than or equal to LOSAL and less than or equal to HISAL of the SALOGRADE table.
1.8 Selecting All Possible Combinations of Rows
If the WHERE clause contains no join-condition at all, then all possible combinations of rows from tables listed in the FROM clause are displayed. This result, called a cartesian product, is normally not desired so that a join-condition is usually specified.
3