0% found this document useful (0 votes)
14 views

MySQL Interview Questions and Answers

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

MySQL Interview Questions and Answers

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MySQL Interview Questions and

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.

2. What are the key features of MySQL?


- Key features of MySQL include support for multiple storage engines, ACID compliance,
replication, full-text indexing, and a wide range of data types.

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.

4. How do you create a database in MySQL?


sql
CREATE DATABASE dbname;

5. How do you create a table in MySQL?


sql
CREATE TABLE tablename (
column1 datatype,
column2 datatype,
...
);

6. What are the different data types supported by MySQL?


- MySQL supports data types such as INT, VARCHAR, TEXT, DATE, TIME, DATETIME,
FLOAT, DOUBLE, and more.

7. How do you insert data into a table in MySQL?


sql
INSERT INTO tablename (column1, column2, ...) VALUES (value1, value2, ...);

8. How do you retrieve data from a table in MySQL?


sql
SELECT * FROM tablename;

9. How do you update data in a table in MySQL?


sql
UPDATE tablename SET column1 = value1, column2 = value2 WHERE condition;

10. How do you delete data from a table in MySQL?


sql
DELETE FROM tablename WHERE condition;

11. What is a primary key in MySQL?


- A primary key is a column or a set of columns that uniquely identifies each row in a
table. It enforces uniqueness and helps in faster retrieval of data.

12. What is a foreign key in MySQL?


- A foreign key is a column or a set of columns in one table that refers to the primary key
in another table. It establishes a relationship between the two tables.

13. How do you add a foreign key constraint in MySQL?


sql
ALTER TABLE tablename ADD CONSTRAINT fk_name FOREIGN KEY (column)
REFERENCES other_table(primary_key_column);

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.

15. How do you create an index in MySQL?


sql
CREATE INDEX index_name ON tablename (column);

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.

17. What is the purpose of the `LIMIT` clause in MySQL?


- The `LIMIT` clause is used to limit the number of rows returned by a `SELECT`
statement. It is commonly used for pagination and improving query performance.

18. How do you retrieve distinct values from a column in MySQL?


sql
SELECT DISTINCT column FROM tablename;

19. What is the purpose of the `GROUP BY` clause in MySQL?


- The `GROUP BY` clause is used to group rows that have the same values into
summary rows. It is typically used with aggregate functions like `COUNT`, `SUM`, `AVG`, etc.

20. How do you perform joins in MySQL?


- Joins in MySQL are performed using the `JOIN` keyword to combine rows from two or
more tables based on a related column between them. Common types of joins include `INNER
JOIN`, `LEFT JOIN`, `RIGHT JOIN`, and `FULL JOIN`.

21. What is a subquery in MySQL?


- A subquery is a query nested within another query. It can be used within the `WHERE`,
`HAVING`, or `FROM` clauses of another query to perform operations on the result set.

22. What is the purpose of the `HAVING` clause in MySQL?


- The `HAVING` clause is used to filter the results of a `GROUP BY` clause based on
specified conditions. It is similar to the `WHERE` clause but is applied after the `GROUP BY`
operation.

23. How do you perform transactions in MySQL?


- Transactions in MySQL are started with the `START TRANSACTION` statement,
followed by a series of SQL statements, and then committed with the `COMMIT` statement or
rolled back with the `ROLLBACK` statement in case of errors.

24. What is normalization in MySQL?


- Normalization is the process of organizing data in a database to reduce redundancy
and dependency. It involves dividing large tables into smaller tables and defining relationships
between them to minimize data duplication and ensure data integrity.

25. What is denormalization in MySQL?


- Denormalization is the process of intentionally adding redundancy to a database for
performance reasons. It involves combining tables or duplicating data to optimize query
performance, especially for read-heavy workloads.

26. How do you create a stored procedure in MySQL?


sql
CREATE PROCEDURE procedure_name ()
BEGIN
-- SQL statements
END;

27. What is the purpose of the `TRIGGER` statement in MySQL?


- The `TRIGGER` statement in MySQL is used to define a set of actions that are
automatically performed in response to certain events on a table, such as `INSERT`,
`UPDATE`, or `DELETE` operations.

28. How do you grant privileges to a user in MySQL?


sql
GRANT privileges ON database_name.table_name TO 'username'@'hostname';

29. What is the

purpose of the `EXPLAIN` statement in MySQL?


- The `EXPLAIN` statement in MySQL is used to obtain information about how MySQL
executes a `SELECT` statement, including the query execution plan, index usage, and
estimated rows.

30. How do you take a backup of a MySQL database?


- MySQL databases can be backed up using tools like `mysqldump` or by using
MySQL's built-in backup tools like `mysqlbackup`.

31. What is the purpose of the `ORDER BY` clause in MySQL?


- The `ORDER BY` clause is used to sort the result set of a `SELECT` statement based
on one or more columns, either in ascending or descending order.

32. How do you remove a table in MySQL?


sql
DROP TABLE tablename;

33. What is the purpose of the `NOW()` function in MySQL?


- The `NOW()` function in MySQL is used to return the current date and time.

34. How do you rename a table in MySQL?


sql
RENAME TABLE old_table TO new_table;

35. How do you concatenate strings in MySQL?


sql
CONCAT(string1, string2, ...);

36. What is the purpose of the `DISTINCT` keyword in MySQL?


- The `DISTINCT` keyword in MySQL is used to eliminate duplicate rows from the result
set of a `SELECT` statement.

37. How do you add a column to an existing table in MySQL?


sql
ALTER TABLE tablename ADD columnname datatype;

38. What is the purpose of the `IN` operator in MySQL?


- The `IN` operator in MySQL is used to specify multiple values in a `WHERE` clause,
similar to multiple `OR` conditions.

39. How do you check if a table exists in MySQL?


sql
SHOW TABLES LIKE 'tablename';

40. What is the purpose of the `COUNT()` function in MySQL?


- The `COUNT()` function in MySQL is used to return the number of rows that match a
specified condition in a `SELECT` statement.

41. How do you find the maximum value in a column in MySQL?


sql
SELECT MAX(column) FROM tablename;

42. What is the purpose of the `LEFT()` function in MySQL?


- The `LEFT()` function in MySQL is used to extract a specified number of characters
from the left side of a string.

43. How do you convert a string to uppercase in MySQL?


sql
UPPER(string);

44. What is the purpose of the `BETWEEN` operator in MySQL?


- The `BETWEEN` operator in MySQL is used to specify a range of values in a
`WHERE` clause.
45. How do you calculate the average value in a column in MySQL?
sql
SELECT AVG(column) FROM tablename;

46. What is the purpose of the `CASE` statement in MySQL?


- The `CASE` statement in MySQL is used to perform conditional logic within a SQL
query.

47. How do you truncate a table in MySQL?


sql
TRUNCATE TABLE tablename;

48. What is the purpose of the `UUID()` function in MySQL?


- The `UUID()` function in MySQL is used to generate a Universally Unique Identifier
(UUID), which is a unique identifier that is unlikely to be duplicated.

49. How do you find the minimum value in a column in MySQL?


sql
SELECT MIN(column) FROM tablename;

50. What is the purpose of the `LIKE` operator in MySQL?


- The `LIKE` operator in MySQL is used to search for a specified pattern in a column.

Scenario Based Coding Questions:-

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;

You might also like