MySQL Interview Questions & Answers - Sachin Kumar
Q: What is MySQL?
A: MySQL is an open-source relational database management system (RDBMS) that stores data in tables. It
uses SQL (Structured Query Language) to manage, insert, update, delete, and retrieve data.
Q: What are tables and rows in MySQL?
A: A table is a collection of related data organized in columns and rows. Each row represents a single record,
and each column represents a field of the record.
Q: What are the different data types in MySQL?
A: Common data types include: Numeric: INT, FLOAT, DOUBLE; String: CHAR, VARCHAR, TEXT;
Date/Time: DATE, DATETIME, TIMESTAMP; Boolean: TINYINT(1).
Q: What is the difference between CHAR and VARCHAR?
A: CHAR is fixed-length (e.g., CHAR(10) always uses 10 bytes), while VARCHAR is variable-length and
stores only the actual characters entered (up to a limit).
Q: What is a Primary Key?
A: A primary key uniquely identifies each row in a table. It must contain unique and non-null values.
Q: Can a table have multiple primary keys?
A: No. A table can have only one primary key, but it can be a composite key made of multiple columns.
Q: What is the difference between WHERE and HAVING?
A: WHERE filters rows before grouping; HAVING filters after GROUP BY.
Q: What is the difference between DELETE, TRUNCATE, and DROP?
A: DELETE removes rows with conditions; TRUNCATE removes all rows quickly; DROP deletes the entire
table.
Q: How do you retrieve only unique values from a column?
A: Use SELECT DISTINCT column_name FROM table_name;
Q: What is a JOIN? Explain types.
A: JOIN combines rows from two tables. Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL
OUTER JOIN (simulated in MySQL with UNIONs).
Q: What is the use of GROUP BY?
A: It groups rows with the same values in columns, used with aggregate functions like COUNT or SUM.
Q: How do you use ORDER BY to sort results?
A: Use: SELECT * FROM table ORDER BY column_name ASC|DESC;
Q: What is the difference between IN and EXISTS?
A: IN checks values in a list; EXISTS checks for the presence of rows in a subquery - EXISTS is faster for
large datasets.
Q: What is an index in MySQL? Why is it used?
A: Indexes speed up data retrieval by creating quick lookup references. They are like bookmarks in the
database.
Q: How can you prevent SQL injection?
A: By using parameterized queries or prepared statements, and validating user input.
Q: What is a Foreign Key?
A: A foreign key links two tables, enforcing referential integrity.
Q: What are constraints in MySQL?
A: Constraints are rules applied to columns - e.g., NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY.
Q: Write a query to find the second highest salary.
A: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
Q: What tools have you used with MySQL?
A: Ive used MySQL CLI, Workbench, and PHPMyAdmin for database interaction and management.
Q: How do you connect MySQL with a frontend or embedded system?
A: Through back-end APIs like Node.js, Flask, or ESP32 libraries to send/receive data over HTTP or sockets.
Q: What are stored procedures and triggers?
A: Stored procedures are precompiled SQL blocks. Triggers are automatic actions executed in response to
specific table events (like INSERT or UPDATE).