- Join two or more tables with an equi-join and qualify columns with table names or aliases.
- Recognise a Cartesian product and avoid it.
- Combine join conditions with normal conditions and ORDER BY.
- Write self-joins and outer joins using the (+) operator.
- Use the set operators UNION, UNION ALL, INTERSECT and MINUS and their rules.
Joining Tables
In a relational database the data is spread over several tables (storing everything in one table would cause redundancy). Joining combines two or more tables so that values from the columns of all of them can be retrieved. Tables can be joined only through a common column – e.g. CCODE for COURSES and BATCHES.
select bcode, b.ccode, name, fcode, stdate from batches b, courses c -- tables separated by commas; b and c are aliases where b.ccode = c.ccode; -- join condition (equi-join)
BCODE CCODE NAME FCODE STDATE ----- ----- ---------------- ----- --------- b1 ora Oracle database gk 12-JAN-01 b2 asp ASP.NET da 15-JAN-01 b3 c C programming hs 20-JAN-01 ...
- A join that uses the equal-to operator is an equi-join.
- A column present in more than one of the tables (CCODE) must be qualified with the table name or alias.
- A table alias is a short name for a table, valid only in the query where it is defined (it is not stored anywhere).
Cartesian Product, Join and Normal Conditions
- Without a join condition the result is the product (Cartesian product): every row of the first table joined with every row of the second – 6 courses × 7 batches = 42 rows. It is usually the result of an error.
- The condition that joins the tables is the join condition; other conditions in WHERE are normal conditions used to filter rows. ORDER BY can sort the joined result.
- Minimum number of join conditions = number of tables − 1 (more if several columns form the join).
select bcode, c.name course, f.name faculty, stdate from batches b, courses c, faculty f where b.ccode = c.ccode -- join condition 1 and b.fcode = f.fcode -- join condition 2 (3 tables -> 2 conditions) and enddate is not null -- normal condition order by stdate;
Self-Join
A self-join joins a table with itself: two copies of the table (two aliases) are joined as if they were different tables.
select b2.* from batches b1, batches b2 where b1.bcode = 'b3' -- anchor the first copy at batch b3 and b2.stdate > b1.stdate; -- batches that started after b3
Outer Join
A normal join returns only rows that have matching values in both tables. If a new course cs (C Sharp) has no batch, it is missing from the join. An outer join also returns parent rows that have no child rows by extending the other table with null rows.
select c.ccode, name, fee, bcode, stdate, enddate from courses c, batches b where c.ccode = b.ccode (+) -- (+) on the side that is extended with nulls order by c.ccode;
CCODE NAME FEE BCODE STDATE ENDDATE ----- ---------------- ---- ----- --------- --------- asp ASP.NET 5000 b2 15-JAN-01 05-MAR-01 c C programming 3500 b3 20-JAN-01 27-FEB-01 cs C Sharp 7000 java Java Language 4500 b5 05-APR-01 10-MAY-01 ...
Set Operators
Set operators combine the results of two queries into one result; they implement the union, intersection and difference operators of relational algebra.
| Operator | Returns |
|---|---|
| UNION | Unique rows of both queries (duplicates removed) |
| UNION ALL | All rows of both queries including duplicates |
| INTERSECT | Rows common to both queries |
| MINUS | Rows of the first query that are not in the second |
select ccode from oldcourses union select ccode from courses; -- ora and c appear only once select ccode from courses intersect select ccode from oldcourses; -- courses in both tables select ccode from oldcourses minus select ccode from courses; -- old courses no longer offered
Rules for set operators
- Both queries must select the same number of columns.
- Corresponding columns must have the same datatype (length and names may differ).
- The column names of the first query become the headings.
- NULLs are treated as identical – unlike normal comparisons, two NULLs match in set operations.
- A join needs a common column; n tables need at least n − 1 join conditions.
- Missing join condition = Cartesian product.
- Self-join uses two aliases of one table; outer join uses (+) to include unmatched rows.
- UNION removes duplicates, UNION ALL keeps them; INTERSECT and MINUS find common/different rows.
Chapter 9 Notes (PDF)
Question / Answer (15 questions)
Moderate-level questions based on the Chapter 9 notes (PDF). Click a question to see the answer.
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The chapter notes PDF is the course study material and is credited to its author below. The explanations, questions and MCQs on this page are original study aids written for this course from the topics covered in that PDF. The other works are cited for verification and further reading. Oracle, SQL*Plus and PL/SQL are trademarks of Oracle Corporation; other names belong to their respective owners.
- Srikanth, P. (n.d.). Oracle For Beginners (chapter notes – the PDF shown on each chapter page). Srikanth Technologies. http://www.srikanthtechnologies.com/
- Codd, E. F. (1970). A Relational Model of Data for Large Shared Data Banks. Communications of the ACM, 13(6), 377–387. https://doi.org/10.1145/362384.362685
- Oracle Corporation. (n.d.). Oracle Database SQL Language Reference, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/
- Silberschatz, A., Korth, H. F., & Sudarshan, S. (2019). Database System Concepts (7th ed.). McGraw-Hill Education.
- Elmasri, R., & Navathe, S. B. (2016). Fundamentals of Database Systems (7th ed.). Pearson.