Questions
26 of 30
1What is a clause in MySQL, and how is it different from a statement?
2What is the purpose of the SELECT clause in MySQL?
3How does the WHERE clause filter rows in a query?
4What is the function of the ORDER BY clause?
5How does the LIMIT clause work, and what is its typical use case?
6What does the DISTINCT clause do in a SELECT query?
7What is the difference between WHERE and HAVING clauses?
8How is the GROUP BY clause used in MySQL?
9What does the AS clause do in SELECT queries?
10What is the role of the FROM clause in query execution?
11Explain how MySQL processes clauses in the logical order of query execution.
12What is the difference between IN, BETWEEN, and LIKE in WHERE clauses?
13Can you use aggregate functions in the WHERE clause? Why or why not?
14How does MySQL handle NULL values in the WHERE and HAVING clauses?
15How can you use the CASE clause for conditional selection?
16What is the use of the JOIN ... ON clause, and how does it differ from the WHERE clause in joins?
17How can the GROUP_CONCAT() function be used with GROUP BY clauses?
18How can you use the HAVING clause without using GROUP BY?
19Explain how the LIMIT clause interacts with OFFSET.
20What is the difference between the ALL, ANY, and SOME clauses when used with subqueries?
21Describe the order of execution of clauses in a MySQL SELECT statement (FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT).
22How does MySQL handle filtering when both WHERE and HAVING clauses are present in a query?
23What are derived tables, and how does the FROM (subquery) clause work internally?
24How can window functions with the OVER() clause be combined with other clauses like WHERE or GROUP BY?
25What happens when ORDER BY and GROUP BY use different columns — how does MySQL handle sorting and aggregation?
26Explain how the optimizer rewrites queries internally when multiple clauses (WHERE, GROUP BY, HAVING, ORDER BY) overlap in logic.
27Can you use the WHERE clause inside subqueries within the SELECT clause? How does scope affect it?
28What are clauses in MySQL, and why are they important?
29How do clauses like UNION, INTERSECT, and EXCEPT interact with ORDER BY and LIMIT in MySQL?
30What are the performance implications of clause order — for example, WHERE filtering before JOIN or HAVING after aggregation?
26 / 30

Explain how the optimizer rewrites queries internally when multiple clauses (WHERE, GROUP BY, HAVING, ORDER BY) overlap in logic.

MySQL Query Optimization and Clause Rewriting

MySQL's query optimizer evaluates queries to determine the most efficient execution plan. When multiple clauses like WHERE, GROUP BY, HAVING, and ORDER BY overlap in logic, the optimizer may rewrite the query internally to improve performance while preserving correctness.

Key Optimizations Performed by MySQL
  1. 1

    Predicate Pushdown – WHERE conditions are applied as early as possible to reduce the number of rows processed by GROUP BY or JOIN operations.

  2. 2

    Aggregation Reordering – GROUP BY and aggregate functions may be reordered or optimized to reduce computation, such as combining similar expressions or removing unnecessary grouping.

  3. 3

    HAVING Simplification – If HAVING conditions can be expressed in terms of WHERE or indexed columns, the optimizer may push them down to filter rows earlier.

  4. 4

    ORDER BY Optimization – If indexes exist that match the ORDER BY columns, MySQL may avoid a separate sort by reading rows in index order.

  5. 5

    Subquery Flattening – Derived tables or subqueries may be merged into the outer query to avoid extra temporary tables when possible.

  6. 6

    Expression Simplification – Constant expressions or redundant calculations are precomputed, reducing runtime evaluation overhead.

Overall, the optimizer ensures that filtering, grouping, aggregation, and sorting are executed efficiently. Even if clauses overlap logically (e.g., WHERE filters rows that could also be filtered by HAVING), the optimizer rearranges or merges operations to minimize the workload without changing the final result.

Example Showing Clause Overlap Optimization
Difficulty: 7/10
Topics: predicate pushdown, query rewrite, execution plan

Scenario Questions

0-2 years experience
  1. 1

    You have a query SELECT col1 FROM t WHERE col1 > 10 GROUP BY col1 HAVING col1 > 10 ORDER BY col1. How will MySQL's optimizer treat the overlapping conditions?

  2. 2

    If you add an index on col1, what part of this query will the optimizer try to satisfy using that index?

2-5 years experience
  1. 1

    During a code review you notice a query with both WHERE and HAVING filtering the same column. The query is running slowly. Walk me through how you would debug the optimizer's rewrite steps and what changes you might make.

  2. 2

    A feature adds an ORDER BY clause to an existing grouped query, and the execution time doubles. Explain why the optimizer might not be able to reuse the previous plan and how you could rewrite the query to help it.

5-8 years experience
  1. 1

    Design a monitoring alert that detects when MySQL fails to push down predicates from HAVING to WHERE, causing full table scans. What metrics and EXPLAIN output would you look at?

  2. 2

    You are scaling a reporting service that runs complex GROUP BY/HAVING queries on a 10 TB table. Discuss the trade‑offs of relying on the optimizer's rewrite versus manually materializing intermediate results.

8+ years experience
  1. 1

    Our legacy system has many queries where WHERE, GROUP BY, HAVING, and ORDER BY overlap. We plan a migration to a newer MySQL version with a different optimizer. How would you assess the risk and plan the migration to ensure query semantics stay identical?

  2. 2

    Across multiple services, teams are writing similar aggregation queries with redundant clauses. Propose an architectural guideline or tooling to enforce optimal query rewrites at compile‑time.

Follow-up Questions

  • How does predicate pushdown affect index usage?
  • What happens if the optimizer cannot eliminate a redundant ORDER BY?
  • Can you give an example where HAVING is evaluated before WHERE and why that matters?