MySQL Interview Questions
Core Concepts
Section titled โCore ConceptsโWhat is the difference between MyISAM and InnoDB?
| Feature | MyISAM | InnoDB |
|---|---|---|
| Transactions | No | Yes (ACID) |
| Foreign keys | No | Yes |
| Row-level locking | No (table-level) | Yes |
| Full-text search | Yes | Yes (5.6+) |
| Default since | MySQL 5.5 | MySQL 5.5+ |
InnoDB is the default and recommended engine for most applications.
What is the difference between CHAR and VARCHAR?
CHAR(n)โ fixed-length, always uses n bytes. Faster for fixed-size data (e.g., country codes).VARCHAR(n)โ variable-length, uses only as much space as needed plus 1-2 bytes for length. Better for strings of variable size.
What are the different types of JOINs?
-- INNER JOIN: only matching rows in both tablesSELECT o.id, c.nameFROM orders oINNER JOIN customers c ON o.customer_id = c.id;
-- LEFT JOIN: all rows from left, matching from right (NULL if no match)SELECT c.name, o.idFROM customers cLEFT JOIN orders o ON c.id = o.customer_id;
-- RIGHT JOIN: all rows from right, matching from leftSELECT c.name, o.idFROM customers cRIGHT JOIN orders o ON c.id = o.customer_id;
-- CROSS JOIN: cartesian productSELECT a.name, b.nameFROM employees aCROSS JOIN departments b;Indexes
Section titled โIndexesโWhat is an index and when should you use one?
An index is a data structure that speeds up SELECT queries at the cost of slower INSERT/UPDATE/DELETE and extra storage.
Use indexes on:
- Columns in
WHEREclauses - Columns used in
JOINconditions - Columns in
ORDER BYorGROUP BY - Foreign key columns
CREATE INDEX idx_orders_customer ON orders(customer_id);CREATE UNIQUE INDEX idx_users_email ON users(email);CREATE INDEX idx_orders_composite ON orders(customer_id, created_at);What is the difference between a clustered and non-clustered index?
- Clustered index โ the table data is physically stored in the order of this index. InnoDB uses the primary key as the clustered index automatically.
- Non-clustered index โ a separate structure with pointers to the actual rows. All non-primary indexes in InnoDB are non-clustered.
What is a covering index?
A covering index contains all the columns a query needs, so MySQL can satisfy the query from the index alone without touching the table data:
-- QuerySELECT customer_id, created_at FROM orders WHERE customer_id = 5;
-- Covering index โ both columns are in the indexCREATE INDEX idx_covering ON orders(customer_id, created_at);Transactions and Locking
Section titled โTransactions and LockingโWhat are the ACID properties?
- Atomicity โ all operations in a transaction succeed or all are rolled back
- Consistency โ the database moves from one valid state to another
- Isolation โ concurrent transactions donโt interfere with each other
- Durability โ committed transactions survive crashes
What are transaction isolation levels?
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;| Level | Dirty Read | Non-Repeatable Read | Phantom Read |
|---|---|---|---|
| READ UNCOMMITTED | Yes | Yes | Yes |
| READ COMMITTED | No | Yes | Yes |
| REPEATABLE READ (default) | No | No | Yes |
| SERIALIZABLE | No | No | No |
What is a deadlock?
Two transactions each hold a lock the other needs, causing both to wait forever. MySQL detects deadlocks and rolls back one transaction automatically.
-- Transaction 1 -- Transaction 2BEGIN; BEGIN;UPDATE accounts SET bal=... UPDATE accounts SET bal=... WHERE id = 1; WHERE id = 2;-- waits for lock on id=2 -- waits for lock on id=1-- DEADLOCK detectedPrevention: always acquire locks in the same order.
Performance
Section titled โPerformanceโHow do you analyze a slow query?
EXPLAIN SELECT * FROM orders WHERE customer_id = 5;EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 5;Key columns to check:
type:ALL(full scan, bad) โreforconst(index use, good)rows: estimated rows examined โ lower is betterExtra:Using filesortorUsing temporaryare warning signs
What is query caching and is it recommended?
MySQLโs query cache was deprecated in MySQL 5.7 and removed in MySQL 8.0. It caused significant contention. Use application-level caching (Redis, Memcached) instead.
Stored Procedures and Functions
Section titled โStored Procedures and Functionsโ-- Stored ProcedureDELIMITER //CREATE PROCEDURE GetOrdersByCustomer(IN cust_id INT)BEGIN SELECT * FROM orders WHERE customer_id = cust_id;END //DELIMITER ;
CALL GetOrdersByCustomer(42);
-- FunctionDELIMITER //CREATE FUNCTION CalculateDiscount(price DECIMAL(10,2), pct INT)RETURNS DECIMAL(10,2)DETERMINISTICBEGIN RETURN price * (1 - pct / 100);END //DELIMITER ;
SELECT CalculateDiscount(100.00, 20); -- returns 80.00Quick Reference: Common Commands
Section titled โQuick Reference: Common Commandsโ-- Show databases and tablesSHOW DATABASES;SHOW TABLES;DESCRIBE orders;
-- Check indexesSHOW INDEX FROM orders;
-- Check slow queriesSHOW VARIABLES LIKE 'slow_query_log%';SET GLOBAL slow_query_log = 'ON';SET GLOBAL long_query_time = 1;
-- Table sizeSELECT table_name, ROUND((data_length + index_length) / 1024 / 1024, 2) AS size_mbFROM information_schema.tablesWHERE table_schema = 'mydb'ORDER BY size_mb DESC;