3 Essential MySQL Interview Questions *

Toptal sourced essential questions that the best MySQL developers and engineers can answer. Driven from our community, we encourage experts to submit questions and offer feedback.

Hire a Top MySQL Developer Now
Toptal logois an exclusive network of the top freelance software developers, designers, marketing experts, product managers, project managers, and finance experts in the world. Top companies hire Toptal freelancers for their most important projects.
1.

Consider the following table definition in a MySQL database:

CREATE TABLE example (
  id INT NOT NULL,
  text1 VARCHAR(32) NOT NULL,
  text2 VARCHAR(32) NOT NULL DEFAULT 'foo'
);

What will be the result of the two INSERT statements below if strict SQL mode is enabled? How will the result differ if strict SQL mode is not enabled? Explain your answer.

INSERT INTO example (id) VALUES(1);
INSERT INTO example (text1) VALUES('test');
View answer

Since the text1 and text2 columns are marked as NOT NULL and have no explicit DEFAULT clauses, attempts to insert implicit NULL values into these columns should presumably fail (note that you can use the SHOW CREATE TABLE statement to see which columns have an explicit DEFAULT clause). So accordingly, these two INSERT statements should fail since they don’t specify a value for one or more of these columns.

If strict SQL mode is enabled, an error will indeed occur and the statements will be rolled back. (Note: In the case of a multi-row statement on a non-transactional table, an error occurs, but if this happens for the second or subsequent row of the statement, the preceding rows will have been inserted.)

However, if strict SQL mode is *not enabled,* MySQL sets the column to the implicit default value for the column data type rather than failing.

In MySQL, implicit defaults are determined as follows:

  • For numeric types, the default is typically zero.
  • For date and time types, the default is the appropriate “zero” value for the type. (This actually a bit more complicated for fields with the type TIMESTAMP.)
  • For string types (other than ENUM), the default value is the empty string. For ENUM, the default is the first enumeration value.

Therefore, after the above two INSERT statements are executed, the contents of the table will be as follows:

mysql> SELECT * FROM example;
+----+-------+-------+
| id | text1 | text2 |
+----+-------+-------+
|  1 |       | foo   |
|  0 | test  | foo   |
+----+-------+-------+

As shown, MySQL has inserted an empty string into column text1 on the first row, and zero into column id on the second row, even though each column is defined as NOT NULL with no DEFAULT clause.

2.

If we create a table and insert a row as follows:

CREATE TABLE game (team1 VARCHAR(32), team2 VARCHAR(32), start TIMESTAMP, end TIMESTAMP);
INSERT INTO game VALUES ("argentina", "brazil", now(), now());

The resulting table will be as follows:

+-----------+--------+---------------------+---------------------+
| team1     | team2  | start               | end                 |
+-----------+--------+---------------------+---------------------+
| argentina | brazil | 2014-07-17 20:44:35 | 2014-07-17 20:44:35 |
+-----------+--------+---------------------+---------------------+

Given the above table definition, what will be the effect of the following SQL:

UPDATE game SET team1 = "uruguay" WHERE team1 = "argentina";
View answer

The updated table will then be as follows:

+-----------+--------+---------------------+---------------------+
| team1     | team2  | start               | end                 |
+-----------+--------+---------------------+---------------------+
| uruguay   | brazil | 2014-07-17 20:50:10 | 2014-07-17 20:44:35 |
+-----------+--------+---------------------+---------------------+

Note that, in addition to the team1 column being modified, the start column was automatically updated as well, but the end field remained unchanged. Why?

The reason the start column was automatically updated is that its type is TIMESTAMP. Fields of type TIMESTAMP have the behavior that when a record in the table is updated, the TIMESTAMP field (i.e., the start field in this example) gets updated to reflect the then-current time.

But surprisingly, if we have multiple columns of type TIMESTAMP, only the first TIMESTAMP column has this behavior but the others do not. That is why the end field remains unchanged in this example.

3.

How are Users and Grants different in MySQL than in other databases?

View answer

Creating a grant in MySQL can effectively create the user as well. MySQL users are implemented in a very rudimentary fashion. The biggest misunderstanding in this area surrounds the idea of a user. In most databases a username is unique by itself. In MySQL, however, it is the combination of user and hostname that must be unique. So, for example, if I create user john@localhost, john@server2 and john@server3, they are actually three distinct users, which can have distinct passwords and privileges. It can be very confusing that “john” logging in from the local command line has different privileges or password than john logging in from server2 and server3.

These sample questions are intended as a starting point for your interview process. If you need additional help, explore our hiring resources—or let Toptal find the best developers, designers, marketing experts, product managers, project managers, and finance experts for you.

Submit an interview question

Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for MySQL Developers?

Looking for MySQL Developers? Check out Toptal’s MySQL developers.

Abraham Bruck

Freelance MySQL Developer
United States
Toptal Member Since June 18, 2020

Abraham's been working with databases since 1984, including Oracle, PostgreSQL, MS Access, MySQL, and more in Unix, Windows, and AWS (cloud) environments. His work experience runs the gamut from data analysis, modeling, migration, conversion, integration, aggregation, mining, optimization, performance tuning, warehousing to extensive, varied, and detailed reporting.

Show More

Oleg Schalginski

Freelance MySQL Developer
Germany
Toptal Member Since February 16, 2022

Oleg is a software developer with over 20 years of experience automating business processes for various industries, including construction, finance, energy, telecom, and government. He has experience using various technologies, including JavaScript, TypeScript, PHP, MySQL, Oracle, Redis, and DynamoDB for storage, as well as frameworks, including .NET, Symfony, React, and Redux. Oleg's goal remains constant—to deliver uber-flexible, user-friendly interfaces that meet business needs.

Show More

Thanga Senthil Rajan Vivakaran

Freelance MySQL Developer
New Zealand
Toptal Member Since October 19, 2021

Raj is a full-stack developer and team leader with over ten years of experience in PHP, JavaScript (jQuery and Angular), SQL (PostgreSQL, MySQL, and Microsoft SQL), HTML, and CSS. He has extensive experience building web applications using several PHP frameworks and content management systems, including SilverStripe, Drupal, and WordPress. Raj upgrades his skills constantly by quickly learning and embracing new technologies.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more