Questions
12 of 48
1What is a JOIN in MySQL, and why is it used?
2What is the difference between INNER JOIN and OUTER JOIN?
3How do you write a basic INNER JOIN query between two tables?
4What is the purpose of the ON clause in JOIN statements?
5What is the difference between using JOIN and WHERE for joining tables?
6What are LEFT JOIN and RIGHT JOIN, and how do they differ from INNER JOIN?
7What is a CROSS JOIN, and what result does it produce?
8Can you perform a JOIN without using an explicit JOIN keyword (i.e., using WHERE)? Explain.
9What happens when columns in joined tables have the same name? How do you resolve ambiguity?
10What are NATURAL JOINS and why are they generally discouraged in production code?
11Explain FULL OUTER JOIN and why MySQL does not support it directly. How can it be simulated?
12What is a SELF JOIN and when would you use it? Provide an example.
13How can you simulate an INTERSECT or EXCEPT operation using JOINs in MySQL?
14What is an ANTI JOIN and how do you implement it in MySQL?
15How do JOINs differ when using subqueries vs. derived tables?
16Performance & Optimization
17How does MySQL execute JOIN operations internally (nested loop, hash join, etc.)?
18What is the difference between a nested loop join and a hash join? Does MySQL support hash joins?
19How do indexes affect JOIN performance in MySQL?
20How can the EXPLAIN command be used to analyze JOIN performance?
21How do you optimize multi-table joins for better performance in large databases?
22What are multi-table joins, and how many tables can you join in a single query?
23What is the impact of NULL values in join conditions?
24What’s the difference between using USING(column_name) and ON in JOIN statements?
25How do you join a table with itself multiple times using aliases?
26Can you join more than one column in a JOIN condition? Give an example.
27How do you use JOINs with aggregations and conditions in MySQL?
28How to perform aggregations efficiently on joined tables in MySQL?
29How can you join tables and still include rows with no matches (using LEFT JOIN and IS NULL)?
30How can HAVING and WHERE behave differently in queries involving JOINs?
31How do GROUP BY and JOIN interact — what are the common pitfalls?
32Can you join on a calculated or derived value (for example, using a function in the ON clause)?
33How would you join three or more tables to combine customer, order, and payment data?
34What is the difference between joining normalized tables and joining denormalized ones?
35Can JOINs cause duplicate rows in results? How do you eliminate them?
36How would you write a query to find customers who have orders but no payments using JOINs?
37How do INNER JOIN and EXISTS differ logically and in performance?
38Complex & Edge Cases
39How does MySQL handle joins across databases (cross-database joins)?
40Can you JOIN temporary tables with permanent tables? Are there limitations?
41What happens when you join large datasets without appropriate indexes?
42How can you optimize memory and CPU usage when performing multiple JOINs on large tables?
43Explain a situation where replacing JOIN with a subquery improved performance.
44Does MySQL 8.0 support hash joins or batched key access joins? When are they used?
45What improvements to join optimization were introduced in MySQL 8.0 compared to earlier versions?
46How does MySQL handle join buffering and block nested loop joins?
47Can window functions be used along with JOINs? Give an example.
48What’s the difference between lateral derived tables and correlated subqueries in JOIN contexts?
12 / 48

What is a SELF JOIN and when would you use it? Provide an example.

Understanding SELF JOIN in MySQL

A SELF JOIN is a join in which a table is joined with itself. This is useful when rows in the same table have a relationship with one another — such as hierarchical data, parent-child relationships, or comparing rows within the same table.

1. When to Use a SELF JOIN
  1. 1

    • When a table contains a hierarchical structure (e.g., employees reporting to managers).

  2. 2

    • To compare rows within the same table (e.g., finding people who live in the same city).

  3. 3

    • To model parent-child relationships within a single table.

  4. 4

    • Whenever you need to relate rows from the same dataset.

2. Example: Employee → Manager Relationship
  1. 1

    Suppose you have an employees table:

  2. 2

    • id

  3. 3

    • name

  4. 4

    • manager_id (references employees.id)

SELF JOIN Example

• Here, the table employees is used twice: once as e (employee) and once as m (manager). • The result lists each employee with their corresponding manager.

3. Key Points About SELF JOIN
  1. 1

    • Uses table aliases to differentiate between the two instances of the same table.

  2. 2

    • Can be used with INNER, LEFT, or RIGHT JOIN depending on the requirement.

  3. 3

    • Essential for hierarchical or relational data stored in one table.

Difficulty: 5/10
Topics: self-join, hierarchical data, query optimization

Scenario Questions

0-2 years experience
  1. 1

    We have a table employees(id, name, manager_id). How would you write a query to list each employee together with their manager's name using a self join?

  2. 2

    If you run a self join on the same table without using table aliases, what error or result would you see?

2-5 years experience
  1. 1

    Our reporting feature needs to show the total number of direct and indirect reports for each manager. Walk me through how you'd extend a self join approach to get this data, and what limitations you might hit.

  2. 2

    During a code review, a teammate's query that uses a self join on a large orders table is running slowly. What steps would you take to diagnose and improve its performance?

5-8 years experience
  1. 1

    We're building a service that frequently queries an employee hierarchy stored as an adjacency list. Discuss the trade‑offs of using self joins versus alternative models (e.g., closure table, nested sets) for read‑heavy workloads at scale.

  2. 2

    Imagine the employees table has grown to 100 million rows and we need to support real‑time ancestor lookups. How would you redesign the schema or indexing strategy to keep self‑join queries performant?

8+ years experience
  1. 1

    Our company is migrating a legacy HR system that uses recursive self joins in MySQL to a micro‑service architecture with polyglot persistence. How would you approach the migration, ensuring data consistency and minimal disruption?

  2. 2

    Across multiple teams, there is a debate about keeping hierarchical data in MySQL using self joins versus moving to a graph database. As a staff engineer, outline the criteria you would use to make a long‑term architectural decision.

Follow-up Questions

  • Why do we need table aliases when writing a self join?
  • What indexes would you add to improve a self join on a hierarchical column?
  • How would you verify that your self‑join query returns the correct hierarchical pairs?