My SQL
My SQL
main
Here are 100+ MySQL interview questions with their answers, ranging from basic to more
intermediate topics:
What is the difference between the HAVING clause and the WHERE clause?
Answer: WHERE filters records before aggregating in GROUP BY , whereas HAVING
filters after aggregation.
How would you list the number of students enrolled in each course, but only
display courses with more than 5 students?
Answer:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE some_column = some_value;
SELECT student_id
FROM students
WHERE EXISTS (SELECT 1 FROM enrollments WHERE students.student_id = enroll
Write a SQL query to find the 3rd highest salary from a salaries table.
Answer:
START TRANSACTION;
-- SQL operations
COMMIT;
Write a SQL query to rank employees based on their salary in descending order.
Answer:
Answer: The default storage engine was MyISAM up to MySQL 5.5, but InnoDB
became the default from MySQL 5.5 onward.
Answer: The SET type is used to store a set of strings. You can store zero or more
string values chosen from a list defined at table creation time.
SELECT * FROM table_name LIMIT 10 OFFSET 20; -- Skips the first 20 record
How can you retrieve the month part from a DATE field in MySQL?
Answer: Using the MONTH() function.
How can you transpose rows into columns, and vice versa, in a query result?
Answer: This process is known as "Pivoting". To convert rows to columns, you use
a combination of aggregate functions with CASE statements. For the reverse,
known as "Unpivoting", you can use UNION ALL .
-- Pivoting:
SELECT
SUM(CASE WHEN column = 'value1' THEN 1 ELSE 0 END) AS 'Value1',
SUM(CASE WHEN column = 'value2' THEN 1 ELSE 0 END) AS 'Value2'
FROM table_name;
-- Unpivoting:
SELECT 'Value1' AS 'Column', Value1 AS 'Value' FROM table_name
UNION ALL
SELECT 'Value2' AS 'Column', Value2 AS 'Value' FROM table_name;
MyISAM typically offers better read performance, while InnoDB offers better
write performance.
How can you lock a table explicitly?
Answer:
How do you get the second highest value from a table column?
Answer:
SELECT column_name
FROM table_name t1
WHERE some_value = (SELECT MAX(column_name) FROM table_name t2 WHERE t1.id
To restore:
mysql -u username -p database_name < backup.sql
SELECT ROW_COUNT();
How can you delete all records from a table without deleting the table?
Answer:
How do you combine results from multiple SQL queries and return a single table?
Answer: You can use the UNION or UNION ALL operator, depending on whether or
not you want duplicate records.
How can you convert a string to upper-case in MySQL?
Answer:
How can you remove leading and trailing whitespace from a string in MySQL?
Answer:
How can you find all the tables that have a specific column name in a database?
Answer:
SELECT table_name
FROM information_schema.columns
WHERE column_name = 'desired_column' AND table_schema = 'your_database_nam
These questions cover a range of advanced topics and should help in assessing the depth
of knowledge of individuals familiar with MySQL.
Answer: The WITH clause, also known as Common Table Expressions (CTE), provides a
temporary result set that you can reference within a SELECT , INSERT , UPDATE , or
DELETE statement. It's useful for breaking down complex queries.
WITH CTE_Name AS (
SELECT column1, column2
FROM table_name
WHERE condition
)
SELECT * FROM CTE_Name;
Answer: A self- join is a join where a table is joined with itself. It's useful for finding
relationships within the same table.
91. What are the different types of subqueries? Explain with examples.
SELECT column_name
FROM table_name
WHERE another_column = (SELECT MAX(column_name) FROM table_name);
SELECT column_name
FROM (
SELECT column_name FROM table_name WHERE condition
) AS subquery_name;
92. How can you update data in one table based on data in another table?
Answer:
UPDATE table1
SET table1.column_name = table2.column_name
FROM table2
WHERE table1.another_column = table2.another_column;
Answer:
94. What's the difference between INNER JOIN and OUTER JOIN ?
Answer: INNER JOIN returns rows when there's a match in both tables. OUTER JOIN
returns all rows from one table and the matching rows from the other table, filling with
NULL if no match is found.
95. How can you clone a table, including both data and schema?
Answer:
Answer:
INSERT INTO table_name (column1, column2)
VALUES (value1a, value2a),
(value1b, value2b),
...;
Answer: Partitioning divides a table into smaller, more manageable pieces, yet still
being treated as a single table. It can improve performance and assist in organizing
large datasets.
Answer: It's used to concatenate values from multiple rows into a single string.
100. How do you show the current SQL mode, and how can you change it?
Answer:
START TRANSACTION;
INSERT INTO table_name1 ...;
INSERT INTO table_name2 ...;
COMMIT; -- Or ROLLBACK;
102. What are the differences between VARCHAR and TEXT data types?
Answer: While both are used to store strings, VARCHAR can store up to 65,535
characters and you can specify its max length, while TEXT can store up to 65,535
characters without specifying max length. VARCHAR can have a default value, but TEXT
cannot.
103. How do you find and fix broken foreign key constraints?
Answer: Identify them using a LEFT JOIN to find orphaned records, and either delete
these records or update them to restore referential integrity.
Answer: FULLTEXT indexes are used for full- text searches. You can create one with:
105. How can you check for index fragmentation on a table and defragment it?
Answer: You can check fragmentation using SHOW TABLE STATUS LIKE 'table_name';
and optimize (defragment) using OPTIMIZE TABLE table_name; .
Answer:
108. What are MySQL stored procedures and how do you use them?
Answer: Stored procedures are SQL codes saved in the database to be reused. Created
using CREATE PROCEDURE , and called via CALL procedure_name() .
109. How would you monitor the performance of your MySQL database in real-time?
110. How can you run SQL script from the command line without entering the MySQL
console?
Answer: Use:
Answer: EXPLAIN provides a query execution plan, showing how MySQL will execute
the query, which can be vital for optimization.
Answer: By adding the NOT NULL constraint during table creation or modification.
Answer:
116. How do you retrieve only a specified number of characters from a string column?
Answer:
117. What are views in MySQL and why are they used?
Answer: Views are virtual tables based on the result set of an SQL statement. They
encapsulate the SQL statement and present data in a simplified manner, ensuring data
abstraction, protection, and to represent a subset of the data.
Answer:
SELECT MAX(column_name)
FROM table_name
WHERE column_name NOT IN (SELECT MAX(column_name) FROM table_name);
These questions should serve well for interviews at product- based companies that expect a
deep understanding of MySQL.