MySQL interview questions and answers

Home >> MySQL interview questions and answers >>

  MySQL interview questions and answers part - 4

This article will give you an overview of MySQL questions and answers that may be helpful for you in an interview or online test.

Question: How would you check if MySql service is running or not on linux.
You can use following script to check MySql service.

 Example:
 # /etc/init.d/mysqld status // to check MySql service.

Question: If the service is running/stop how would you stop/start/restart the service on linux.
You can use following script to start, stop and restart the mysql service.

 Example:
 # /etc/init.d/mysqld start // for start.
 # /etc/init.d/mysqld stop // for stop.
 # /etc/init.d/mysqld restart // for restart.

Question: How will you login to MySQL.
You can use following script to get login to MySQL.

 Example:
 # mysql -u 'db-username' -p

After that it will ask for password, then enter the password.

Now you have successfully login to mysql server.

Question: How will you obtain list of all the databases?
After getting login to MySQL, you can use the following query to show all the database.

 Example:
 mysql> show databases;

Question: How will you switch to a database, and start working on that? 
You can use the following query to switch the database.

 Example:
 mysql> use 'database-name';

Question: How will you get the list of all the tables, in a database?
You can use the following query to get the list of all the tables, in a database.

 Example:
 mysql> show tables;

After that it will display the name of all tables in selected database.

Question: How will you get the Field Name and Type of a MySql table?
You can use the following query to get get the Field Name and Type of a MySql table.

 Example:
 mysql> describe 'table-name';

Question: How will you see all the contents of a table?
You can use the following query to see all the contents of a table.

 Example:
 mysql> select * from 'table-name'; 

Question: How will you delete a table?    
You can use the following query to delete a table.

 Example:
 mysql> drop table 'table-name'; 

Question: How will you delete a database?
You can use the following query to delete a database.

 Example:
 mysql> drop database 'database-name'; 

Question: How to get current MySQL version?
You can use the following query to check MySQL version.

 Example:
 mysql> SELECT VERSION();

Question: How can you see all indexes defined for a table?
Indexes are defined for the table by.

 Example:
 mysql> SHOW INDEX FROM 'table_name';

Question: How do you get current user in mysql?
You can use the following query to get current user in MySQL.

 Example:
 mysql> SELECT user();

Question: How can I check MySQL engine type for a specific table?
You can use the following query to check MySQL engine for a specific table.

 Example: 
 mysql> SHOW TABLE STATUS WHERE Name = 'table_name';

Question: How would you change a table to InnoDB.
You can use the following query to change table engine.

 Example:  
 mysql> ALTER TABLE 'table_name' ENGIEN InnoDB;

Question: How to display Nth highest salary from a table in a MySQL query?
Let us take a table named 'employee' and the salary  data is given below in the image.

 Example:
 // To find Nth highest salary is:
 mysql> SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC LIMIT 1;
 // Result will be : 40000.
 
 // If you want to find 3rd largest salary:
 mysql> SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC LIMIT 2, 1; 
 // Result will be : 25000. 

Write your comment now