Have a Data engineering or data science interview coming up? Need to practice some of the most asked MySQL interview questions? The article compiles the list of the MySQL interview questions that you should know.
Common MySQL Interview Questions & Answers
1. What is MySQL?
MySQL is one of the most popular open-source DBMS (database management system). MySQL is easy to use, reliable, and fast. A DB management system that works on embedded systems as well as client-server systems.
2. Why is MySQL so popular?
First of all, MySQL is open-source. Second, it is widely adopted, so a lot of code is already available. Even entire developed systems are there that can be referred to for the upcoming projects. MySQL has relational databases; hence it makes it have methodical storage rather than a big dump of unorganized mess. And finally, as said earlier, MySQL is quick and robust.
3. What are the tables in MySQL? Explain the types.
This is a must-know MySQL interview question. Let’s see the answer-
MySQL stores everything in logical tables. Tables can be thought of as the core storage structure of MySQL. And hence tables are also known as storage engines. Here are the storage engines provided by MySQL:
· MyISAM – MyISAM is the default storage engine for MySQL. It extends the former ISAM storage engine. MyISAM offers big storage, up to 256TB! The tables can also be compressed to get extra storage. MyISAM tables are not transaction-safe.
· MERGE – A MERGE table is a virtual table that consolidates different MyISAM tables that have a comparable structure to one table. MERGE tables use the indexes of the base tables, as they do not have indexes of their own.
· ARCHIVE – As the name suggests, Archive helps in archiving the tables by compressing them, in-turn reducing the storage space. Hence, you can store a lot of records with the Archive. It uses the compression-decompression procedure while writing and reading the table records. It is done using the Zlib library.
· CSV – This is more like a storage format. CSV engine stores the values in the Comma-separated values (CSV) format. This engine makes it easier to migrate the tables into a non-SQL pipeline.
· InnoDB – InnoDB is the most optimal while choosing an engine to drive performance. InnoDB is a transaction-safe engine. Hence it is ACID-compliant and can efficiently restore your database to the most stable state in case of a crash.
· Memory– Memory tables were formerly known as HEAP. With memory tables, there can be a performance boost as the tables are stored in the memory. But it does not work with large data tables due to the same reason.
· Federated – Federated tables allow accessing remote MySQL server tables. It can be done without any third-party integration or cluster technology.
4. Write a query for a column addition in MySQL
For this, an ALTER TABLE query is required. Once invoked, simply mention the column and its definition. Something like this:
ALTER TABLE cars
ADD COLUMN engine VARCHAR(80) AFTER colour;
5. What is a foreign key? Write a query to implement the same in MySQL.
A foreign key is used to connect two tables. A FOREIGN KEY is a field (or assortment of it) in one table that alludes to the PRIMARY KEY in another table. The FOREIGN KEY requirement is utilised to forestall activities that would crush joins between tables.
To assign a foreign key, it is important to mention it while creating the table. It can be assigned by invoking the FOREIGN KEY query. Something like this:
FOREIGN KEY (Any_ID) REFERENCES Table_to_reference(Any_ID)
6. What is MySQL workbench?
MySQL Workbench is a bound together visual instrument for database modelers, designers, and DBAs. MySQL Workbench provides Data modelling, SQL, and server setup set of administrative tools. To put it simply, MySQL workbench makes it possible to operate the database management system through GUI.
7. How does database import/export work in MySQL?
It can be done in two ways. One is to use phpMyAdmin, and the second is to use the command line access of MySQL. The latter can be done by using the command named mysqldump. It goes something like this:
· mysqldump -u username -p databasename > dbsample.sql
To import a database into MySQL, only a sign change is required, with a command of MySQL. The command goes something like this:
· mysql -u username -p databasename < dbsample.sql
8. How can we delete a column or a row in MySQL?
Now dropping a column can be simply done by using the ALTER TABLE command and then using the DROP command. It goes something like this:
ALTER TABLE table_name DROP column name;
To drop a row, first, an identification for the row is required. Once that is handy, use the DELETE command in conjunction with the conditional WHERE command. Something like this:
DELETE FROM cars WHERE carID = 3;
9. What are the different ways to join tables in MySQL?
Join is used to link one or more tables together, with the common column’s values in both tables. Primarily there are four types of joins:
1. Inner Join – Inner join uses a join predicate, which is a condition used to make the join. Here is the syntax:
SELECT something FROM tablename INNER JOIN another table ON condition;
2. Left Join – Left join also requires a join condition. The left join chooses information beginning from the left table. For each entry in the left table, the left compares each entry in the right table. Here is the syntax:
SELECT something FROM tablename LEFT JOIN another table ON condition;
3. Right Join – Opposite to left join and, with one difference in the query, that is the name of join. Here care should be taken about the order of tables. Here is the syntax:
SELECT something FROM tablename LEFT JOIN another table ON condition;
4. Cross Join – Cross join has no join condition. It makes a cartesian of rows of both the tables. Here is the syntax:
SELECT something FROM tablename CROSS JOIN another table;
Note: While dealing with just one table, self-join is also possible.
It is one of the most dealt with MySQL interview questions. Interviewers do like to see if the candidate understands the basics or not and join one of the core concepts.
10. Can a primary key be dropped in MySQL? If yes, how?
Yes, it is possible to drop the primary key from a table. The command to use is again, the ALTER TABLE followed by DROP. It goes like this:
ALTER TABLE table_name DROP PRIMARY KEY;
11. What are Procedures in MySQL?
Procedures (or stored procedures) are subprograms, just like in a regular language, embedded in the database. A stored procedure consists of a name, SQL statement(s) and parameters. It utilises the caching in MySQL and hence saves time and memory, just like the prepared statements.
12. What is a trigger in MySQL?
A trigger is a table-associated database object in MySQL. It is activated when a specified action takes place.
A trigger can be invoked after or before the event takes place. It can be used on INSERT, DELETE, and UPDATE. It uses the respective syntax to define the triggers. For example, BEFORE INSERT, AFTER DELETE, etc.
13. How to add users in MySQL?
To simply put, the user can be added by using the CREATE command and specifying the necessary credentials. First, log in to the MySQL account and then apply the syntax. Something like this:
CREATE USER ‘testuser’ IDENTIFIED BY ‘sample password’;
Users can be granted permissions, by the following commands:
GRANT SELECT ON * . * TO ‘testuser’;
14. What is the core difference between Oracle and MySQL?
The core difference is that MySQL works on a single-model database. That means it can only work with one base structure, while Oracle is a multi-model database. It means it can support various data models like graph, document, key-value, etc.
Another fundamental difference is that Oracle’s support comes with a price tag for industrial solutions. While MySQL is open-source.
Now this question is one of the MySQL interview questions that should be understood carefully. Because it directly deals with the industry standards and what the company wants.
15. What is CHAR and VARCHAR in MySQL?
Both of them define a string. The core difference is that CHAR is a fixed-length while VARCHAR is variable length. For example, if CHAR(5) is defined, then it needs exactly five characters. If VARCHAR(5) is defined, then it can take at most five characters. VARCHAR can be said to have more efficiency in the usage of memory as it can have dynamic memory allocations.
16. Which drivers are necessary for MySQL?
There are many types of drivers in MySQL. Mostly they are used for connections with different computational languages. Some of them are listed below:
· PHP Driver
· Python Driver
· C – Wrapper
· Perl and Ruby Drivers
17. What is a LIKE statement? Explain % and _ in LIKE.
While using filters in commands like SELECT, UPDATE, and DELETE, conditions might require a pattern to detect. LIKE is used to do just that. LIKE has two wildcard characters, namely % (percentage) and _ (underscore). Percentage(%) matches a string of characters, while underscore matches a single character.
For example, %t will detect trees and tea both. However, _t will only detect one extra character, i.e., strings like ti or te.
18. How to convert timestamps to date in MySQL?
It is a rather simple question that requires knowledge on two commands, like DATE_FORMAT and FROM_UNIXTIME.
DATE_FORMAT(FROM_UNIXTIME(`date_in_timestamp`), ‘%e %b %Y’) AS ‘date_formatted’
Also Read: Java Interview Questions & Answers
19. Can a query be written in any case in MySQL?
This MySQL interview question often confuses people who are just getting started with MySQL. Although most of the time, the queries are written in capital or some in small letters, there is no such case sensitivity to MySQL queries.
For example, both create table tablename and CREATE TABLE tablename, works fine.
However, if required, it is possible to make the query case sensitive by using the keyword BINARY.
This MySQL interview question can be tricky, especially when asked to make the query case-sensitive explicitly.
20. How to save images in MySQL?
Images can be stored in the MySQL database by converting them to BLOBS. But it is not preferred due to the large overhead it creates. Plus, it puts unnecessary load on the RAM while loading the entire database. It is hence preferred to store the paths in the database and store the images on disk.
21. How to get multiple condition results from data in MySQL?
There are two ways to do so. The first is to use the keyword OR while using the WHERE condition. The other is to use a list of values to check and use IN with WHERE.
22. What are the different file formats used by MyISAM?
Typically, a MyISAM table is stored using three files on disk. The data file and the index file, which are defined with extensions .MYD and .MYI, respectively. There is a table definition file that has .frm extension.
23. How does DISTINCT work in MySQL?
DISTINCT is used to avoid the problem of duplicity while fetching the results of a particular query. DISTINCT is used to make sure the results do not contain repeated values. DISTINCT can be used with the SELECT clause. Here is the syntax for it:
SELECT DISTINCT something FROM tablename;
24. Is there any upper limit for the number of columns in a table?
Although the exact size limitation depends on many factors, MySQL has a hard limit on max size to be 4096 columns. But as said, for a given table, the effective-maximum may be less.
25. What are Access Control Lists or ACLs, in accordance with MySQL?
The ACLs or Access control lists are used in a way to give a guideline for security in the MySQL database. MySQL provides security based on ACLs for all the tasks performed by users like connection requests, queries, and any other operation.
26. How to make connections persistent in MySQL?
While making a connection request, if Mysql_pconnect is used rather than mysql_connect, then it can make the connection persistent. Here ‘p’ means persistent. The database connection is not closed every time.
27. Explain the SAVEPOINT statement in MySQL.
SAVEPOINT is a way of making sub-transactions in MySQL, which are also known as nested transactions.
SAVEPOINT marks a point in a regular transaction. It indicates a point to which the system can rollback.
Check out: SQL Developer Salary in India
So, these were some MySQL interview questions. To know about the subject and other preparations, do visit upGrad courses and PG programs that help you in finding the right track and applications to boost your career.
If you are curious to learn about SQL, and more about full-stack development, check out IIIT-B & upGrad’s PG Diploma in Full Stack Software Development which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.