TGViewer
Artificial Intelligence & ChatGPT Prompts Artificial Intelligence & ChatGPT Prompts @curiousprogrammer · 42.2K subscribers
Post #1716 628
JOINS

Definition

Joins in MySQL allow you to retrieve data from two or more tables based on a related column. They are used to combine rows from multiple tables.

Types of Joins

1. INNER JOIN:
- Returns rows where there is a match in both tables.
- Syntax:
            SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;


- Example:
            SELECT employees.name, departments.name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id;


2. LEFT JOIN (OUTER JOIN):
- Returns all rows from the left table and matching rows from the right table. Non-matching rows have NULL.
- Example:
            SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.id;


3. RIGHT JOIN (OUTER JOIN):
- Returns all rows from the right table and matching rows from the left table. Non-matching rows have NULL.
- Example:
            SELECT employees.name, departments.name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.id;


4. FULL OUTER JOIN:
- Returns all rows from both tables, matching where possible. Not natively supported in MySQL, but can be simulated using UNION.
- Example:
            SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.id

UNION

SELECT employees.name, departments.name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.id;


5. CROSS JOIN:
- Returns the Cartesian product of both tables.
- Example:
            SELECT employees.name, departments.name
FROM employees
CROSS JOIN departments;


Interview Questions

1. What is the difference between INNER JOIN and OUTER JOIN?
- INNER JOIN only includes rows with matches in both tables, while OUTER JOIN includes unmatched rows.
2. How can you simulate a FULL OUTER JOIN in MySQL?
- Use UNION of LEFT JOIN and RIGHT JOIN.
3. What is a Cartesian product, and when does it occur?
- A Cartesian product occurs in a CROSS JOIN or when no ON condition is specified, resulting in all possible row combinations.
  • ❤ 1
More from @curiousprogrammer
  1. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  2. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  3. Oct 9, 2026Let's now understand the above Data Analyst Roadmap in detail: 🧠↗️ 1️⃣ Learn Excel ⭐️ The…
  4. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  5. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  6. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →