MySQL Interview Questions

What is MySQL?


What are the technical features of MySQL?

MySQL database software is a client or server system which includes

  • Multithreaded SQL server supporting various client programs and libraries
  • Different backend
  • Wide range of application programming interfaces and
  • Administrative tools.

Why MySQL is used?


What are Heap tables?



  • BLOB or TEXT fields are not allowed
  • AUTO_INCREMENT is not supported by HEAP tables
  • Indexes should be NOT NULL

What is the default port for MySQL Server?

The default port for MySQL server is 3306.

What are the advantages of MySQL when compared with Oracle?

  • MySQL is portable
  • GUI with command prompt.
  • Administration is supported using MySQL Query Browser

Differentiate between FLOAT and DOUBLE?

Following are differences for FLOAT and DOUBLE:


Differentiate CHAR_LENGTH and LENGTH?


How to represent ENUMs and SETs internally?


What is the usage of ENUMs in MySQL?


Define REGEXP?


Difference between CHAR and VARCHAR?

Following are the differences between CHAR and VARCHAR:

  • CHAR and VARCHAR types differ in storage and retrieval

Give string types available for column?

The string types are:

  • SET
  • BLOB
  • ENUM
  • CHAR
  • TEXT

What storage engines are used in MySQL?


Technique involves:

  • Storage mechanism
  • Locking levels
  • Indexing
  • Capabilities and functions.

What are the drivers in MySQL?

Following are the drivers available in MySQL:

  • PHP Driver
  • JDBC Driver
  • ODBC Driver
  • PYTHON Driver
  • PERL Driver
  • RUBY Driver
  • CAP11PHP Driver
  • net5.mxj



What is the difference between primary key and candidate key?



How do you login to MySql using Unix shell?

We can login through this command:


What does myisamchk do?


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


What is the difference between MyISAM Static and MyISAM Dynamic?


MyISAM Static would be easier to restore in case of corruption.

What are federated tables?


What, if a table has one column defined as TIMESTAMP?

Timestamp field gets the current timestamp whenever the row gets altered.





How can you see all indexes defined for a table?

Indexes are defined for the table by:

SHOW INDEX FROM <tablename>;



How can we convert between Unix & MySQL timestamps?

UNIX_TIMESTAMP is the command which converts from MySQL timestamp to Unix timestamp


What are the column comparisons operators?


How can we get the number of rows affected by query?

Number of rows can be obtained by

  SELECT COUNT (user_id) FROM users;

Is Mysql query is case sensitive?




seleCt vErSiOn(), current_DATE;

All these examples are same. It is not case sensitive.

What is the difference between the LIKE and REGEXP operators?


  SELECT * FROM employee WHERE emp_name REGEXP “^b”;SELECT * FROM employee WHERE emp_name LIKE “%b”;

What is the difference between BLOB AND TEXT?


  • BLOB



  • TEXT



What is the difference between mysql_fetch_array and mysql_fetch_object?

Following are the differences between mysql_fetch_array and mysql_fetch_object:


mysql_fetch_object –  Returns a result row as object from database.

How can we run batch mode in mysql?

Following commands are used to run in batch mode:

  mysql ;mysql mysql.out


Each MyISAM table is stored on disk in three formats:

  • The ‘.frm’ file stores the table definition

What are the different tables present in MySQL?

Total 5 types of tables are present:

  • MyISAM
  • Heap
  • Merge
  • ISAM

MyISAM is the default storage engine as of MySQL .

What is ISAM?


What is InnoDB?


How MySQL Optimizes DISTINCT?



How to enter Characters as HEX Numbers?



How to display top 50 rows?



How many columns can be used for creating Index?




CURRENT_DATE() shows current year,month and date only.

What are the objects can be created using CREATE statement?

Following objects are created using CREATE statement:

  • USER
  • VIEW

How many TRIGGERS are allowed in MySql table?



What are the nonstandard string types?

Following are Non-Standard string types:

  • TEXT

What are all the Common SQL Function?










Explain Access Control Lists?



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:


What is TEXT?

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


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


How would you enter Characters as HEX Numbers?

– 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;



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.


$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.




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




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?


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


– 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.


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.


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


[[email protected] 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.


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.


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

mysql > describe test;
| 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
[[email protected] tmp]# service mysqld stop
Stopping MySQL: [ OK ]
[[email protected] 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:

[[email protected] ]# mysqld_safe –skip-grant-tables –skip-networking &[1] 13007
[[email protected] ]# Starting mysqld daemon with databases from /var/lib/mysql
[[email protected] ]#

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
[[email protected] tmp]# mysql -u root

Your MySQL connection id is 1 to server version: 4.1.16
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;

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?


2. Heap
3. Merge

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:


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?


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(‘’, ‘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

[WHERE where_condition]
[LIMIT row_count]


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..

Explain the tools that are available for managing MySQL Server.

Following are the tools to manage MySQL server:

mysqld – MySQL server daemon. It is used to start the mysql server.
mysqladmin – Used to perform administrative tasks.


Mysqladmin shutdown – Used to shut down mysql server.
Mysqladmin ping – check the alive status of the server.
Mysqladmin version – to check version of mysql server.
mysql – A command-line interface for end users to manage user data objects.
mysqlcheck – A command-line interface for administrators to check and repair tables.
mysqlshow – A command-line interface for end users to see information on tables and columns.
mysqldump – A command-line interface for administrators or end users to export data from the server to files.
mysqlimport – A command-line interface for administrators or end users to load data files into tables program tool to load data into tables.

Explain the 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.

Apart from offering a wide list of command line options, mysql at a broad level is used for:-

1. Format query output as HTML tables and XML elements
2. Provides access to server side help.
3. Allows files to be executed as a batch.

Following are the most commonly used mysql command line options:

-u username – Specifies username while connecting to server.
-p password – Specifies password while connecting to server.
-h hostname – Specifies hostname when server is running. Default being local host.
-e command – Executes the specified command.

What is ``mysqld``?

Mysqld is a daemon server program typically used to start and stop mysql server. It runs in the background

Here’s how mysqld can be invoked:

>cd \mysql\bin
>mysqld –console

Following are the options provided:

1. –help – Displays the help message.
2. –version – returns the version.
3. –log=filename- Specifies server to write log files to filename.
4. –log-error=filename- Specifies server to write error log files to filename.