MySQL is one of the most widely used relational database management systems in web and enterprise applications. For developers, backend engineers, and database professionals, MySQL interviews often focus not only on SQL syntax but also on query optimization, indexing, transactions, database design, concurrency, and performance.
This article covers 10 important MySQL interview questions with answers and explanations to help you prepare for technical interviews.
1. What is MySQL and how is it different from SQL?
Answer
MySQL is a relational database management system (RDBMS) used to store, manage, and retrieve structured data.
SQL (Structured Query Language) is the language used to communicate with relational databases.
For example:
SELECT * FROM users WHERE status = 'active';
Here, SQL is the language, while MySQL is the database system executing the query.
Explanation
Other database systems such as PostgreSQL, Oracle Database, and SQL Server also use SQL, although each may have its own features and syntax extensions.
In an interview, remember:
SQL = query language
MySQL = database management system
RDBMS = database system based on tables and relationships
2. What is the difference between PRIMARY KEY and UNIQUE KEY?
Answer
A PRIMARY KEY uniquely identifies each row in a table, while a UNIQUE KEY ensures that values in a column or group of columns are unique.
Example:
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE,
name VARCHAR(100)
);
Here:
idis the primary key.emailmust contain unique values.
Explanation
Important differences include:
| Feature | PRIMARY KEY | UNIQUE KEY |
|---|---|---|
| Purpose | Identifies a row | Enforces uniqueness |
| Number per table | One | Multiple allowed |
| NULL | Not allowed | NULL handling is allowed |
| Typical use | Record identifier | Unique business values |
For example, a user can have one id as the primary key and a unique email address.
3. What is an Index in MySQL and why is it important?
Answer
An index is a data structure that helps MySQL find rows faster without scanning the entire table.
Example:
CREATE INDEX idx_users_email
ON users(email);
A query such as:
SELECT * FROM users
WHERE email = 'user@example.com';
can benefit from the index on email.
Explanation
Without an appropriate index, MySQL may need to examine many rows.
Indexes can significantly improve:
WHEREqueriesJOINoperationsORDER BYGROUP BY
However, indexes are not free. They consume storage and can slow down INSERT, UPDATE, and DELETE operations because indexes also need to be maintained.
A good interview answer should mention both benefits and costs.
4. What is the difference between INNER JOIN, LEFT JOIN, and RIGHT JOIN?
Answer
Joins combine data from multiple tables.
INNER JOIN
Returns only matching records from both tables.
SELECT u.name, o.order_id
FROM users u
INNER JOIN orders o
ON u.id = o.user_id;
LEFT JOIN
Returns all records from the left table and matching records from the right table.
SELECT u.name, o.order_id
FROM users u
LEFT JOIN orders o
ON u.id = o.user_id;
Users without orders will still appear, with NULL values for order columns.
RIGHT JOIN
Returns all records from the right table and matching records from the left table.
SELECT u.name, o.order_id
FROM users u
RIGHT JOIN orders o
ON u.id = o.user_id;
Explanation
In real-world applications, INNER JOIN and LEFT JOIN are used frequently.
For example, if you want all users, including users who have never placed an order, use LEFT JOIN.
5. What is normalization in MySQL?
Answer
Normalization is the process of organizing database tables to reduce data duplication and improve data consistency.
For example, instead of storing customer information repeatedly inside every order record:
orders
------------------------------------------------
order_id | customer_name | customer_email
we can separate the data:
customers
------------------------------
id | name | email
orders
------------------------------
id | customer_id | amount
Explanation
Common normal forms include:
1NF — First Normal Form
2NF — Second Normal Form
3NF — Third Normal Form
BCNF — Boyce-Codd Normal Form
Normalization generally improves data consistency.
However, highly normalized databases may require more joins. In performance-sensitive systems, carefully chosen denormalization can sometimes be useful.
6. What is the difference between DELETE, TRUNCATE, and DROP?
Answer
All three remove data or database objects, but they work differently.
DELETE
Removes selected rows.
DELETE FROM users
WHERE status = 'inactive';
TRUNCATE
Removes all rows from a table.
TRUNCATE TABLE users;
DROP
Removes the table itself.
DROP TABLE users;
Explanation
A simplified comparison:
| Command | Removes Rows | Removes Table | WHERE Supported |
|---|---|---|---|
| DELETE | Yes | No | Yes |
| TRUNCATE | All rows | No | No |
| DROP | Yes | Yes | No |
The exact transactional and locking behavior of TRUNCATE depends on the MySQL version and storage engine, so experienced candidates should avoid claiming that it is universally equivalent to a transaction-safe DELETE.
7. What are ACID properties in MySQL?
Answer
ACID stands for:
Atomicity
Consistency
Isolation
Durability
These properties are fundamental to reliable database transactions.
Example:
START TRANSACTION;
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
UPDATE accounts
SET balance = balance + 100
WHERE id = 2;
COMMIT;
If something goes wrong:
ROLLBACK;
Explanation
Atomicity: A transaction succeeds completely or is rolled back.
Consistency: The database moves from one valid state to another valid state.
Isolation: Concurrent transactions should not incorrectly interfere with each other.
Durability: Once committed, the changes are intended to survive failures.
In MySQL, InnoDB is the commonly used transactional storage engine.
8. What is a transaction and when should you use one?
Answer
A transaction groups multiple database operations into one logical unit.
Example:
START TRANSACTION;
INSERT INTO orders (user_id, total)
VALUES (10, 500);
UPDATE products
SET stock = stock - 1
WHERE id = 25 AND stock > 0;
COMMIT;
If an operation fails:
ROLLBACK;
Explanation
Transactions are especially important when multiple operations must succeed together.
Common examples include:
Bank transfers
Order creation
Inventory updates
Payment processing
Account balance changes
For example, creating an order but failing to update inventory could leave the application in an inconsistent state. A transaction can help maintain the required consistency.
9. How do you optimize a slow MySQL query?
Answer
The first step is to identify why the query is slow instead of blindly adding indexes.
Use:
EXPLAIN
SELECT *
FROM orders
WHERE user_id = 100;
For deeper analysis, MySQL also provides EXPLAIN ANALYZE in supported versions.
Explanation
Important optimization techniques include:
Check the execution plan.
Add appropriate indexes.
Avoid unnecessary
SELECT *.Optimize joins.
Reduce unnecessary rows before sorting/grouping.
Review expensive
ORDER BYandGROUP BYoperations.Avoid functions on indexed columns when they prevent effective index use.
Check for unnecessary subqueries.
Review table and index statistics.
Consider query and application-level caching where appropriate.
For example, instead of:
SELECT *
FROM users
WHERE email = 'user@example.com';
you may select only the required columns:
SELECT id, name, email
FROM users
WHERE email = 'user@example.com';
A strong senior-level answer should emphasize execution-plan analysis before optimization.
10. What is the N+1 query problem and how can you solve it?
Answer
The N+1 query problem occurs when an application executes one query to retrieve a list of records and then executes another query for each individual record.
For example:
SELECT * FROM users;
If there are 1,000 users and the application then runs:
SELECT * FROM orders WHERE user_id = 1;
SELECT * FROM orders WHERE user_id = 2;
SELECT * FROM orders WHERE user_id = 3;
and so on, the application can end up executing 1,001 queries.
Explanation
This can cause significant performance problems.
Instead, the data can often be retrieved using a single query:
SELECT
u.id,
u.name,
o.order_id,
o.amount
FROM users u
LEFT JOIN orders o
ON o.user_id = u.id;
Other approaches include:
Eager loading in ORM frameworks
Batch queries
Proper joins
Data aggregation
Query optimization
The key is to avoid repeatedly querying the database for related data when it can be fetched efficiently in batches.
Quick MySQL Interview Revision
Before a MySQL interview, make sure you understand:
SQL vs MySQL
Primary key and foreign key
Unique keys
Indexes
Composite indexes
Clustered and secondary indexes
INNER JOIN
LEFT JOIN
RIGHT JOIN
Self JOIN
Normalization
Denormalization
DELETE vs TRUNCATE vs DROP
GROUP BY and HAVING
Subqueries
Common Table Expressions (CTEs)
Window functions
Transactions
ACID properties
Isolation levels
Deadlocks
EXPLAIN and execution plans
Query optimization
N+1 queries
Database constraints
Foreign keys
Stored procedures
Views
Triggers
Partitioning
Replication
Backup and recovery
Database security
Final Interview Tip
For experienced candidates, simply knowing SQL syntax is usually not enough. Be prepared to explain why a query is slow, how indexes affect performance, how transactions maintain consistency, how concurrent transactions interact, and how database design affects application scalability.
When answering MySQL interview questions, explain your approach with a practical example whenever possible. This demonstrates both theoretical knowledge and real-world database experience.