SQL Interview Questions
SQL Interview Questions
Invented in 1974, SQL is a programming language designed to interface with Relational Database
Management Systems (RDBMS). An RDBMS structures data into tables made up of rows (called
“records”) and columns (called “fields”), which can then be queried, combined, and manipulated in
various ways to achieve the desired outcome.
MySQL, Microsoft SQL Server, Oracle, Amazon Redshift, and IBM DB2 are all examples of RDBMS-
based software. The chances that you will encounter one (or more) of these technologies in the course of
your career or interview is pretty high. So, let’s make sure you’re prepared.
Subsets of SQL
There are three different subsets of SQL:
Data Definition Language (or DDL), which allows you to CREATE, ALTER, or DROP tables entirely
Data Manipulation Language (or DML), which allows you to SELECT or UPDATE existing data or
INSERT new data into an existing table
Data Control Language (or DCL), which allows you to GRANT or REVOKE access to a database
The most commonly used subset of SQL is Data Manipulation Language. The most commonly used
command in SQL is SELECT. For the most part, unless you are a data engineer, you will
use SELECT to make your queries and perform basic manipulations on a database without having to
worry about the actual creation of a database or the task of managing permissions.
It’s important to familiarize yourself with these beginner querying clauses and understand when it’s
appropriate to use them in order to structure your queries to return useful results.
Solution: Both WHERE and HAVING filter a table to meet the conditions you set. The difference
between the two is shown when they are used in conjunction with the GROUP BY clause. The WHERE
clause is used to filter rows before grouping (before the GROUP BY clause), and HAVING is used to
filter rows after grouping.
Common Operators
SQL also contains three types of basic operators that allow you to further narrow down your queries:
arithmetic operators, comparison operators, and logical operators. Arithmetic operators should be familiar
to you if you have basic knowledge of mathematics.
+ (for addition)
- (for subtraction)
* (for multiplication)
/ (for division)
= (equal to)
!= (not equal)
<> (not equal)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
!< (not less than)
!> (not greater than)
Logical operators can be a bit trickier to get a handle on, but SQL is prized for its intuitive syntax, which
is closer to ordinary speech compared to other programming languages. Logical operators include:
ISNULL
ALL
ANY
AND
BETWEEN
NOT
LIKE
IN
UNIQUE
Since these operators are highly contextual, it’s difficult to explain them in abstract terms, but if you
familiarize yourself with all of them, you’ll be on solid footing when it comes to the basics of SQL in
your next interview.
Given the above table, how would you structure a query to return all users who visited the site on or
before January 15th?
Set Operators
Sometimes, it is useful to be able to compare the results of two separate SELECT qu
queries,
eries, often across
different tables. In these cases, SQL has built
built-in
in set operators that help us compare the results of our
queries with as little effort as possible. These set operators include:
It’s important to familiarize yourself with these operators, but even more important to develop a sense of
which operator is the appropriate solution for a given situation.
Q3. What is the difference between UNION and UNION ALL? When might you prefer using one
over the other?
Solution: UNION and UNION ALL are SQL operators used to concatenate two or more result sets. This
allows us to write multiple SELECT statements, retrieve the desired results, then combine them together
into a final, unified set.
Types of Joins
Often, rather than querying two separate tables with a set operator, we’ll want to actually merge the two
tables into one for ease of comparison. In this case, we’ll find ourselves performing a JOIN. There are
four commonly used JOINs in SQL:
INNER JOIN (which returns records with matching values from both tables)
LEFT JOIN (which returns all the records of the first table and any matching records from the second
table)
RIGHT JOIN (which returns all the records from the second table and any matching records from the
first table; the “opposite” of a LEFT JOIN)
FULL JOIN (which returns all records with matching values from either the first or the second table)
It’s important to be able to know, when looking at two different tables, what sort of data becomes
available when you perform a given JOIN. Developing a sense of when each JOIN is appropriate and
what sort of analysis each makes available will help you when using your SQL toolkit in a real-world
setting.
Sample SQL Interview Question:
Q4. Write a SQL query to select the 2nd highest salary in the engineering department. If more than
one person shares the highest salary, the query should select the next highest salary.
Here’s a hint:
The department_id field in the employees’ table is associated with the id field in the department’s table.
We call the department_id a foreign key beca
because
use it is a column that references the primary key of another
table, which in this case is the id field in the department’s table.
Based on this common field, we can join both tables, using INNER JOIN to associate the name of the
department name to the employees
loyees that are a part of those departments.
Aggregate Functions
Sometimes, rather than retrieving an individual record or set of records, you’ll be asked to retrieve
specific features of a table’s given field. In this case, you’ll want to apply one of SQL
SQL’s
’s built-in
built aggregate
functions. They’re called aggregate functions because they deal with data in “aggregate,” or as a group,
rather than individually. Examples of aggregate functions include:
Being able to recognize situations where aggregate functions can help you narrow down a query is a
useful skill that comes with time and practice.
1. Write a query to compute a metric to measure the quality of the search results for each query.
2. You want to be able to compute a metric that measures the precision of the ranking system based
on position. For example, if the results for dog and cat ar
are…
Here are
re some other examples of easy SQL Interview Questions:
Q6. Given the above table, what command would you use to add a new record to a database?
Q7. Given the above table, how would you structure a query to retrieve all of the entries from a
single field?
Q8. Given the above table, what command would you use to eliminate the existing table?
Q9. Given the above table, how would you structure a query to return only users that joined on
January 1st?
Q10. Given the above table, how would you structure a query to return the last date any user joined
the website?
Q11. Given the above table, how would you structure a query to return all users who visited after a
certain date AND spent more than 100 dollars?
Q12. Given the above table, how would you structure a query to return all users who spent exactly
15 dollars on the website?
Q13. Given the above table, how would you structure a query to determine the total amount spent
by all visitors to your website?
Q14.
4. Given the above table, how would you structure a query to determine the highest amount
spent by any individual visitor to your website? The lowest amount?
Q17. Given the above tables, how would you structure a query to return the list of names of people
who are both users and subscribers?
Q18. Given the above tables, how would you structure a query to return matching records from the
users’ table while preserving ALL the records from the subscribers’ table?
Q19. Given the above tables, how would you structure a query to return records with matching
values from either table?