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

How do JOINs differ when using subqueries vs. derived tables?

JOINs Using Subqueries vs. Derived Tables in MySQL

JOINs can be performed directly on tables, on derived tables (subqueries in the FROM clause), or involve subqueries inside the SELECT or WHERE clause. Although both subqueries and derived tables use nested queries, their behavior, performance, and optimization differences are important to understand.

1. What Is a Subquery?
  1. 1

    • A query nested inside SELECT, WHERE, or HAVING.

  2. 2

    • Returns a single value (scalar), a list (IN), or sometimes a table.

  3. 3

    • Not directly joinable unless used in FROM (which becomes a derived table).

Example of a Subquery in WHERE

Here, the subquery returns a single value, and no JOIN is used.

2. What Is a Derived Table?
  1. 1

    • A SELECT subquery placed in the FROM clause.

  2. 2

    • Acts like a temporary, inline table.

  3. 3

    • Can be joined with other tables.

  4. 4

    • Must have an alias.

Example of a Derived Table Used with JOIN

Here, the derived table d produces grouped data that can be joined like a regular table.

3. Key Differences Between Subqueries and Derived Tables
  1. 1

    Placement: Subqueries appear in WHERE/SELECT; derived tables appear in FROM.

  2. 2

    Join Capability: Only derived tables can be joined; WHERE subqueries cannot.

  3. 3

    Optimization: MySQL may optimize derived tables (materialize or merge), but scalar subqueries often execute per row unless optimized.

  4. 4

    Reusability: Derived tables can be reused in multiple JOINs; subqueries cannot.

  5. 5

    Readability: Derived tables make complex joins and aggregations clearer.

4. When to Use What?
  1. 1

    Use subqueries when you need a single value or simple existence checks.

  2. 2

    Use derived tables when you need multi-column results that must be joined.

  3. 3

    Use derived tables for better readability in complex transformations.

  4. 4

    Avoid subqueries that execute per-row (especially correlated ones) unless necessary.

In summary: Subqueries are used for filtering or single-value extraction, while derived tables behave like temporary tables that can participate in JOINs. Derived tables are typically more flexible and efficient in JOIN-heavy queries.

Difficulty: 6/10
Topics: subquery vs derived table, MySQL optimizer, performance impact

Scenario Questions

0-2 years experience
  1. 1

    We need to list each customer with the date of their most recent order. How would you write that using a subquery in the SELECT clause versus a derived table in the FROM clause, and what difference would you expect in the result set?

  2. 2

    If you join a subquery that aggregates sales per region to the regions table, and then rewrite the same logic as a derived table, what change might you see in MySQL's execution plan?

2-5 years experience
  1. 1

    Our reporting page started timing out after we changed a join from a derived table to a correlated subquery. Walk me through how you would investigate and fix the performance regression.

  2. 2

    When refactoring a legacy query that uses a subquery in the SELECT list to a derived table in the FROM clause, what trade‑offs do you consider regarding readability, optimizer behavior, and result correctness?

  3. 3

    A query using a derived table works on dev data but fails with ‘Too many tables’ on production. How would you decide whether to keep the derived table or rewrite it as a subquery?

5-8 years experience
  1. 1

    Design a daily metrics aggregation pipeline in MySQL. Would you prefer derived tables or subqueries for the intermediate joins, and how would you justify that choice for scalability and optimizer hints?

  2. 2

    Our analytics microservice sometimes hits the temporary table limit because of complex joins. Explain how using derived tables versus subqueries could affect temporary table usage and overall system throughput.

  3. 3

    If you need to rewrite a suite of queries to run on a read‑replica with limited CPU and memory, how would you restructure joins that currently use subqueries to improve performance while preserving semantics?

8+ years experience
  1. 1

    Your organization is creating a company‑wide guideline for MySQL query patterns. Propose a policy for when to use derived tables versus subqueries, considering maintainability, optimizer predictability, and future migration to a columnar store.

  2. 2

    During a migration to a sharded MySQL architecture, how does the choice between subqueries and derived tables impact query routing, data locality, and cross‑shard performance, and what guidelines would you set for engineering teams?

Follow-up Questions

  • What does the EXPLAIN output look like for each approach?
  • How would adding an index affect the two versions?
  • Can you think of a case where the optimizer would rewrite a subquery into a derived table automatically?