Back to articles list Articles Cookbook
9 minutes read

Top 10 MySQL Interview Questions And Answers

You learned SQL and now you want to use this valuable skill to get a better job. Now it’s time to prepare for the job interview. We’ve got ten MySQL interview questions and answers to help you with that.

In today's hyperconnected world, knowing how to deal with it has become a necessary skill. That’s why you probably learned (or want to learn) SQL: either your job requires it or you realized that you need SQL to get a better job. Here’s what you should know about SQL to land a job.

One of the decisions you must make when learning SQL is which ‘flavor’ you want to start with. If you decide on MySQL, a good starting point would be our SQL Basics in MySQL course. It’s interactive, requires no previous IT experience, and gives you plenty of opportunities (126, to be exact) to practice writing MySQL queries.

Learning SQL is one thing. Getting better at it is another. Practicing what you learned is crucial, and our SQL Practice Set in MySQL course, with 88 additional exercises, is designed for that purpose.

Once you’re comfortable using MySQL, you decide to look for a job working with data. What kind of questions will you face during the interview? Let’s start with a few basics, then get into the details.

What Is MySQL?

MySQL is an open-source Relational Database Management System (RDBMS) – or, colloquially, database. It was created by MySQL AB, a Swedish company, and is now owned by the Oracle Corporation. It is the second most popular database in the world, and it’s been so consistently for the last eight years.

Top 10 MySQL Interview Questions And Answers

Source: https://db-engines.com/en/ranking_trend

What’s the reason for MySQL’s popularity? Being open-source (read: free!) is one of the main reasons. But that’s not the only reason.

MySQL is valued for its speed. Actually, this was one of the main ideas behind developing it. It is a web-based database, which means you don’t need expensive and elaborate systems to run it. Installing it on your computer is very easy; here are the MySQL installation  instructions.

Another of its strengths is scalability, which means a performance drop won’t follow an increase in data and workload.  Also, its dialect learning curve is relatively flat. MySQL is not only a database, but it’s also a distinct dialect of the SQL programming language. As with any natural language, SQL dialects conform to the language standard to some extent while also having their own distinct characteristics. MySQL is partially compliant with the SQL standard, but it also has (or doesn’t have) commands that can’t be found in standard SQL or some other dialects. Also, the syntax rules can sometimes slightly differ.

Learning MySQL means learning the second-most-popular SQL dialect and, at the same time, having a firm foundation in standard SQL. This allows you to move to other databases fairly easily if you need to.

With MySQL being so popular, you can imagine there are plenty of job options you can choose from.

What Jobs Can You Get With MySQL?

Some companies that reportedly use MySQL are Airbnb, Netflix, Amazon, Twitter, Uber, Shopify, and Slack. This is a whole range of companies, with one common point: they all heavily rely on data.

Top 10 MySQL Interview Questions And Answers

If you’re proficient with MySQL, you’ll usually have a wide choice of job opportunities. It might be as a data analyst, where you’ll be extracting data and analyzing it to gain business insights. Or you can work as a database designer, database administrator, or data engineer. Some tasks here include designing, building, and maintaining databases, or data cleaning and building data pipelines for others to use. Some other options include a job as a data scientist, statistician, or a quantitative analyst.

How do you find these jobs? There are many job-hunting websites that can help you land a position. You might also want to go freelance.

Ok, so much for the basics. No matter how well you know MySQL, your knowledge will be tested in job interviews. Let’s have a look at the more detailed questions you might be faced with!

MySQL Job Interview Questions (And Answers)

Question #1: What Is MySQL?

This is an easy question; we’ve already answered it.

Answer: MySQL is an open-source database (or RDBMS) that uses the MySQL dialect of SQL to communicate with databases. Developed by MySQL AB, it is now part of the Oracle Corporation.

Question #2: What Is DATETIME in MySQL? When Do You Use It?

Answer: DATETIME is a date and time data type in MySQL. It contains both date and time values in the format ‘YYYY-MM-DD hh:mm:ss’. It is used when the exact time (as well as the date) of an event has to be recorded – e.g. the date and time an order was placed or shipped, user logins and logouts, or  employees’ work hours.

You can refresh you knowledge of MySQL date functions by reading our in-depth guide.

Question #3: What’s the Difference Between CHAR_LENGTH() and LENGTH()?

Answer: Both CHAR_LENGTH() and LENGTH() are string functions. The difference is that CHAR_LENGTH() returns the length of the string in characters. The LENGTH() function returns the string length as a number of bytes.

Question #4: Is MySQL Case-sensitive?

Answer: Yes and no.

If you’re comparing nonbinary strings, such as CHAR, VARCHAR, or TEXT,  then the answer is no. This means that when you filter such data, it’s not necessary to know if the data is stored all in uppercase, lowercase, or a combination.

For example, if you need a list of all the employees named John, you can write your query like this:

SELECT first_name,
	 last_name
FROM employees
WHERE first_name = 'john';

