SQL Joins CheatSheet - rnakidi/dsa GitHub Wiki
You often need to combine data from 𝗺𝘂𝗹𝘁𝗶𝗽𝗹𝗲 𝘁𝗮𝗯𝗹𝗲𝘀 𝘄𝗵𝗲𝗻 𝘄𝗼𝗿𝗸𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗱𝗮𝘁𝗮𝗯𝗮𝘀𝗲𝘀.
But how do we decide which join to use?
This 𝗦𝗤𝗟 𝗝𝗼𝗶𝗻𝘀 𝗖𝗵𝗲𝗮𝘁𝘀𝗵𝗲𝗲𝘁 simplifies the most common join types with 𝗰𝗹𝗲𝗮𝗿 𝘃𝗶𝘀𝘂𝗮𝗹𝘀 and sample SQL queries. Perfect for beginners, job interviews, or a quick refresher for experienced developers!
What are T1 and T2?
- 𝗧𝟭 (Table 1) is your primary table with data.
- 𝗧𝟮 (Table 2) is the secondary table you want to combine with T1.
✅ 𝗟𝗘𝗙𝗧 𝗝𝗢𝗜𝗡: Returns all rows from T1 and matches from T2. ✅ 𝗜𝗡𝗡𝗘𝗥 𝗝𝗢𝗜𝗡: Returns rows with matches in both T1 and T2. ✅ 𝗥𝗜𝗚𝗛𝗧 𝗝𝗢𝗜𝗡: Returns all rows from T2 and matches from T1. ✅ 𝗙𝗨𝗟𝗟 𝗢𝗨𝗧𝗘𝗥 𝗝𝗢𝗜𝗡: Returns all rows from both tables (matches and non-matches). ✅ 𝗨𝗡𝗜𝗢𝗡 𝗔𝗟𝗟: Combines rows from both tables without removing duplicates. ✅ 𝗘𝗫𝗖𝗟𝗨𝗦𝗜𝗢𝗡𝗦: Left Join Exclude T2, Right Join Exclude T1, etc.
Use 𝗜𝗡𝗡𝗘𝗥 𝗝𝗢𝗜𝗡 when you only need matches from both tables. Switch to 𝗟𝗘𝗙𝗧 𝗝𝗢𝗜𝗡 or 𝗥𝗜𝗚𝗛𝗧 𝗝𝗢𝗜𝗡 based on your needs for unmatched data.