Oracle SQL JOIN Types – Part 2

365 Days Of Daily Coding: Day 19

Continuing with the Oracle SQL Join Types- Part 1 series, I have listed down the other 6 ‘JOIN’ types in SQL.

5. SEMI JOIN

SELECT *
FROM Table1 t1
WHERE EXISTS (SELECT 1
FROM Table2 t2
WHERE t1.fk = t2.id
);

“A semi-join is not the same thing at all: it returns a set of rows in one table that is constrained by the existence of data in some other table, without actually drawing any data from that other table. It’s implemented by ‘NOT IN’ or ‘EXISTS’.”

Output of the query:

idNamefkfk_table3
1Steve1–
4Fred1–
3Mary2–
2Aron3–
5Anne5–
6Beth81
Output of ‘Semi Join’

Difference between LEFT JOIN and SEMI JOIN

I found an excellent stackover flow post regarding the difference between what a ‘LEFT OUTER JOIN’ and ‘SEMI- JOIN’. The difference between the ‘LEFT OUTER JOIN’ and ‘SEMI-JOIN’ comes from the order of the execution of the queries. The latter being more efficient that a ‘LEFT OUTER JOIN’.

For instance: Example taken from the post

LEFT JOIN

SELECT DISTINCT s.id
FROM  students s
      LEFT JOIN grades g ON g.student_id = s.id
WHERE g.student_id IS NOT NULL

Order of Execution:

  1. FROM All the records from ‘student’ table is selected.
  2. JOIN The ‘JOIN’ clause returns every other rows combining both thee tables ‘student’ and ‘grades’.
  3. WHERE Records with the student_id is discarded or filtered out
  4. SELECTS All the records are selected after the ‘WHERE’ clause
  5. DISTINCT Only the distinct ids are returned after the records have been selected.

SEMI JOIN

SELECT s.id
FROM  students s
WHERE EXISTS (SELECT 1 FROM grades g
              WHERE g.student_id = s.id)

Order of Execution:

  1. FROM All the records from the table ‘student’ is selected.
  2. Each records are processed separately row by row.
  3. SUBQUERY The record is checked if there’s atleast one grade for that student.
  4. SELECT After all the records with the grade are identified, they are selected.

This order of execution may vary because of the SQL optimiser but the general idea is to under the sequence of the execution of the queries and why ‘SEMI JOIN’ is efficient compared to ‘LEFT OUTER JOIN’ in the above case.

6. ANTI SEMI JOIN

SELECT *
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1
FROM Table2 t2
WHERE t1.fk = t2.id
);

Anti Semi Join is exactly opposite of the Semi Join. For anti semi join, for every row in the first table with no match in the second table is returned.

Output of the above query:

IdNamefkfk_Table3
7Johnny–1
8Kerry–2
Output of ‘ANTI SEMI JOIN’

7. LEFT OUTER JOIN with exclusion – replacement for a NOT IN

SELECT *
FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.fk = t2.id
WHERE t2.id IS NULL;

Left Outer Join with exclusion is like the ‘ANTI SEMI JOIN’. The only difference would be the order of execution where the anti join could be more efficient than the former.

Output of the above query:

IdNamefkfk_Table3IdFavorite Color
7Johnny–1––
8Kerry–2––
Output of ‘LEFT OUTER JOIN’

8. RIGHT OUTER JOIN with exclusion – replacement for a NOT IN

SELECT *
FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.fk = t2.id
WHERE t1.fk IS NULL;

Much has been talked about this while talking about ‘SEMI JOIN’.

Output of the above query:

IdNamefkfk_Table3IdFavorite Color
––––4Pink
––––6Mauve
––––7Orange
Output of ‘RIGHT OUTER JOIN with exclusion – replacement for a NOT IN’

9. FULL OUTER JOIN

SELECT *
FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.fk = t2.id
ORDER BY t1.id;

A full outer join performs a join between two tables that returns the results of an INNER join as well as the results of a left and right outer join. source

Output of the above query:

IdNamefkfk_Table3IdFavorite Color
1Steve1–1Red
1Steve1–1Indigo
2Aron3–3Blue
3Mary2–2Green
4Fred1–1Red
4Fred1–1Indigo
5Anne5–5Purple
6Beth818Yellow
7Johnny–1––
8Kerry–2––
––––4Pink
6Mauve
7Orange
Output of ‘FULL OUTER JOIN’

10. CROSS JOIN, the Cartesian product

SELECT *
FROM Table1 t1
CROSS JOIN Table2 t2;

A CROSS JOIN is a JOIN operation that produces the Cartesian product of two tables but unlike ‘JOIN’ operation there is no ‘JOIN’ clause however a ‘WHERE’ clause can be specified with the ‘SELECT’. source

Output of the above query:

IdNamefkfk_Table3IdFavorite Color
1Steve1–1Red
2Aron311Red
3Mary–11Red
4Fred121Red
5Anne2–1Red
1Steve1–2Green
2Aron312Green
3Mary–12Green
4Fred122Green
5Anne2–2Green
1Steve1–3Blue
2Aron313Blue
3Mary–13Blue
4Fred123Blue
5Anne2–3Blue
1Steve1–4Pink
2Aron314Pink
3Mary–14Pink
4Fred124Pink
5Anne2–4Pink
Sample of output of ‘CROSS JOIN’

Leave a comment