MySQL Interview Questions and Answers: Advanced Level

MySQL 55 min min read Updated: Nov 27, 2026 Advanced
MySQL Interview Questions and Answers: Advanced Level
Advanced Topic 2 of 3

How to Use This Page

These questions test whether you understand how MySQL works inside, not only the syntax. Explain the reason (the why) and mention a trade-off. Facts refer to MySQL 8.0.

Q1. What is a clustered index in InnoDB?

InnoDB stores the table rows inside the primary key B-tree, so the table is the primary key index. Other indexes are secondary: they store their columns plus the primary key value. So a long primary key makes every secondary index larger, and a lookup through a secondary index may need a second lookup in the clustered index.

Q2. What is a covering index?

An index that holds all columns a query needs, so MySQL answers from the index without reading table rows. EXPLAIN shows Using index.

Q3. Explain the leftmost prefix rule.

A composite index on (a, b, c) can serve conditions on a, on a, b, and on a, b, c, but not on b or c alone (apart from special cases such as skip scan). Put equality columns first, then the range column.

Q4. Why can an index be ignored?

A function on the column (WHERE MONTH(d) = 3), a type mismatch (comparing a string column with a number), a leading wildcard in LIKE '%x', low selectivity (the optimizer prefers a scan), or stale statistics.

Q5. How do you read an EXPLAIN result?

Check type (ALL is a full scan, ref and range use an index), key (index used), rows (estimated rows read) and Extra (Using index, Using filesort, Using temporary). EXPLAIN ANALYZE (8.0.18+) also runs the query and shows actual rows and time.

Q6. What isolation levels does InnoDB support and what is the default?

READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ (default) and SERIALIZABLE. Under REPEATABLE READ a transaction reads from one consistent snapshot, so repeated reads give the same result.

Q7. What is MVCC?

Multi-version concurrency control: InnoDB keeps old row versions in the undo log so readers see a consistent snapshot and do not block writers. Locking reads (FOR UPDATE) and writes use the latest version and take locks.

Q8. What are gap locks and next-key locks?

Under REPEATABLE READ, a locking range read or write locks the index records and the gaps between them (a next-key lock is a record lock plus the gap before it) to prevent phantom rows. This can block inserts into a range. Under READ COMMITTED gap locking is mostly disabled.

Q9. How do deadlocks happen and how do you handle them?

Two transactions each hold a lock the other needs. InnoDB detects it, rolls back one (error 1213) and the other continues. Reduce them with a consistent lock order, short transactions and good indexes, and always retry the whole transaction in the application.

Q10. What is the difference between SELECT ... FOR UPDATE and LOCK IN SHARE MODE (FOR SHARE)?

FOR UPDATE takes an exclusive lock on the rows read, so others cannot modify or lock them. FOR SHARE takes a shared lock: others can read and share-lock but not modify. Both last until the transaction ends. NOWAIT and SKIP LOCKED change the waiting behaviour.

Q11. What does innodb_flush_log_at_trx_commit control?

Durability versus speed. 1 flushes the redo log to disk at every commit (safe). 2 writes to the OS cache and flushes once a second. 0 flushes once a second. Values 0 and 2 can lose about a second of committed transactions on a crash.

Q12. What is the InnoDB buffer pool?

The memory cache for table and index pages. Its size (innodb_buffer_pool_size) is the most important tuning setting: a well-sized pool keeps hot data in memory and avoids disk reads.

Q13. How does MySQL replication work?

The primary writes changes to its binary log. Each replica copies it to a relay log (IO thread) and applies it (SQL thread). It is asynchronous by default, so replicas may lag. GTIDs identify transactions, and Group Replication or InnoDB Cluster give automatic failover.

Q14. How do you paginate a large table efficiently?

Avoid deep OFFSET because MySQL reads and discards the skipped rows. Use keyset pagination: remember the last id and query WHERE id > :last ORDER BY id LIMIT n.

Q15. What is ONLY_FULL_GROUP_BY and why keep it?

A sql_mode flag that rejects selecting a column that is neither grouped nor aggregated. Without it MySQL returns an arbitrary row's value, which hides bugs. Use ANY_VALUE() when any value is acceptable.

Q16. How do window functions differ from GROUP BY?

GROUP BY collapses rows into one per group. A window function (OVER) keeps every row and adds a calculated column such as a rank, running total or previous value. They need MySQL 8.0.

Q17. How can you store and index JSON?

Use the JSON type for flexible data. Index a field through a generated column or a functional index, and index arrays with a multi-valued index (8.0.17+) queried with MEMBER OF, JSON_CONTAINS or JSON_OVERLAPS.

Q18. How do you add a column to a huge table without downtime?

Use online DDL (ALGORITHM=INSTANT for adding a column in 8.0, or INPLACE, LOCK=NONE), or an online schema change tool such as gh-ost or pt-online-schema-change. Test first on a copy of production data.

Q19. What is the difference between a view and a table, and what is a materialized view in MySQL?

A view is a stored query with no data of its own. MySQL has no built-in materialized view: you simulate it with a summary table refreshed by a scheduled event or a job.

Q20. How would you find and fix a slow query?

Find it with the slow query log or sys.statement_analysis, read EXPLAIN (and EXPLAIN ANALYZE), add or reorder an index, rewrite the query (avoid functions on columns, select fewer columns, keyset pagination), refresh statistics with ANALYZE TABLE and measure again with realistic data.

Quick Proof of One Answer

The leftmost prefix rule on a real table of 20,000 rows is shown in the index lesson. Here is a smaller check of the ONLY_FULL_GROUP_BY answer:

sql SELECT category, name FROM products GROUP BY category;
Error

ERROR 1055: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shop.products.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Conclusion

Strong answers explain how InnoDB works: clustered indexes, MVCC, locks, the buffer pool, replication and the way the optimizer picks indexes. Always add one trade-off.

Quick Summary: clustered index, covering index, MVCC, gap locks, deadlocks, replication, optimization.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators