80 Top SQL Interview Questions and Answers (2024) - DataCamp
80 Top SQL Interview Questions and Answers (2024) - DataCamp
B LO G S Category
Elena Kosourova
TOPICS
SQL
Data Analysis
Whether you're a job hunter who is looking for a new opportunity to apply your SQL skills or a
hiring manager who is going to interrogate a candidate for a job opening in their company,
knowing common SQL interview questions and answers to them is a must for you.
In this article, we're going to take a look at 80 essential SQL questions and answers for beginners
and intermediate practitioners that will help you better prepare for the interview and know what to
expect from your interviewer/interviewee.
Note that for the sake of convenience, this article is mostly addressing job searchers since they
are the main audience for such information. However, this content will definitely be of use also for
hiring managers/recruiters, especially for conducting their first SQL interviews.
For beginners
1. General questions. Expect questions about your experience, the SQL flavors you're familiar
with, and your level of proficiency.
2. Technical questions. These will cover the basics of SQL, such as what SQL is, its applications,
SQL statements, SQL commands, and types of SQL queries, among others.
2. Advanced commands. Questions may cover topics like joins, primary and foreign keys,
indexes, and SQL relationships.
4. Advanced queries. You may be asked about subqueries, both nested and correlated, as well
as how to perform specific tasks like finding the nth highest value in a column.
While this information can be mentioned in your resume, be ready to talk about it. Naturally, there
are no "right" answers to such questions, and there is no need to make up things when answering
them.
Don't worry if your experience in SQL is limited: this is something your interviewer, most probably,
already knows from your resume. Since they are interested in talking to you anyway, your profile
was considered a good fit for their company.
Also, it's perfectly fine if you have only worked with one SQL flavor. Remember that all SQL dialects
are fairly similar among themselves. Therefore, being familiar with only one of them is a solid
basis for you to learn any others.
When answering technical questions, the best strategy is to give as precise answers as possible.
It may look like an attempt to deviate from the main topic. In addition, it may provocate additional
questions about which you can feel less confident.
1. What is SQL?
It stands for Structured Query Language. A programming language used for interaction with
relational database management systems (RDBMS). This includes fetching, updating, inserting,
and removing data from tables.
retrieve and summarize the necessary information from a table or several tables
All in all, SQL allows querying a database in multiple ways. In addition, SQL easily integrates with
other programming languages, such as Python or R, so we can use their combined power.
Data Manipulation Language (DML) – to access, manipulate, and modify data in a database.
Data Control Language (DCL) – to control user access to the data in the database and give or
revoke privileges to a specific user or a group of users.
Data Query Language (DQL) – to perform queries on the data in a database to retrieve the
necessary information from it.
DQL: – SELECT
7. What is a database?
A structured storage space where the data is kept in many tables and organized so that the
necessary information can be easily fetched, manipulated, and summarized.
11. What is an SQL query, and what types of queries do you know?
A query is a piece of code written in SQL to access the data from a database or to modify the
data. Correspondingly, there are two types of SQL queries: select and action queries. The first
ones are used to retrieve the necessary data (this also includes limiting, grouping, ordering the
data, extracting the data from multiple tables, etc.), while the second ones are used to create, add,
delete, update, rename the data, etc.
FOREIGN KEY – provides shared keys between two and more tables.
LEFT (OUTER) JOIN – returns all records from the left table and those records from the
right table that satisfy a defined join condition.
RIGHT (OUTER) JOIN – returns all records from the right table and those records from
the left table that satisfy a defined join condition.
FULL (OUTER) JOIN – returns all records from both (or all) tables. It can be considered as
a combination of left and right joins.
Clustered index – defines the physical order of records of a database table and performs
data searching based on the key values. A table can have only one clustered index.
Non-clustered index – keeps the order of the table records that doesn't match the physical
order of the actual data on the disk. It means that the data is stored in one place and a non-
clustered index – in another one. A table can have multiple non-clustered indexes.
Compound ( += , -= , *= , /= , etc.)
String ( % , _ , + , ^ , etc.)
P O W E R E D B Y D A T A C A M P W O R K S PA C E
29. What are some common statements used with the SELECT query?
The most common ones are FROM , GROUP BY , JOIN , WHERE , ORDER BY , LIMIT , and
HAVING .
P O W E R E D B Y D A T A C A M P W O R K S PA C E
UPDATE table_name
SET col_1 = value_1, column_2 = value_2
WHERE condition;
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
We can specify that we need a descending order using the DESC keyword; otherwise, the order
will be ascending by default. Also, we can sort by more than one column and specify for each one,
ascending or descending order separately. For example:
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
37. What is the DISTINCT statement and how do you use it?
This statement is used with the SELECT statement to filter out duplicates and return only unique
values from a column of a table. The syntax is:
P O W E R E D B Y D A T A C A M P W O R K S PA C E
40. What is NULL value? How is it different from zero or a blank space?
A NULL value indicates the absence of data for a certain cell of a table. Instead, zero is a valid
numeric value, and an empty string is a legal string of zero length.
Scalar functions – work on each individual value and return a single value.
On the other hand, SQL functions can be built-in (defined by the system) or user-defined (created
by the user for their specific needs).
COUNT() – returns the number of rows, including those with null values
UCASE() (in other SQL flavors – UPPER() ) – returns a string converted to the upper case
LCASE() (in other SQL flavors – LOWER() ) – returns a string converted to the lower case
INITCAP( ) – returns a string converted to the title case (i.e., each word of the string starts
from a capital letter)
MID() (in other SQL flavors – SUBSTR() ) – extracts a substring from a string
UCASE() (in other SQL flavors – UPPER( )) – returns a string converted to the upper case
LCASE() (in other SQL flavors – LOWER() ) – returns a string converted to the lower case
INITCAP() – returns a string converted to the title case (i.e., each word of the string starts
from a capital letter)
CONCAT() – joins two or more string values appending the second string to the end of the
first one
SUBSTR() – returns a part of a string satisfying the provided start and end points
LENGTH() (in other SQL flavors – LEN() ) – returns the length of a string, including the
blank spaces
LPAD() and RPAD() – return the padding of the left-side/right-side character for right-
justified/left-justified value
TRIM() – removes all the defined characters, as well as white spaces, from the left, right, or
both ends of a provided string
8. What is the default data ordering with the ORDER BY statement, and how
do you change it?
By default, the order is ascending. To change it to descending, we need to add the DESC keyword
as follows:
UNION ALL – returns the records obtained by at least one of two queries (including
duplicates)
EXCEPT (called MINUS in MySQL and Oracle) – returns only the records obtained by the
first query but not the second one
11. What is the difference between a primary key and a unique key?
While both types of keys ensure unique values in a column of a table, the first one identifies
uniquely each record of the table, and the second one prevents duplicates in that column.
14. In which order the interpreter executes the common statements in the
SELECT query?
FROM – JOIN – ON – WHERE – GROUP BY – HAVING – SELECT – ORDER BY – LIMIT
One-to-many – each record in one table corresponds to several records in another table
Many-to-many – each record in both tables corresponds to several records in another table
22. What is the difference between renaming a column and giving an alias to
it?
Renaming a column means permanently changing its actual name in the original table. Giving an
alias to a column means giving it a temporary name while executing an SQL query, with the
purpose to make the code more readable and compact.
CASE
WHEN condition_1 THEN value_1
WHEN condition_2 THEN value_2
WHEN condition_3 THEN value_3
...
ELSE value
END;
P O W E R E D B Y D A T A C A M P W O R K S PA C E
26. What is the difference between the DELETE and TRUNCATE statements?
DELETE is a reversible DML (Data Manipulation Language) command used to delete one or more
rows from a table based on the conditions specified in the WHERE clause. Instead, TRUNCATE
is an irreversible DDL (Data Definition Language) command used to delete all rows from a table.
DELETE works slower than TRUNCATE . Also, we can't use the TRUNCATE statement for a table
containing a foreign key.
27. What is the difference between the DROP and TRUNCATE statements?
DROP deletes a table from the database completely, including the table structure and all the
associated constraints, relationships with other tables, and access privileges. TRUNCATE deletes
all rows from a table without affecting the table structure and constraints. DROP works slower
than TRUNCATE . Both are irreversible DDL (Data Definition Language) commands.
28. What is the difference between the HAVING and WHERE statements?
The first one works on aggregated data after they are grouped, while the second one checks each
row individually. If both statements are present in a query, they appear in the following order:
WHERE – GROUP BY – HAVING . The SQL engine interprets them also in the same order.
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
In this way, we can also delete multiple records if they satisfy the provided condition.
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
To select all odd records, the syntax is identical in both cases, only that we would use the
inequality operator <> instead of = .
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
38. How to find the values in a text column of a table that start with a certain
letter?
Using the LIKE operator in combination with the % and _ wildcards. For example, we need to
find all surnames in a table that start with "A". The query is:
P O W E R E D B Y D A T A C A M P W O R K S PA C E
Here, we assume that a surname must contain at least two letters. Without this assumption
(meaning that a surname can be just A), the query is as follows:
SELECT id
FROM table_name
ORDER BY id DESC
LIMIT 1;
P O W E R E D B Y D A T A C A M P W O R K S PA C E
SELECT TOP 1 id
FROM table_name
ORDER BY id DESC
P O W E R E D B Y D A T A C A M P W O R K S PA C E
P O W E R E D B Y D A T A C A M P W O R K S PA C E
Conclusion
To sum up, we discussed the 80 essential beginner and intermediate SQL interview questions and
the right answers to them. Hopefully, this information will help you to get ready for the interview
and feel more confident, whether you're looking for a job in SQL or hiring candidates for an
intermediate SQL position.
If you feel that you need more training to better prepare for an interview, consider the following
SQL courses and tracks of DataCamp:
Introduction to SQL
SQL Foundations
Intermediate SQL
FAQs
How important is it to learn specific SQL dialects, and which ones should I
focus on?
TOPICS
Related
10 Top Data Analytics
Conferences for 2024
Matt Crabtree
Matt Crabtree
See More
Learn Python
Learn R
Learn AI
Learn SQL
Learn Power BI
Learn Tableau
Assessments
Career Tracks
Skill Tracks
Courses
D ATA C O U R S E S
Python Courses
R Courses
SQL Courses
Power BI Courses
Tableau Courses
Azure Courses
Spreadsheets Courses
AI Courses
W O R K S PA C E
Get Started
Templates
Integrations
Documentation
C E R T I F I C AT I O N
Certifications
Data Scientist
Data Analyst
Data Engineer
RESOURCES
Resource Center
Upcoming Events
Blog
Code-Alongs
Tutorials
Open Source
RDocumentation
Course Editor
Data Portfolio
Portfolio Leaderboard
PLANS
Pricing
For Business
For Universities
DataCamp Donates
S U P P O RT
Help Center
Become an Affiliate
ABOUT
About Us
Learner Stories
Careers
Become an Instructor
Press
Leadership
Contact Us
DataCamp Español
Privacy Policy Cookie Notice Do Not Sell My Personal Information Accessibility Security
Terms of Use