Questions
43 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?
43 / 48

Explain a situation where replacing JOIN with a subquery improved performance.

When Replacing a JOIN with a Subquery Improves Performance

Although JOINs are generally efficient, there are specific scenarios where replacing a JOIN with a subquery (especially EXISTS or a scalar subquery) can dramatically improve performance. This usually happens when a JOIN produces large intermediate result sets or multiplies rows unnecessarily.

1. Row Multiplication from JOINs Causing Large Intermediate Datasets
  1. 1

    JOINs can produce duplicates when the joined table contains multiple matches.

  2. 2

    This leads to massive intermediate result sets that MySQL needs to sort, buffer, or group.

  3. 3

    A subquery (EXISTS or IN) avoids row multiplication because it only checks existence.

JOIN Example (Slow Due to Row Multiplication)

If a customer has 1000 orders, that customer's row appears 1000 times. MySQL must process and filter these duplicate rows.

Optimized Version Using EXISTS (No Row Multiplication)

MySQL stops scanning as soon as it finds the first matching order. This reduces I/O, CPU usage, and memory consumption.

2. Large JOINed Table with Good Filter in Subquery
  1. 1

    JOINs often pull millions of rows before filtering.

  2. 2

    A subquery can apply the filter first, using indexes efficiently.

  3. 3

    This allows MySQL to eliminate unnecessary row comparisons.

JOIN (Processes Many Unnecessary Rows)
Subquery Version (Filtered Early → Faster)

If sales is indexed on year, product_id, the subquery filters down to only relevant rows, which is far faster than joining millions of rows first.

3. Avoiding Temporary Tables and Filesorts
  1. 1

    JOINs involving GROUP BY or DISTINCT may trigger on-disk temporary tables.

  2. 2

    EXISTS usually avoids sorting and temporary tables entirely.

  3. 3

    This reduces disk I/O and memory pressure.

4. When Only Existence Matters
  1. 1

    JOINs return full row data, even if not needed.

  2. 2

    EXISTS stops at the first match, returning a simple boolean check.

  3. 3

    This is much faster on large tables.

Replacing a JOIN with a subquery improves performance when JOINs produce large intermediate results, when only existence checks are needed, when filtering can be pushed into the subquery, and when avoiding on-disk temporary tables is critical. EXISTS-based subqueries often provide superior performance for large, selective datasets.

Difficulty: 6/10
Topics: query optimization, subqueries vs joins, MySQL execution plan

Scenario Questions

0-2 years experience
  1. 1

    We have a MySQL query that joins a large orders table with a customers table to filter recent orders. How would you rewrite it using a subquery to potentially improve performance, and what steps would you take to verify it helped?

  2. 2

    If you notice a query with a LEFT JOIN returning many duplicate rows and running slowly, what simple change could you try with a subquery, and what result would you expect?

2-5 years experience
  1. 1

    In a feature that generates a report of users with more than 10 purchases, the current JOIN query is timing out. Walk me through how you would refactor it to use a correlated subquery, what indexes you’d consider, and how you’d measure the impact.

  2. 2

    During a code review you see a query that joins products to inventory and then filters on inventory.stock > 0. The join is causing a full table scan. Explain why replacing the join with a NOT EXISTS subquery might be faster, and how you’d test that hypothesis.

5-8 years experience
  1. 1

    Our analytics pipeline runs a nightly MySQL job that aggregates sales per region using multiple joins across large fact tables. The job is taking hours. Describe how you would evaluate replacing some of those joins with derived‑table subqueries or temporary tables, what risks you’d watch for, and how you’d ensure correctness at scale.

  2. 2

    When optimizing a high‑traffic API, you discovered that a query joining sessions and users is a bottleneck under load. Discuss the trade‑offs of rewriting it as a subquery that pre‑filters session IDs, including considerations around caching, query plan stability, and potential deadlocks.

8+ years experience
  1. 1

    Our legacy monolith stores audit logs in a MySQL table that is frequently joined with the main transactions table for compliance reports. The joins are now causing performance regressions as data grows. Propose an architectural migration strategy that leverages subqueries, materialized views, or data partitioning, and explain how you’d coordinate this change across multiple teams.

  2. 2

    Imagine you’re leading a cross‑team effort to standardize query patterns across services. How would you create guidelines for when to prefer subqueries over joins in MySQL, taking into account maintainability, optimizer behavior, and future schema evolution?

Follow-up Questions

  • What specific metrics would you look at to confirm the subquery improved performance?
  • How would you handle a case where the subquery version ends up slower than the original join?
  • Can you describe any pitfalls with MySQL's optimizer when using subqueries?