Top SQL Interview Questions
If you are looking for a role in software or technology, data science or systems, you will have to work with databases at some point.
SQL (Structured Query Language) allows users to access and manipulate data. It has been a popular programming language since it became standard in the 1980s.
It can be used to:
- Execute queries and retrieve data
- Insert/update/delete records
- Create databases, tables and procedures
- Allow users to present information in result-sets that can be used for a multitude of business purposes
Based around RDMS (Relational Database Management Systems), SQL is used in modern database systems like Oracle, Microsoft Access and MySQL.
There are many roles where knowledge and experience of SQL are needed – from the obvious database administrator roles to project manager and consultant roles.
Software engineers, .NET developers and business analysts all need to know how to access and manipulate data. With so many roles across multiple sectors, it is a career path with lots of potential.
There are thousands of roles requiring SQL, but getting a tech job is not always simple.
Even if you have mastered the syntax, applied the statements and can navigate a database quickly and efficiently, there is more to the interview process than proving your abilities.
Your resume might have landed you the interview, but the next step is up to you and your expertise.
There are a few things to think about when you are preparing for an interview.
It goes without saying that you want to make a great first impression, so taking the time to be well prepared will not only ensure that you present yourself well but will also allow you time to practice and get up to speed on some of the areas where you might struggle.
As soon as the time and date are confirmed, you should be getting yourself ready.
One of the first things to decide is what you are going to wear.
Depending on the sector, techincal jobs tend to require more casual attire than more corporate roles, but you should always dress smartly for an interview.
Choosing the right interview outfit early will make sure that you don’t have a last-minute panic about what you are wearing.
If you are stuck on the most appropriate attire, we have some clothing tips that might help.
As you have applied for a role that requires SQL, you should already have a working knowledge of the syntax, keywords, operators and commands that make up SQL statements – but even if you feel confident about your level of knowledge, extra practice is always a good idea.
You can find practice questions online that can test your knowledge and abilities. Make the most of the time leading up to your interview to brush up on the language you need to know and the most used operators and statements.
One of the best ways to prepare for your interview is to do some research on the company.
You may have applied for hundreds of SQL roles, but you want the recruiter to know you are serious about this particular job at their company.
Finding out all you can about the business will make sure they know you have taken the initiative and are interested.
Researching the company can help you find out what their ethos is, how their culture works and what their future plans might be.
They might have shared exciting news, details of awards they have won, and all sorts of other information on their website.
You can also use the job description to help you prepare for the interview.
If they mention that they are looking for someone proficient in SQL and Java, then ensuring you have the relevant knowledge and experience in both is necessary.
Of course, if you don’t have the expertise, learn some basics if you can and explain honestly that you are still learning – and are willing to take on more learning to become proficient.
Both the research on the company and the information in the job description will give you some idea of the kind of person they are looking for, allowing you to present yourself in a way that matches their needs.
For example, if they are an educational establishment, their SQL requirements will likely revolve around extracting student data. Knowledge about data protection and cleansing might be advantageous.
They might be looking for someone who can also manage projects and lead a team – so you can assume they want to know about your leadership background and whether you have experience in dealing with sensitive information.
There are a few things that are likely to be discussed in your interview. Give yourself a head start by ensuring that you are confident in your answers and knowledge in the following key areas:
The interviewer is likely to want to know more details about the examples and information you provided in your resume, so be prepared to expand on that.
Throughout the interview process, remember to ‘show, don’t tell’, so walk the interviewer through specific examples where you can, using the STAR method to keep the information relevant.
Be enthusiastic and engaging, work on body language and confidence, and arm yourself with some great answers to typical interview questions using your research into the company and knowledge of what they are looking for in the job description.
This is the part of the interview process where you either know it or you don’t.
The interviewer wants to be sure that your knowledge, experience and expertise in SQL matches what they are looking for, so the ‘coding test’ is a particularly important part of the interview process.
The SQL coding test is where the interviewer will be assessing your ability, knowledge and problem-solving skills to assess whether you can offer them the expertise they need for the role.
You will be required to complete some SQL exercises in one of two ways:
Computer-based SQL coding test – You will use a computer and a given data set with tasks to complete. The interviewers may be observing what you are doing and asking relevant questions, and you will have a few minutes of thinking time.
Using a whiteboard – This challenge requires you to write out the code avoiding syntax or logical errors (you won’t get any immediate feedback as you would when executing the code on a computer).
Both challenges may involve more than one query, more than one table or require you to run subqueries to get the required result.
Whichever way you are required to produce the statement you need, working through logically is the best way to get the right result-set.
Throughout the SQL coding test, be prepared to explain the operators, clauses and queries that you are writing in a way that is simple to understand, even for those without coding experience.
This is an important part of the test for the recruiter because they want to know that you understand the process and know the wider implications of the queries, rather than just knowing the right query to use.
For the coding test, the best thing you can do is ensure that you know your statements and queries. SQL tests are not something that you can do on the fly; they require specific clauses, operators and syntax. Completing a query demands knowledge about the relationship between tables, columns, rows and fields.
Practicing specific questions may help you understand the logic, but there are no guarantees what questions will be asked on the test you take.
A sensible way to ensure you have the best chance to make a good impression is to practice the most used queries and basic statements in test conditions.
You can find coding challenges on the internet that range from basic skills to more complex queries.
It is important to ensure your basic SQL knowledge is up to date. If you want to brush up on the basics and need to remember the logic behind each clause, the tutorial at W3 Schools is perfect.
Go through the whole tutorial and answer each practice question as you go or select a specific section to brush up on.
If you are confident in your theoretical knowledge but need more practical experience, HackerRank has literally hundreds of challenges rated from easy to hard that utilize different queries and subqueries.
Practice is essential even if you are from an SQL background. A new role may involve new datasets or even a new server, so don’t skip the opportunity to improve your skills and knowledge just because you are confident in what you already do.
The SQL coding test is an example of a challenge where you either know it or you don’t. Give yourself the best chance with as much practice as you can.
You have two tables: customers and orders. Write a query that selects the total sales per customer.
SELECT customer.name, SUM(sales.amount) FROM orders GROUP BY customer.name
This is a straightforward query that could trip you up because of the information given.
For these questions, you might get to see the first few rows of the tables to get an idea of what they look like and what format the column names are written in. If not, you can assume if necessary, but make sure that you follow the logic in the question where you can.
This question introduces the idea that there are two tables, but the information you need to provide is only from the table that is labeled ‘orders’.
Consider the following slow query:
SELECT * from clicks where date=”2020-01-01” GROUP BY ip_address
How would you go about optimizing this query?
From the context of the question, you can assume that the database itself contains a lot of information and that the result-set is likely to be slow.
For a smaller dataset, you can place an index on dates; for larger datasets, it is better to partition the data based on dates. This makes each partition available for separate computation, improving the time taken on a slow query as above.
What is database normalization? Under what circumstances might you choose to use an unnormalized schema?
Database normalization is a process that should be used when creating databases to reduce data redundancy.
Data redundancy can affect the consistency of the data and take up more space on the disk, as well as producing problems for DELETE, UPDATE and INSERT clauses.
Data normalization relies heavily on Normal Forms, and at least the first three. This means that the database contains rows and columns that are not ordered, have unique data in every row and column intersection, and there are no hidden values. The values in different columns depend on the whole primary key.
An unnormalized schema might be used when you are looking to improve performance and make the table easier to read – with the expense of losing some write ability. This is a reverse-engineering process to make it quicker to retrieve information and it always starts with a normalized database.
What is ACID compliance? When might you not want an ACID compliant system?
ACID is the presence of four components that ensure your database transactions can be completed quickly and without errors.
The four components are:
- Atomicity – Each part of the database transaction can be broken into smaller parts, and each part needs to work to complete the transaction
- Consistency – All data needs to be consistent to be in the database
- Isolation – Multiple transactions can happen at the same time without affecting other transactions
- Durability – Data saves make sure that your database is safe even if there is a system failure or a power outage
There are non-ACID compliant systems like NoSQL which can store other data and is popular in agile development and used by companies like Facebook and Twitter for speed – this doesn’t mean that their databases are not ‘safe’, rather that they use other methods of providing data integrity.
Using non-ACID compliant systems is fine if only one user is updating the data or there isn’t any order that transactions need to be completed in.
When would you use a NoSQL database?
A NoSQL database allows more flexibility than typical relational databases, allowing storage of different kinds of data.
Using NoSQL is preferred when you don’t know the schema ahead of time, or there is a need to be flexible in the way data is stored and retrieved to be transformed into an application (like with Facebook and Twitter user information).
Most enterprise solutions use NoSQL because the architecture can be scaled up to handle large volumes of data at high speed.
SQL and the ability to access and manipulate databases is a skill that is in wide use throughout the technology world. Both entry-level and expert roles are available in a range of different industries.
SQL interviews are generally a combination of normal interview processes and an SQL coding test.
Make sure you practice the SQL basics and prepare to answer questions about your previous experience, resume and skills. This will give you the best chance of performing well and getting the job.