57 MySql Interview Questions and Answers

Dear Readers, Welcome to MySQL Interview questions with answers and explanation. These 57 solved MySQL questions will help you prepare for technical interviews and online selection tests conducted during campus placement for freshers and job interviews for professionals.

After reading these tricky MySQL questions, you can easily attempt the objective type and multiple choice type questions on this topic.

How many TRIGGERS are allowed in MySql table?

MySql table allows following 6 triggers:

- BEFORE INSERT
- AFTER INSERT
- BEFORE UPDATE
- AFTER UPDATE
- BEFORE DELETE and
- AFTER DELETE

Differentiate between FLOAT and DOUBLE.

FLOAT stores floating point numbers with accuracy up to eight places and has four bytes while DOUBLE stores floating point numbers with accuracy upto 18 places and has eight bytes.

Tell us something about Heap tables.

- HEAP tables are found in memory.
- They are used for high speed storage on temporary basis.

Some of their characteristics are:
- They do not allow BLOB or TEXT fields.
- Only comparison operators like =, <, >, = >, =< can be used with them.
- AUTO_INCREMENT is not supported by HEAP tables
- Indexes should be NOT NULL

How do you control the max size of a HEAP table?

- Maximum size of Heap table can be controlled using MySQL config variable called max_heap_table_size.

What are the advantages of MySQL in comparison to Oracle?

- MySQL is open source software available at zero cost.
- It is portable.
- GUI with command prompt.
- Administration is supported by MySQL Query Browser.

What does myisamchk do?

- It compresses the MyISAM tables, which reduces their disk or memory usage

How can we convert between Unix & MySQL timestamps?

- MySQL timestamp can be converted into Unix timestamp using the command UNIX_TIMESTAMP.
- Unix timestamp can be converted into MySQL timestamp using the command FROM_UNIXTIME.

What is BLOB?

- BLOB stands for binary large object.
- It that can hold a variable amount of data.

There are four types of BLOB based on the maximum length of values they can hold:

- TINYBLOB
- BLOB
- MEDIUMBLOB
- LONGBLOB

What is TEXT?

TEXT is case-insensitive BLOB. The four types of TEXT are:

- TINYTEXT
- TEXT
- MEDIUMTEXT
- LONGTEXT

What is the difference between BLOB and TEXT?

- In BLOB sorting and comparison is performed in case-sensitive for BLOB values.
- In TEXT types sorting and comparison is performed case-insensitive.

How is MyISAM table stored?

MyISAM table is stored on disk in three formats.
- ‘.frm’ file – storing the table definition
- ‘.MYD’ (MYData) - data file
- ‘.MYI’ (MYIndex) – index file

Explain advantages of MyISAM over InnoDB?

- MyISAM follows a much more conservative approach to disk space management – storing each MyISAM table in a separate file, which can be further compresses, if required.
- InnoDB stores the tables in tablespace. Further optimization is difficult with them.

How would concatenate strings in MySQL?

With the use of - CONCAT (string1, string2, string3)

How would you get the current date in Mysql?

By using
SELECT CURRENT_DATE();

How would you enter Characters as HEX Numbers?

- To enter characters as HEX numbers, you can enter HEX numbers with single quotes and a prefix of (X).
- Alternatively, just prefix HEX numbers with (Ox).

How are MySQL timestamps seen to a user?

- MySQL time stamps are seen to a user in a readable format : YYYY-MM-DD HH:MM:SS.

How will you export tables as an XML file in MySQL?

MYSQL’s query browser has a provision called “Export Result Set” which allows the tables to be exported as XML.

What is the use of i-am-a-dummy flag in MySQL?

Using the i-am-dummy flag makes the SQL engine refuse any Updates or deletes to execute if the WHERE clause is not present. It is very useful when using delete statements. Using i-am-dummy flag will not allow the following statement to execute:
Delete from employee;

What are the differences between MySQL_fetch_array(), MySQL_fetch_object(), MySQL_fetch_row()?

