Common MySQL Interview Questions and Answers
1. What is MySQL?
MySQL is an open-source relational database management system (RDBMS) based on SQL
(Structured Query Language). It is used to manage and organize data in a relational database and
supports various operating systems.
2. How do you create a database in MySQL?
Write a query to create a new database named `test_db`.
Answer:
```sql
CREATE DATABASE test_db;
```
3. How do you create a table in MySQL?
Write a query to create a table named `employees` with columns `id`, `name`, and `salary`.
Answer:
```sql
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2)
);
```
Common MySQL Interview Questions and Answers
4. How do you insert data into a table?
Write a query to insert a new record into the `employees` table.
Answer:
```sql
INSERT INTO employees (name, salary) VALUES ('John Doe', 50000.00);
```
5. How do you retrieve data from a table?
Write a query to select all records from the `employees` table.
Answer:
```sql
SELECT * FROM employees;
```
6. How do you update data in a table?
Write a query to update the salary of the employee with `id` 1 to 55000.
Answer:
```sql
UPDATE employees SET salary = 55000.00 WHERE id = 1;
```
7. How do you delete data from a table?
Common MySQL Interview Questions and Answers
Write a query to delete the employee with `id` 1 from the `employees` table.
Answer:
```sql
DELETE FROM employees WHERE id = 1;
```
8. What is a JOIN in MySQL?
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
9. How do you perform an INNER JOIN?
Write a query to perform an INNER JOIN between the `employees` table and a `departments` table
on the `department_id` column.
Answer:
```sql
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
```
10. What is a PRIMARY KEY?
A PRIMARY KEY is a unique identifier for a table. It ensures that each record in a table is unique
and not null.
Common MySQL Interview Questions and Answers
11. How do you add a PRIMARY KEY to an existing table?
Write a query to add a PRIMARY KEY to the `id` column of the `employees` table.
Answer:
```sql
ALTER TABLE employees ADD PRIMARY KEY (id);
```
12. What is a FOREIGN KEY?
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in
another table. It helps maintain referential integrity between the two tables.
13. How do you add a FOREIGN KEY to a table?
Write a query to add a FOREIGN KEY to the `department_id` column in the `employees` table that
references the `id` column in the `departments` table.
Answer:
```sql
ALTER TABLE employees ADD CONSTRAINT fk_department
FOREIGN KEY (department_id) REFERENCES departments(id);
```
14. How do you find the maximum value in a column?
Write a query to find the maximum salary in the `employees` table.
Common MySQL Interview Questions and Answers
Answer:
```sql
SELECT MAX(salary) AS max_salary FROM employees;
```
15. How do you count the number of rows in a table?
Write a query to count the number of employees in the `employees` table.
Answer:
```sql
SELECT COUNT(*) AS employee_count FROM employees;
```