MySQL Interview Questions and Answers
MySQL Interview Questions and Answers
Answers
1. What is MySQL?
- MySQL is an open-source relational database management system (RDBMS) that uses
Structured Query Language (SQL) for managing and querying data.
3. What is the difference between MyISAM and InnoDB storage engines in MySQL?
- MyISAM is a non-transactional storage engine with fast performance for read-heavy
workloads, while InnoDB is a transactional storage engine with support for ACID transactions,
foreign keys, and row-level locking.
14. What is the difference between `CHAR` and `VARCHAR` data types in MySQL?
- `CHAR` is a fixed-length string type with a maximum size, while `VARCHAR` is a
variable-length string type with a maximum size. `CHAR` is padded with spaces to the specified
length, while `VARCHAR` stores only the actual length of the data.
16. What is the difference between `UNIQUE` and `PRIMARY KEY` constraints in MySQL?
- Both `UNIQUE` and `PRIMARY KEY` constraints enforce uniqueness, but a table can
have multiple `UNIQUE` constraints while it can have only one `PRIMARY KEY` constraint.
Additionally, `PRIMARY KEY` columns cannot contain NULL values, while `UNIQUE` columns
can.
Sure, here are 20 scenario-based coding questions along with their answers in MySQL:
1. Scenario:
You have a table named `employees` with columns `employee_id`, `first_name`, `last_name`,
and `salary`. Write a SQL query to find the average salary of all employees.
Answer:
sql
SELECT AVG(salary) AS average_salary FROM employees;
2. Scenario:
You have two tables named `orders` and `order_details`. `orders` contains `order_id` and
`order_date`, while `order_details` contains `order_id`, `product_id`, and `quantity`. Write a SQL
query to find the total quantity of each product ordered.
Answer:
sql
SELECT product_id, SUM(quantity) AS total_quantity
FROM order_details
GROUP BY product_id;
3. Scenario:
You have a table named `students` with columns `student_id`, `name`, and `age`. Write a
SQL query to find the names of all students who are older than 20 years.
Answer:
sql
SELECT name
FROM students
WHERE age > 20;
4. Scenario:
You have a table named `products` with columns `product_id`, `name`, and `price`. Write a
SQL query to find the product with the highest price.
Answer:
sql
SELECT *
FROM products
ORDER BY price DESC
LIMIT 1;
5. Scenario:
You have a table named `customers` with columns `customer_id`, `name`, and `city`. Write a
SQL query to find the number of customers in each city.
Answer:
sql
SELECT city, COUNT(*) AS num_customers
FROM customers
GROUP BY city;
6. Scenario:
You have a table named `employees` with columns `employee_id`, `first_name`, `last_name`,
and `department_id`. Write a SQL query to find the total number of employees in each
department.
Answer:
sql
SELECT department_id, COUNT(*) AS num_employees
FROM employees
GROUP BY department_id;
7. Scenario:
You have a table named `orders` with columns `order_id` and `order_date`. Write a SQL
query to find the number of orders placed in each month of the year.
Answer:
sql
SELECT MONTH(order_date) AS month, COUNT(*) AS num_orders
FROM orders
GROUP BY MONTH(order_date);
8. Scenario:
You have a table named `students` with columns `student_id`, `name`, and `grade`. Write a
SQL query to find the top 3 students with the highest grades.
Answer:
sql
SELECT *
FROM students
ORDER BY grade DESC
LIMIT 3;
9. Scenario:
You have two tables named `employees` and `departments`. `employees` contains
`employee_id`, `first_name`, `last_name`, and `department_id`, while `departments` contains
`department_id` and `department_name`. Write a SQL query to find the department with the
most employees.
Answer:
sql
SELECT d.department_name, COUNT(e.employee_id) AS num_employees
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
GROUP BY d.department_name
ORDER BY num_employees DESC
LIMIT 1;
10. Scenario:
You have a table named `sales` with columns `sale_id`, `sale_date`, and `amount`. Write
a SQL query to find the total sales amount for each month of the year.
Answer:
sql
SELECT MONTH(sale_date) AS month, SUM(amount) AS total_sales_amount
FROM sales
GROUP BY MONTH(sale_date);
11. Scenario:
You have a table named `orders` with columns `order_id` and `order_date`. Write a SQL
query to find the number of orders placed on each day of the week (Monday, Tuesday, etc.).
Answer:
sql
SELECT DAYNAME(order_date) AS day_of_week, COUNT(*) AS num_orders
FROM orders
GROUP BY DAYNAME(order_date);
12. Scenario:
You have a table named `students` with columns `student_id`, `name`, `grade`, and
`subject`. Write a SQL query to find the average grade of students in each subject.
Answer:
sql
SELECT subject, AVG(grade) AS average_grade
FROM students
GROUP BY subject;
13. Scenario:
You have a table named `products` with columns `product_id`, `name`, and
`category_id`. Write a SQL query to find the number of products in each category.
Answer:
sql
SELECT category_id, COUNT(*) AS num_products
FROM products
GROUP BY category_id;
14. Scenario:
You have a table named `employees` with columns `employee_id`, `first_name`,
`last_name`, and `manager_id` (where `manager_id` refers to the `employee_id` of the
manager). Write a SQL query to find the name of each employee and their manager's name.
Answer:
sql
SELECT e.first_name AS employee_name, m.first_name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;
15. Scenario:
You have a table named `orders` with columns `order_id`, `order_date`, and
`customer_id`. Write a SQL query to find the total number of orders placed by each customer.
Answer:
sql
SELECT customer_id, COUNT(*) AS num_orders
FROM orders
GROUP BY customer_id;
16. Scenario:
You have a table named `sales` with columns `sale_id`, `sale_date`, and `amount`. Write
a SQL query to find the total sales amount for each year.
Answer:
sql
SELECT YEAR(sale_date) AS year, SUM(amount) AS total_sales_amount
FROM sales
GROUP BY YEAR(sale_date);
17. Scenario:
You have a table named `students` with columns `student_id`, `name`, `grade`, and
`gender`. Write a SQL query to find the average grade of male and female students separately.
Answer:
sql
SELECT gender, AVG(grade) AS average_grade
FROM students
GROUP BY gender;
18
. Scenario:
You have a table named `orders` with columns `order_id`, `order_date`, and
`customer_id`. Write a SQL query to find the number of orders placed by each customer in
2021.
Answer:
sql
SELECT customer_id, COUNT(*) AS num_orders
FROM orders
WHERE YEAR(order_date) = 2021
GROUP BY customer_id;
19. Scenario:
You have a table named `employees` with columns `employee_id`, `first_name`,
`last_name`, and `hire_date`. Write a SQL query to find the number of employees hired in each
year.
Answer:
sql
SELECT YEAR(hire_date) AS year, COUNT(*) AS num_employees_hired
FROM employees
GROUP BY YEAR(hire_date);
20. Scenario:
You have two tables named `students` and `grades`. `students` contains `student_id`,
`name`, and `age`, while `grades` contains `student_id` and `grade`. Write a SQL query to find
the average grade of students older than 20 years.
Answer:
sql
SELECT AVG(g.grade) AS average_grade
FROM students s
JOIN grades g ON s.student_id = g.student_id
WHERE s.age > 20;