Mysql_fetch_object returns the result from the database as objects while mysql_fetch_array returns result as an array. This will allow access to the data by the field names.

E.g. using mysql_fetch_object field can be accessed as $result->name and using mysql_fetch_array field can be accessed as $result->[name]. mysql_fetch_row($result):- where $result is the result resource returned from a successful query executed using the mysql_query() function.

Example:
$result = mysql_query(“SELECT * from students");
while($row = mysql_fetch_row($result))
{
        Some statement;
}

What is difference between mysql_connect and mysql_pconnect?

Mysql_connect() opens a new connection to the database while mysql_pconnect() opens a persistent connection to the database. This means that each time the page is loaded mysql_pconnect() does not open the database. Mysql_close() cannot be used to close the persistent connection. Though it can be used to close mysql_connect().

What is MySQL data directory? How to determine the location of the data directory?

MySQL stores its data on the disk on the data dictionary. Each subdirectory under this data dictionary represents a MySQL database, inside those directories. By default the information managed my MySQL = server mysqld is stored in data directory.A default location of data directory in windows is C:\mysql\data or C:\Program Files\MySQL\MySQL Server 5.0 \data..

What you can use Regular Expression for in MySQL? Support your answer with an example.

Regular expressions in MySql are used in queries for searching a pattern in a string.

1. * Matches 0 more instances of the string preceding it.
2. + matches 1 more instances of the string preceding it.
3. ? Matches 0 or 1instances of the string preceding it.
4. . Matches a single character.
5. [abc] matches a or b or z
6. | separates strings
7. ^ anchors the match from the start.

REGEXP can be used to match the input characters with the database.

Example:

The following statement retrieves all rows where column employee_name contains the text 1000 (example salary):
Select employee_name
From employee
Where employee_name REGEXP ‘1000’
Order by employee_name

“.” Can be used to match any single character. “|” can be used to match either of the two strings

How will you export tables as an XML file in MySQL?

From the command prompt type the following statement:
mysql -u test --xml -e 'SELECT * FROM t1' > t1.xml

where ‘–u test‘ is the user name, --xml indicates the type of the file is xml, -e for export

What is the use of i-am-a-dummy flag in MySQL?

The flag i-am-a-dummy flag makes the MySQL engine to deny the UPDATE and DELETE commands unless the WHERE clause is present.

What are the differences between MySQL_fetch_array(), MySQL_fetch_object(), MySQL_fetch_row()?

The mysql_fetch_object() returns the result from the database as an object.
Ex: $result->name

The mysql_fetch_array() returns the result from the database as an associative array or numeric array or both by using mysql_NUM or mysql_ASSOC options.
EX: $result[0] ,$result['name']

The mysql_fetch_row() returns the result from the database as a numeric array.
Ex: $result[0]

What is difference between mysql_connect and mysql_pconnect?

Mysql_connect:

- Opens a new connection to the database.
- The database connection can be closed.
- Opens the page every time the page is loaded.

Mysql_pconnect:

- Opens a persistent connection to the database.
- The database connection can not be closed.
- The page need not be opened every time the page is loaded.

What is MySQL data directory? How to determine the location of the data directory?

MySQL data directory is most important location in which all MySQL databases are stored. The default data directory is located in the file mysql.

If the out of the space is the issue, then the directory need to be moved another location. Before moving, the database need to be closed. After moving the MySQL configuration file need to be edited. Look for the ‘datadir’ entry and change the path to the new directory.

What you can use Regular Expression for in MySQL? Support your answer with an example.

Regular expressions are a set of characters. Regular expressions are used for finding certain sequences in strings.

The following are the regular expression characters:

- * Represents matching characters that are 0 or more occurrences of the string that precedes it.
- + Represents matching characters that are 1 or more occurrences of the string that precedes it.
- ? Represents matching characters that are 0 or 1 occurrences of the string that precedes it.
- . Represents single character pattern matching.
- [abc] Represents matching characters that is either a or b or c.
- | Used for separation of strings.
- ^ Finds the matching pattern starting from the beginning.

To match the input characters with database tables, REGEXP is used. For example:
Select cityname
from territories
where cityname REGEXP ‘^(jo)*’;

The above query returns all city names that has the substring ‘jo’.

What are the steps required to view your MYSQL database?

There are certain steps that are required to before you can view MySQL database and they are as follows:

1. Login as database user

example:
[root@ tmp]# mysql -u mysqluser -p sales

2. List all your MySQL databases: use the show command to view the list of all available MySQL databases.

example
To view sales database use the command shown below:

mysql > show databases;
| Database |
| salesdata |

Write a command to view MySQL database table structure

To view the database table structure describe command is used that provides the list of all the data fields used in the database table.

Example

A table named sales will have the four fields like: name, description, num and date.

mysql > describe test;
| Field | Type | Null | Key | Default | Extra|
| num | int(11)| |PRI | NULL | auto_increment |
| date | date| | MUL | 0000-00-00 |
| name | varchar(50) | MUL |
| description | varchar(75) | YES | | NULL

Write a command to view the content of the table

To view all the data that is contained inside a table named sales use the select command. For example: to see the data of first row in a table using the command as:
mysql > select * from sales limit 1;

If the database is created recently then it will give a blank listing, but after the data is being entered it will show the full listing in a tabular form.

What is the procedure to configure the application of MYSQL?

The procedure to configure the application of MySQL is:

1. First create a database
2. Test the database by informing about the database name, IP address of database client server, username and password of application.
3. Edit the special application specific configuration file using a GUI or command line.
4. Configure the language with which MySQL will interact.

What are the applications required to support MYSQL?

The applications that are required to support MySQL are as follows:

1. php-mysql MySQL database is used specifically to support PHP
2. perl-DBI : provides generic Perl interface for interacting with relational databases
3. perl-DBD-MySQL database specific support for Perl
4. Web server is required to configure the database and its configuration
5. Programming language is required which supports MySQL.

Write a query to stop MYSQL in unix

The query to stop MySQL is quite useful when an error is occurred or when data has to be saved from any mishap. It is also used for retrieving the root password because it is either easily forgotten or misplaced. To stop the service the following command is required:
Stop MySQL
[root@ tmp]# service mysqld stop
Stopping MySQL: [ OK ]
[root@ tmp]#

Write a query to MYSQL in safe mode and to change the root password

To start MySQL in safe mode, mysqld_safe command is used which allow it to run in the safe mode and it also doesn’t allow the reading of tables with the database passwords:
[root@ ]# mysqld_safe --skip-grant-tables --skip-networking &[1] 13007
[root@ ]# Starting mysqld daemon with databases from /var/lib/mysql
[root@ ]#

After running the MySQL in safe mode the password protection gets removed and to use the password protection mechanism a command is used as follows:
mysql -u root command
[root@bigboy tmp]# mysql -u root

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.16
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> [ a message is being shown which allow user to take the control of the root]

How to take MYSQL database backup?

To take the database backup use the following syntax:
mysqldump --add-drop-table -u [username] -p[password] [database] > [backup_file]

This command will take the database backup by knowing the username and password for the database connection and dropping any table which is being deleted or not in use. It is always a good practice to take the backup of mysql as it contains all the database information that a user can access. While using the command keep a note that there should not be any space between –p switch and password, if there is then you will get a syntax error.

Write a command with which MySQL table can be repaired

The command syntax with which mysql table can be repaired is as follows:
REPAIR TABLE tablename;
REPAIR TABLE tablename QUICK;
REPAIR TABLE tablename EXTENDED;

The command will just do as it says repair a specified table, but if QUICK or EXTENDED is used then the meaning of it changes. In case of QUICK it will repair only the index tree, whereas in case of EXTENDED it will create index row by row and repair it.

What are the different tables present in MySQL?

There are many tables that remain present by default. But, MyISAM is the default database engine used in MySQL. There are five types of tables that are present:

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

What does the file with the extension: frm, myd, and myi contain?

MySQL default table type is MyISAM, where there are three kind of files that are stored inside MyISAM. The file names begin with the table name and have the extensions such as frm, myd and myi. The explanation of each file is given below:

.frm file consists of the table definition that are stored in the database
.myd is an extension that is used by a data file.
.myi is an extension that is used by index file.

What is the difference between MYSQL and SQL?

- SQL is known as standard query language, as the name implies it is the language which is used to interact with the database like MySQL.

- MySQL is a database that store various types of data and keep it safe. A PHP script is required to store and retrieve the values inside the database.

How database are managed?

Database is a collection of data and it is managed by a database server, which is a special program that is also known as MySQL database server. Application that you create usually communicates with the database server in the language which it can understand; mostly SQL language is used for communication. Database server in return interacts with the web server on same server or computer. Database server and web server result in the data which is being shown on the web.

What is required to create MYSQL database?

To create MySQL databse the first component which has to be present is a database server on which the queries of database will run and software tool through which you can access the applications. It also requires PHP scripts for communicating with the database using SQL commands.

What do you understand by MYSQL terminal?

MySQL terminal is used as a command line interface in many operating system. It provides a way to access the database and other resources using the SQL commands that are interpreted by the MySQL database server.

Why phpMyAdmin is used for MYSQL?

PhpMyAdmin is a very popular and easy to use GUI tool that can allow SQL commands to be run to create database, create tables, insert data and retrieve it. It provides a web based interface to the user for the ease of use. phpMyAdmin allows user to manage everything from one place and no other installation is required in the computer after this.

Write a query to create a database and a table?

MySQL comes up with some default database that can be used as a base to create a new one. The command that is used to create a new database is as follows:
CREATE DATABASE SQL command

The command has to be written in MySQL terminal. This command will create a new database and then you can create new tables and include data in it.

How can you make a database as your current database?

After making a database the first thing which has to be done is to create a table inside the database to test the new database that is being created. The command which is used to do that is:
USE aliendatabase;

This command allows us to make a database which is not a current database as my current. I have to just use the USE variable and the name of the database and it will become active for use.

What is the difference between a database and a table?

There is a major difference between a database and a table. The differences are as follows:

1. Tables are a way to represent the division of data in a database. Whereas, database is a collection of tables and data.
2. Tables group the data in relation with each other and create a dataset; this dataset will be used in the database. The data which are stored in the table in any form is a part of the database, but opposite is not true.

Is the syntax correct? Explain the meaning of the syntax given below

$dbc = mysqli_connect('data.aliensabductedme.com', 'owen', 'aliensrool', 'aliendatabase');

Yes the syntax is correct and this query is getting used to connect the database with a PHP script. PHP uses three functions to communicate with MySQL database. The three functions are:

mysqli_connect(), mysqli_query(), and mysqli_close().

mysqli_connect() and mysqli_query() are used to connect to mysql database and issue a query to the database. mysqli_query() is also used to retrieve the string or any other data from the database. Mysqli_close() is used to close a connection with the database.

How to connect a PHP script with the MySQL database?

PHP script can be connected with MySQL database by using the function called as mysqli_connect() function. You need to provide the information related to your location, username and password to the server to get permission to interact with the MySQL database server. When database name will be asked provide the name and it will connect you to the database.

What is the difference between truncate and delete?

Delete command is used to delete data from a table for example Remove emails where we write a script to delete the customer’s data. It deletes the rows of data from a table. The syntax of it as follows:
DELETE FROM table_name

Whereas, truncate is very dangerous command and should be used carefully as it deletes every row from a table. The syntax of it as follows:
TRUNCATE TABLE "table_name"

How important is to list the column names when doing an INSERT?

It is not important to list the column names when doing using an INSERT command as you can provide the column information and values in the table in the same order in which they appear in the table structure. It is safer and convenient way to specify the column names as it will keep the count of the column you are visiting.

Where’s database data actually stored? Is there a way to see the files which are stored?

Database data is usually get stored in the computer hard-disk and you can manage the data by the database program like MySQL and phpAdmin. The files can be seen but database files remain in binary format so it can be opened and read but, you have to put extra effort to understand it. SQL is given for the purpose of interacting with the database and read the database and retrieve the information out of it.

Why to use CHAR instead of VARCHAR in the database?

CHAR is much more accurate and efficient to use. CHAR doesn’t have to keep a count of the variable length. It is more efficient when you have to use it for a text column which is of an exact length. Char is used for the data which are fixed, but VARCHAR is used for data like password, which are variable.

What are ENUMs used for in MySQL?

ENUM is used to limit the possible values and store it together. It is a function that can be created to store the similar values together. It is used in creation of table.

The syntax of it is as follows:
CREATE TABLE months (month ENUM “January”, “February”, “March”,…);
INSERT months VALUES (“April”);

What is the purpose of -> in the MySQL terminal?

-> prompt in the command of MySQL indicates that a single statement is being entered across multiple lines. From this prompt MySQL interprets that you haven’t finished entering the statements. It has no impact of enter which you might press to go to the next line. MySQL will execute the statement only when you will insert the semicolon in the end which it recognizes.

How to find the unique values if the value in the column is repeated?

If the values in the column of a table are repeating and a unique value has to be found then the following command can be used in the query:
SELECT DISTINCT user_firstname FROM users;

There is another command which can be used to find the command to see the distinct values as:
SELECT COUNT (DISTINCT user_firstname) FROM users;

When to use ORDER BY in DELETE statement?

The ORDER BY clause in DELETE statement is used when a deletion has to take place by in a specified order.

The syntax is like this
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
ORDER BY
clause is specified, the rows are deleted in the order that is specified.

What is the difference between Unix timestamps and MySQL timestamps?

The unix timestamp is stored as 32 bit integer whereas, MySQL timestamps are stored in 32 bit integers but represented differently then UNIX timestamps like YYYY-MM-DD HH:MM:SS format. Unix timestamp is given as month-day-year-HH:MM:SS..
MySQL - Explain how can you start and shutdown MySQL server
Explain how can you start and shutdown MySQL server - Set the mysql directory and type: ./bin/mysqld_safe &......
MySQL - Tools that are available for managing MySQL Server
Tools that are available for managing MySQL Server - mysqld - MySQL server daemon. It is used to start the mysql server.....
MySQL - Purpose of "mysql" command-line interface
Purpose of mysql command-line interface - Mysql or mysql monitor is a Command line interface used to manage mysql’s data objects like databases, tables etc..
Post your comment
Discussion Board
Contents for interview Questions
Being a IT Recruiter i need to work on different Technology and make sure that i am talking to right candidate for specific requirement given to me. I have gone through contents for interview Questions for MYSQL and i must say that this is excellent site to ask questions for initial screening of candidates.

Excellent site with fully loaded information.

Nitin Gujarati
Nitin Gujarati 08-12-2015
database
would like to know more about usage of join and its types. (with syntax)
how would you perform normalization on tables with example table.
CSE 06-26-2014
question
how many type of tables in sqldb ? please describe them .
Durga prasad 04-6-2012
MySQL interview questions and answers
How can you see all indexes defined for a table?
SHOW INDEX FROM table_name;

How do you concatenate strings in MySQL?
CONCAT (string1, string2, string3)
Raveesh 12-6-2011
MySQL interview questions and answers
What is the default port for MySQL Server?
- 3306

What is the function of myisamchk?
- It compresses the MyISAM tables. This reduces their disk usage.

Explain CSV tables.
They special tables for which the data is saved into comma-separated values files. They cannot be indexed.
Paresh 12-6-2011
MySQL interview questions and answers
I want to find out all databases starting with ‘test’, I have access to?
- SHOW DATABASES LIKE ‘test%’;

Explain % and _ inside LIKE statement?
- % corresponds to 0 or more characters
- _ is exactly one character.

I want to start and stop MySQL on Windows? How do I do that?
- net start MySQL, net stop MySQL
Venkat 12-6-2011