some commonly asked MySQL interview questions and answers, categorized by difficulty level:
1. Basic MySQL Interview Questions
Q1: What is MySQL?
A: MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) for managing data.
Q2: How do you check the MySQL version?
A: Use one of the following commands:
or in Linux CLI:
Q3: How do you create a database in MySQL?
A: Use the CREATE DATABASE
command:
Q4: How do you list all databases?
A: Use:
Q5: How do you create a table in MySQL?
A: Example:
2. Intermediate MySQL Interview Questions
Q6: What are MySQL storage engines?
A: MySQL supports different storage engines:
- InnoDB – Supports transactions, foreign keys, ACID compliance.
- MyISAM – Faster reads, but no transactions or foreign keys.
- Memory – Stores data in RAM for fast access.
- CSV – Stores data in plain-text CSV files.
Check storage engine:
Q7: What is the difference between CHAR
and VARCHAR
?
Feature | CHAR(n) |
VARCHAR(n) |
---|---|---|
Storage | Fixed-length | Variable-length |
Speed | Faster | Slightly slower |
Usage | Small fixed-size data | Variable-length text |
Example:
Q8: How do you find duplicate records in a table?
A: Using GROUP BY
and HAVING
:
Q9: What is an Index in MySQL?
A: An index improves search speed but slows down writes. Types:
- Primary Index (Primary Key)
- Unique Index (
UNIQUE
) - Full-Text Index (for text search)
- Composite Index (multi-column index)
Example:
Check indexes:
Q10: What is the difference between INNER JOIN
, LEFT JOIN
, and RIGHT JOIN
?
Join Type | Description |
---|---|
INNER JOIN | Returns matching rows from both tables. |
LEFT JOIN | Returns all rows from the left table and matching rows from the right. |
RIGHT JOIN | Returns all rows from the right table and matching rows from the left. |
Example:
3. Advanced MySQL Interview Questions
Q11: What are ACID properties in MySQL?
A: ACID ensures database reliability:
- Atomicity – Transactions are all-or-nothing.
- Consistency – The database remains valid before and after transactions.
- Isolation – Transactions do not interfere with each other.
- Durability – Changes persist even after system failure.
InnoDB
supports ACID; MyISAM
does not.
Q12: What is the difference between DELETE
, TRUNCATE
, and DROP
?
Command | Effect |
---|---|
DELETE |
Deletes specific rows (can use WHERE ). |
TRUNCATE |
Removes all rows but keeps the table structure. |
DROP |
Deletes the entire table (schema + data). |
Example:
Q13: How do you optimize a slow MySQL query?
A: Optimization techniques:
- Use EXPLAIN to analyze query execution:
- Add Indexes for faster lookups.
- Use LIMIT to fetch only required rows.
- Avoid
SELECT *
, specify needed columns. - Optimize JOINs and avoid unnecessary subqueries.
- Use caching (Redis, Memcached).
Q14: How do you perform database replication in MySQL?
A: Replication allows automatic copying of data:
- Master-Slave Replication (one primary, multiple read replicas)
- Master-Master Replication (both can write)
- Group Replication (multi-primary setup)
Basic steps:
- Enable binary logging on the master (
my.cnf
): - Configure a slave with
CHANGE MASTER TO
. - Start replication:
Q15: What is the difference between NOW()
, CURDATE()
, and CURRENT_TIMESTAMP
?
Function | Description |
---|---|
NOW() |
Returns current date and time (YYYY-MM-DD HH:MM:SS ). |
CURDATE() |
Returns only the current date (YYYY-MM-DD ). |
CURRENT_TIMESTAMP |
Same as NOW() , but for TIMESTAMP fields. |
Example:
4. MySQL Query Challenges
Q16: How do you find the second-highest salary?
Q17: How do you get the nth highest salary?
or using DENSE_RANK()
:
Q18: How do you swap values in two columns without using a third variable?
real-world MySQL scenarios and how to handle them:
1. Handling Millions of Records Efficiently
Scenario:
A large e-commerce website has millions of orders. Searching for a specific order is very slow.
Solution:
- Use Indexing: Add indexes on frequently searched columns.
- Partition the Table: Use range partitioning by year.
- Use
EXPLAIN
to Analyze Query Performance:
2. Preventing Duplicate Entries
Scenario:
Users accidentally submit the same form twice, creating duplicate records.
Solution:
- Use
UNIQUE
constraint: - Use
INSERT IGNORE
to prevent duplicate inserts: - Use
ON DUPLICATE KEY UPDATE
:
3. Recovering Accidentally Deleted Data
Scenario:
A developer accidentally deleted a table’s data without a backup.
Solution:
- Check MySQL Binary Logs (if enabled):
- Recover using
mysqlbinlog
: - Enable
binlog_format = ROW
in MySQL to allow rollback via logs.
4. Managing High Traffic on a Website
Scenario:
A social media website experiences traffic spikes, slowing down the database.
Solution:
- Use Read Replicas (Replication)
- Set up Master-Slave Replication to distribute read queries.
- Connect read queries to the slave.
- Enable Query Caching (
Redis
orMemcached
) - Use Connection Pooling (
MySQL Pool
)
5. Calculating Monthly Active Users (MAU)
Scenario:
You need to generate a monthly active users (MAU) report.
Solution:
Use DISTINCT
and DATE_FORMAT()
:
6. Handling Soft Deletions Instead of Hard Deletes
Scenario:
You need to delete users but keep their data for compliance reasons.
Solution:
Use a deleted_at
column instead of DELETE
:
7. Avoiding Deadlocks in High-Concurrency Transactions
Scenario:
A banking app allows multiple users to transfer money, leading to deadlocks.
Solution:
- Always Lock in the Same Order:
- Use
NOWAIT
orSKIP LOCKED
to avoid waiting indefinitely:
8. Paginating Large Datasets Efficiently
Scenario:
A news website paginates articles, but OFFSET
is slow for large datasets.
Solution:
Use Keyset Pagination instead of OFFSET
:
Instead of:
9. Fixing Slow JOIN
Queries
Scenario:
A JOIN
query between orders
and users
takes too long.
Solution:
- Check
EXPLAIN
output: - Create an Index on
user_id
:
10. Finding the Most Popular Products
Scenario:
Find the top 5 most sold products in the last 30 days.
Solution:
Use GROUP BY
and ORDER BY
:
Final Thoughts
These real-world MySQL scenarios cover performance tuning, data recovery, replication, concurrency, and scalability.