JOINS
Mastering SQL Joins & Row Selection Techniques: A Practical Guide
Understanding how to retrieve specific records and combine data from multiple tables is a fundamental skill for any database developer or administrator. In this guide, we will break down how to fetch up to the $Nth$ record using Oracle SQL, followed by a comprehensive overview of all major SQL Join types with clear examples.
Part 1: How to Select Up to the Nth Record
When working with large datasets, you often need to limit your output or extract a specific range of records. Using Oracle's ROWNUM pseudo-column alongside the MINUS set operator allows you to isolate exact rows.
Sample Data (EMP Table)
| ID | NAME | DOJ | |
| 1 | KAMLESH | 07-MAY-15 | kamlesh.mishra@aol.com |
| 2 | RANJIT | 11-DEC-11 | kamlesh.mishra@aol.com |
| 3 | RAJESH | 11-FEB-01 | kamlesh.mishra@aol.com |
| 4 | SHIVAM | 01-FEB-21 | kamlesh.mishra@aol.com |
Query: Fetching the 3rd Record
SELECT * FROM EMP WHERE ROWNUM <= 3
MINUS
SELECT * FROM EMP WHERE ROWNUM < 3;
Output:
| ID | NAME | DOJ | |
| 3 | RAJESH | 11-FEB-01 | kamlesh.mishra@aol.com |
Part 2: Understanding SQL Joins
Joins are used to combine columns from multiple tables based on a related column between them.
1. Equi Join
An Equi Join is a conditional join that uses an equality operator (=) to match values from columns in two or more tables.
Tables: EMP & DEPARTMENT
SELECT ID, NAME, DOJ, DEPARTMENT.DNO, DEPARTMENT.DNAME
FROM EMP, DEPARTMENT
WHERE EMP.DNO = DEPARTMENT.DNO;
Output:
| ID | NAME | DOJ | DNO | DNAME |
| 1 | KAMLESH | 07-MAY-15 | 10 | HR |
| 3 | RAJESH | 11-FEB-01 | 20 | BPO |
| 4 | SHIVAM | 01-FEB-21 | 10 | HR |
2. Non-Equi Join
A Non-Equi Join matches records using comparison operators other than equality (such as >, <, >=, <=, or BETWEEN). It is frequently used for grading or range-matching data.
SELECT ID, NAME, DOJ, DNO, SALARY.SALARY, SALARY.GRADE
FROM EMP, SALARY
WHERE EMP.SALARY > SALARY.SALARY;
*(Yields 10 rows matching salaries against the threshold grade table ranges.)
3. Outer Joins
Outer joins return matched records as well as unmatched rows from one or both tables. In Oracle syntax, the outer join operator is denoted by (+).
A. Left Outer Join
Fetches all matching records plus all non-matching records from the left table.
SELECT ID, NAME, DOJ, SALARY, DEPARTMENT.DNO, DEPARTMENT.DNAME
FROM EMP, DEPARTMENT
WHERE EMP.DNO = DEPARTMENT.DNO(+);
B. Right Outer Join
Fetches all matching records plus all non-matching records from the right table.
SELECT ID, NAME, DOJ, SALARY, DEPARTMENT.DNO, DEPARTMENT.DNAME
FROM EMP, DEPARTMENT
WHERE EMP.DNO(+) = DEPARTMENT.DNO;
C. Full Outer Join
Combines the results of both Left and Right outer joins using the UNION operator to show all records from both tables.
SELECT ID, NAME, DOJ, SALARY, DEPARTMENT.DNO, DEPARTMENT.DNAME
FROM EMP, DEPARTMENT WHERE EMP.DNO = DEPARTMENT.DNO(+)
UNION
SELECT ID, NAME, DOJ, SALARY, DEPARTMENT.DNO, DEPARTMENT.DNAME
FROM EMP, DEPARTMENT WHERE EMP.DNO(+) = DEPARTMENT.DNO;
4. Self Join
A Self Join is used when a table must be joined with itself. This is exceptionally useful for hierarchical data structures, such as finding employee-manager relationships.
SELECT E.NAME, E.NO, E.MGR_ID, E1.NAME AS MANAGER_NAME
FROM EMP E, EMP E1
WHERE E.NO = E1.MGR_ID;
Output:
| NAME | NO | MGR_ID | MANAGER_NAME |
| ANJAY | 4 | 2 | KAMLESH |
| SUNIL | 3 | 1 | RAJESH |
| KAMLESH | 1 | 4 | SUNIL |
| RAJESH | 2 | 3 | ANJAY |
5. Cross Join (Cartesian Product)
A Cross Join returns the Cartesian product of the tables involved—meaning every row from the first table is paired with every row from the second table.
SELECT NAME, NO, MGR_ID, DNO, DNAME
FROM EMP, DEPARTMENT;
Output: (8 rows generated: 4 employees x 2 departments)
