Skip to content

Latest commit

 

History

History
178 lines (142 loc) · 5.31 KB

Database_SQL_Join.md

File metadata and controls

178 lines (142 loc) · 5.31 KB

Database SQL - Join

Overview

graph LR;
  A["Join"] --> B1["Inner Join"];
  A --> B2["Outer Join"];
  A --> B3["Cross Join"];
  A --> B4["Self Join"];
  B2 --> C1["Left Join (Left Outer Join)"];
  B2 --> C2["Right Join (Right Outer Join)"];
  B2 --> C3["Full Join (Full Outer Join)"];
  C1 --> D1["Left Join Excluding Inner Join"];
  C2 --> D2["Right Join Excluding Inner Join"];
  C3 --> D3["Full Join Excluding Inner Join"];
Loading

0_rFMChX4SAmQ9RzF9

Inner Join

  • Venn diagram

    Screenshot 2023-11-12 at 10 43 14 AM
  • SQL example

    SELECT * FROM TableA a
    INNER JOIN TableB b
    ON a.key = b.key;
  • Result example

    inner_join

Outer Join

Left Join

  • Venn diagram

    Screenshot 2023-11-12 at 4 30 24 PM
  • SQL example

    SELECT * FROM TableA a
    LEFT JOIN TableB b
    ON a.key = b.key;
  • Result example

    left_join

  • Notes

    • All the records from the left table will be included into the result, even if there is no matching on the right table.

Left Join Excluding Inner Join

  • Venn diagram

    Screenshot 2023-11-12 at 6 52 51 PM
  • SQL example

    SELECT * FROM TableA a
    LEFT JOIN TableB b
    ON a.key = b.key
    WHERE b.key IS NULL;
  • Result example

    left_join_excluding

Right Join

  • Venn diagram

    Screenshot 2023-11-12 at 7 07 27 PM
  • SQL example

    SELECT * FROM TableA a
    RIGHT JOIN TableB b
    ON a.key = b.key;
  • Result example

    right_join

Right Join Excluding Inner Join

  • Venn diagram

    Screenshot 2023-11-12 at 7 08 02 PM
  • SQL example

    SELECT * FROM TableA a
    RIGHT JOIN TableB b
    ON a.key = b.key
    WHERE a.key IS NULL;
  • Result example

    right_join_exluding

Full Join

  • Venn diagram

    Screenshot 2023-11-12 at 7 08 34 PM
  • SQL example

    SELECT * FROM TableA a
    FULL OUTER JOIN TableB b
    ON a.key = b.key;
  • Result example

    full_join

Full Join Excluding Inner Join

  • Venn diagram

    Screenshot 2023-11-12 at 7 09 20 PM
  • SQL example

    SELECT * FROM TableA a
    FULL OUTER JOIN TableB b
    ON a.key = b.key
    WHERE a.key IS NULL or b.key IS NULL;
  • Result example

    full_join_exluding

Cross Join

  • SQL example

    SELECT * FROM TableA a
    CROSS JOIN TableB b;
  • Result example

    cross_join

Self Join

  • SQL example
    SELECT * FROM TableA a1
    FULL OUTER JOIN TableA a2
    ON a1.column1 = a2.column2;
  • Result example
    • Original table
      ID Name ManagerID
      456 Robert
      383 Cook 222
      777 Daniel 222
      123 Smith 456
      222 William 456
    • Result
      ID Name ManagerID Manager
      456 Robert
      383 Cook 222 William
      777 Daniel 222 William
      123 Smith 456 Robert
      222 William 456 Robert