Top 14 MySQL Interview Questions and Answers

Here, you will come across some of the most frequently asked questions in MySQL job interviews which will help you in your interview preparation.

Let's have a look at some of the most popular and significant MySQL questions and answers:

Interview Questions




Most Essential And Frequently Asked Interview
Questions And Answer

MySQL

Q.1. What do you mean by foreign key?

Ans.:

SQL | Foreign Key

A foreign key is a key used to link two tables. 

View Detials

Q.2. What is a view in SQL?

Ans.:

SQL | View

View in SQL is a kind of virtual table. It does not physical exists in our database but have rows and column as there we have in a real database table. we can create a view by selecting fields from one of the tables present in the database. A View can have all the rows of a table or specific rows based on certain conditions.

View Detials

Q.3. What is the difference between primary key and unique constraints?

Ans.:

SQL | Difference between primary key and unique constraints

There is only one primary key in a table. It creates the clustered index automatically and it cannot have NULL value
whereas there can be multiple unique constraints in a table. It does not create the clustered index automatically and it can have a NULL value

Q.4. Write an SQL query to find names of employee start with 'A'?

Ans.:

SQL | LIKE operator

The LIKE operator of SQL is used for such kind of operations. It is used to fetch filtered data by searching for a particular pattern in where clause.

View Detials

Q.5. What is the difference between BETWEEN and IN operator in SQL?

Ans.:

SQL | difference between BETWEEN and IN operator

The BETWEEN operator selects all records between the given range whereas IN operator selects all matching records given with the WHERE clause.

View Detials

Q.6. How can we repair a MySQL table?

Ans.:

MySQL | Repair Table

REPAIR command is used for repair a MySQL table

REPAIR TABLE users

We can also specify Quick and Extended after the table name

REPAIR TABLE users QUICK

If we specify Quick then MySQL will do a repair of only the index tree.

REPAIR TABLE users EXTENDED

 But if we specify the extended then it creates index row by row.

Q.7. Find nth largest salary from employees table?

Ans.:

By using following query we can find out the nth largest salary from employees table.

SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET n-1

In the above query n is replaced by any number where n >= 1. Suppose you want to find fifth highest salary then you have to use offset = 5-1 i.e.: 4

The query will be:

SELECT salary FROM `employees` ORDER BY salary DESC LIMIT 1 OFFSET 4

 

Q.8. How can we know the number of days between two given dates using MySQL?

Ans.:

DATEDIFF function is use for finding the number of days between two given dates using MySQL

DATEDIFF(‘2018-03-07′,’2017-01-01’)

 

Q.9. What are the 3 different ways in PHP to connect with MySQL?

Ans.:

The three different way in PHP to connect with MySQL is:

  1. MySQL
  2. MySQLI
  3. PDO

Note: We should use MySQLI because MySQLI is imporoved version of MySQL and have more function then MySQL.

Q.10. What are the different storage engine present in MySQL?

Ans.:

There are 5 different types of storage engine present in MySql.

  1. MyISAM
  2. Heap
  3. Merge
  4. INNO DB
  5. ISAM

MyISAM is the default storage engine for MySQL

Q.11. How can we find the number of rows in a table using MySQL?

Ans.:

MySQL | Find the number of rows in a table

We can use the below query to count the number of rows in a table

>SELECT COUNT(*) FROM table_name;

 

Q.12. How can we increase the speed of a MySQL select query?

Ans.:

MySQL |  Increase the speed of a MySQL select query

For increasing the speed of a query we have to do a couple of things

  • First of all instead of using select * from table1, use select column1, column2, column3.. from table1.
  • Try to introduce the index in the table.
  • Always use the limit keyword if we are looking for any specific number of rows from the result set.

 

Q.13. What is the best collation to use for MySQL?

Ans.:

MySQL | What is the best collation to use for MySQL?

Mostly we use:

  • utf8_general_ci
  • utf8_unicode_ci

utf8_general_ci is used for fast sorting but it is less accurate whereas utf8_unicode_ci is used for better accuracy but a little slow as compare to utf8_general_ci.

We can also use other collations based on specific languages like utf8_swedish_ci.

Q.14. What are triggers in Mysql?

Ans.:

Mysql | Trigger

The trigger can be defined as a database object just like a stored procedure. It is a set of actions that fires automatically when an event occurs in a database.

View Detials