SQL Coding Tasks-Net Boss-Sample
SQL Coding Tasks-Net Boss-Sample
TOP 30
SQL
INTERVIEW
CODING TASKS
WITH WINNING SOLUTIONS
Matthew Urban
TOP 30 SQL Interview Coding Tasks with Winning Solutions
by Matthew Urban
Although we have taken every care to ensure that the information contained
in this book is accurate, the publisher and author disclaim all responsibility
for errors or omissions. If you find a mistake in the text or the code, we would
be grateful if you would report it by visiting javafaq.io. By doing so, you can
help us improve next editions of this book.
ISBN 978-83-65477-14-9
Top 30 SQL Interview Coding Tasks With Winning Solutions
Preface
Matthew Urban
IT specialist, Senior Java developer
5
Matthew Urban
Data-set
From the perspective of a recruiter, it is easier to use the same data-set during
each job interview as is the case with this book. For simplicity, the database
schema of a small e-commerce system is used. It contains the following ta-
bles: customers, products, suppliers, orders and order_items. Each cod-
ing task described in this book is based on the data-set given below.
customers
ID FIRST_NAME LAST_NAME EMAIL PHONE COUNTRY
products
ID NAME AVAILABLE PRICE SUPPLIER
6
Top 30 SQL Interview Coding Tasks With Winning Solutions
suppliers
ID COMPANY_NAME CONTACT_NAME ADDRESS COUNTRY EMAIL
1 2014-11-25 2014-12-02 3
2 2016-12-02 2016-12-09 3
3 2017-02-10 2017-02-18 1
4 2018-03-10 2018-03-19 4
5 2019-09-20 2019-09-30 4
order_items
1 2 1 780 0
1 3 1 50 0
2 3 2 50 50
3 5 1 30 30
7
Matthew Urban
The easiest way to verify if a person knows the basics of SQL is to ask them to
retrieve data from a database.
Solution
Given three tables: customers, products and orders you need to create
queries which retrieve all rows from them. Listing 1.1 presents simple SELECT
statements.
Please notice that the wildcard (*) causes all columns to be retrieved. In many
cases, it is necessary to retrieve only part of the data. Listing 1.2 presents
an example SELECT statement which retrieves only the first and last name of
a customer.
8
Top 30 SQL Interview Coding Tasks With Winning Solutions
WHERE
First, you are asked to prepare a query which returns all customers which are
from the USA.
OR
Second, you are asked to prepare a query which returns all customers which
are from the USA or Canada.
AND
Finally, the last most basic operator. You need to prepare a query which re-
turns all products from supplier ‘Brandon’ and price lower than $20.
9
Matthew Urban
The IN operator is very often used in SELECT statements. The IN operator can
be seen as shorthand for multiple OR conditions, but it can also take the re-
sults from other SELECT queries as input.
List of values
You are asked to prepare a query which returns all customers which are from
the following list of countries:
• USA,
• Canada,
• Australia,
• Great Britain,
• New Zealand.
Listing 3.1 presents the correct implementation of such a query.
Subquery
Another way to use the IN clause is to pass a list of values by selecting data
from another table. For example, you may be asked to retrieve products
which were sold in quantities higher than 100. Listing 3.2 presents an example
of such a query. From the order_items table, you retrieve a list of products
identifiers which sold more than 100 items in one order. Next, such a list is
passed to the IN clause. Finally, the SELECT statement returns all products
which match previously selected identifiers.
10
Top 30 SQL Interview Coding Tasks With Winning Solutions
11
Matthew Urban
A list of most basic data operations contains save, update, delete and read.
Each developer needs to be aware of how to save data in the database before
he can modify, delete or read it. To put new rows into an existing table, the
SQL provides the INSERT INTO statement.
INSERT INTO
In most cases during a job interview, you are asked to save a new customer
and a new product in the database. Listing 4.1 presents a correct solution.
12
Top 30 SQL Interview Coding Tasks With Winning Solutions
The UPDATE statement is used to modify existing records in the database. The
syntax of UPDATE allows you to modify selected columns in one or more rows
at once. If you do not specify the WHERE clause, all records are going to be
updated.
UPDATE
You are usually asked to prepare three UPDATE queries: one which modifies all
records, a second which modifies only a single record and a third which mod-
ifies a subset of data. For example, you need to write a query which removes
all phone numbers of all customers. Listing 5.1 presents a solution.
Next, you need to prepare a query which modifies the name of a product with
identifier 6, as presented in Listing 5.2.
Finally, you are asked to prepare a query which modifies the shipping date of
all orders placed on 2018-09-15.
13