It will return all the Johns in your company, no matter if their name is written as JOHN, John, or john.

If the comparison is made on binary strings (BINARY, VARBINARY, or BLOB), then MySQL is case-sensitive. For example, this code …

SELECT id,
       image
FROM post_data
WHERE image LIKE 'a%';

… will only return  data from the column image that starts with 'a'. Data that starts with 'A' will not be included.

If we’re talking about tables and their aliases, then case sensitivity depends on your operating system. If it’s Windows or macOS, the table names are not case sensitive. In Unix, they are case sensitive.

Column names and aliases, indices, and partitions are not case-sensitive, regardless of the operating system.

Question #5: Which JOINs Does MySQL Support?

Answer: MySQL supports the following four JOINs:

Note that MySQL doesn’t support FULL JOIN, which is supported by other databases.

Question #6: How Can I Label Data Based on Condition?

Answer: There are three ways to set a condition and label data based on it. The first one is the CASE statement. It is a conditional statement with the following syntax:

CASE 
    WHEN condition THEN value
    WHEN condition THEN value
    ELSE value
END CASE;

The second option is the IF statement. It has a similar syntax to the CASE statement:

IF condition THEN value
    ELSEIF condition THEN value
    ELSE value
END IF;

The third option is to use the IF() function. Even though it can be used in the same way, don’t confuse it with the IF statement. Its syntax is:

IF(condition, value_if_true, value_if_false);

Question #6: What Does the Query TRUNCATE TABLE employees Do?

Answer: This query deletes all the data from the table, but it doesn’t delete the table itself (like the DROP command does).

Unlike the DELETE command, TRUNCATE doesn’t delete data row by row; it drops the table and recreates it as an empty table. In practice, this means TRUNCATE is faster than DELETE, which is good to know when deleting data from large tables.

Question #7: Convert This Data Type to Float.

Given the table orders, convert the column price to a float data type.

Answer: There are two ways of changing data types in MySQL. The first one is the CONVERT() function. To do what the question asks, you should write this query:

SELECT CONVERT(price USING float)
FROM orders;

The argument is the column price from the table orders. The USING keyword is followed by the data type (FLOAT in this case).

Another option is using the CAST() function. You can rewrite the above query in the following way:

SELECT CAST(price AS float)
FROM orders;

The syntax is the same, except that AS replaces USING.

Question #8: Write a Query that Returns the Average Salary by Department.

You’re given the table employees with the following columns:

  • id
  • first_name
  • last_name
  • salary
  • department

Write a query that calculates the average salary by the department. The output should include all departments except Accounting.

Answer:

SELECT department,
       AVG(salary) AS average_salary
FROM employees
WHERE department <> 'accounting'
GROUP BY department;

This SELECT statement takes the department from the table and then uses the AVG() aggregate function to calculate the average salary. The question asks you to exclude the Accounting department; this is done in the WHERE clause. Since  MySQL is not case sensitive when comparing strings, it doesn’t matter whether you write ‘accounting’ in uppercase or lowercase.

Finally, the output is grouped by the department to show the average salary by department.

With this question, you’re entering the field of data analysis. Here are five SQL queries every aspiring data analyst should know

Question #9: Show Only Employees With a Salary Above the Company Average

Using the same table as the query above, you’re asked to show only those employees who earn more than the average of the entire company.

Answer:

SELECT first_name,
	  last_name,
	  salary
FROM employees
WHERE salary > (SELECT AVG(salary)
			   FROM employees);

This query selects the employees and their salary from the table employees. To show only employees with salaries above average, we need a subquery in the WHERE clause. The subquery uses the AVG() function to calculate the company’s average salary. WHERE then compares this result with each employee’s salary.

Every subquery can be replaced with a Common Table Expression (CTE). The interviewers might ask you about that, so get prepared by going through these five CTE interview questions beforehand.

Question #10: What Data Does This LEFT JOIN Query Return?

Here’s the query:

SELECT 
  c.id,
  c.first_name,
  c.last_name, 
  SUM(o.value) AS sum_of_orders
FROM customers c 
LEFT JOIN orders o 
ON c.id = o.cust_id
GROUP BY c.id, c.first_name, c.last_name
HAVING order_date > '2021-12-31';

Answer: The query returns the customers and the total value of their orders placed in 2022. Using LEFT JOIN means all the customers will be listed. If there are customers that didn’t place any orders in 2022, they will have a NULL value in the column sum_of_orders.

Do You Feel Ready For an SQL Job Interview?

The above ten examples should give you a good feel of what to expect at the job interview. Of course, there are plenty of other SQL concepts that could come up. They can be entry level questions for SQL developers or they can be a more advanced combination of theory and coding.

Whatever you do, don’t come unprepared to the job interview. Use resources like SQL Basics in MySQL and SQL Practice Set in MySQL to learn and fine-tune your coding skills. And of course, get some advice from your more senior colleagues; they’ve been there.