As a data scientist, you will often be asked to demonstrate your SQL skills in interviews. This is because SQL is a critical tool for accessing, manipulating, and analyzing data in many data science projects. To prepare for a data science interview, it is important to practice your SQL skills and be able to answer a range of questions on different topics.
In this article, I will provide a selection of data science SQL interview practice questions, along with sample data and solutions. These questions are designed to help you prepare for a data science interview and get a better understanding of how to use SQL in a data science context. We will cover a range of topics, including data manipulation, data analysis, and SQL optimization.
Use the following tables to practice writing SQL queries.
Tables
customer_id | first_name | last_name | phone | |
1 | Ahmed | Ali | ahmed_ali@example.com | 555-1234 |
2 | Fatima | Rahman | fatima.rahman@example.com | 555-5678 |
3 | Jacob | Levy | jacob_levy@example.com | 555-2468 |
4 | Malik | Jackson | malik_jackson@example.com | 555-3698 |
5 | Michaela | Gomez | michaela_gomez@example.com | 555-9876 |
6 | Omar | Rodriguez | omar_rodriguez@example.com | 555-2345 |
7 | Rachel | Cohen | rachel_cohen@example.com | 555-6789 |
8 | Sofia | Torres | sofia_torres@example.com | 555-1357 |
9 | Yusuf | Ali | yusuf_>ali@example.com | 555-4801 |
10 | Zara | Patel | zara_patel@example.com | 555-9012 |
booking_id | origin | destination | flight_date | flight_day | num_passengers | trip_type | length_days | sales_channel |
UA100006 | LAX | ORD | 3/20/2023 | Sunday | 1 | One-way | 13 | Internet |
DL100007 | LAX | JFK | 3/21/2023 | Monday | 2 | Roundtrip | 10 | Mobile |
AC100008 | YYZ | JFK | 3/22/2023 | Tuesday | 3 | Roundtrip | 7 | Travel Agent |
BA100009 | JFK | LHR | 3/23/2023 | Wednesday | 1 | One-way | 14 | Internet |
UA100010 | LHR | SFO | 3/24/2023 | Thursday | 4 | Roundtrip | 16 | Mobile |
DL100011 | JFK | LAX | 3/25/2023 | Friday | 2 | One-way | 9 | Internet |
AC100012 | LAX | LHR | 3/26/2023 | Saturday | 1 | One-way | 12 | Travel Agent |
BA100013 | LHR | JFK | 3/27/2023 | Sunday | 2 | Roundtrip | 6 | Internet |
UA100014 | JFK | SFO | 3/28/2023 | Monday | 3 | Roundtrip | 8 | Mobile |
AC100015 | YYZ | JFK | 3/29/2023 | Tuesday | 1 | One-way | 13 | Internet |
DL100016 | LAX | JFK | 3/30/2023 | Wednesday | 2 | Roundtrip | 11 | Mobile |
UA100017 | LHR | ORD | 3/31/2023 | Friday | 3 | Roundtrip | Internet | |
UA100007 | JFK | LAX | 4/5/2023 | Tuesday | 2 | Roundtrip | 14 | Mobile |
UA100008 | ORD | DFW | 5/2/2023 | Thursday | 1 | One-way | 11 | Internet |
UA100009 | DEN | MIA | 6/15/2023 | Wednesday | 2 | Round-trip | 12 | Mobile |
UA100010 | LGA | ATL | 7/1/2023 | Friday | 1 | One-way | 10 | Internet |
UA100011 | SFO | SEA | 8/19/2023 | Sunday | 1 | One-way | 8 | Mobile |
UA100012 | MSP | LAX | 9/10/2023 | Tuesday | 2 | Roundtrip | 15 | Internet |
UA100013 | DCA | BOS | 10/7/2023 | Friday | 1 | One-way | 7 | Mobile |
UA100014 | SFO | JFK | 11/15/2023 | Tuesday | 2 | Roundtrip | 16 | Internet |
UA100015 | LAX | IAH | 12/1/2023 | Thursday | 1 | One-way | 9 | Mobile |
UA100016 | MCO | PHL | 1/20/2024 | Sunday | 1 | One-way | 8 | Internet |
order_id | booking_id | customer_id | order_date | total_amount | payment_status |
1 | UA100006 | 2 | 3/5/2023 | 525 | Paid |
2 | DL100007 | 4 | 2/19/2023 | 1050 | Paid |
3 | AC100008 | 6 | 3/2/2023 | 1575 | Paid |
4 | BA100009 | 3 | 3/8/2023 | 525 | Paid |
5 | UA100010 | 8 | 3/9/2023 | 2100 | Paid |
6 | DL100011 | 5 | 2/18/2023 | 1050 | Paid |
7 | AC100012 | 1 | 2/14/2023 | 525 | Paid |
8 | BA100013 | 10 | 1/16/2023 | 1050 | Paid |
9 | UA100014 | 7 | 3/13/2023 | 1575 | Paid |
10 | AC100015 | 9 | 12/29/2022 | 525 | Paid |
11 | DL100016 | 4 | 12/30/2022 | 1050 | Paid |
12 | UA100017 | 6 | 3/11/2023 | 1575 | Paid |
13 | UA100007 | 1 | 3/11/2023 | 1050 | Paid |
14 | UA100008 | 2 | 4/25/2023 | 525 | Paid |
15 | UA100009 | 3 | 5/31/2023 | 1050 | Paid |
16 | UA100010 | 10 | 6/16/2023 | 525 | Paid |
17 | UA100011 | 10 | 8/4/2023 | 525 | Paid |
18 | UA100012 | 2 | 8/26/2023 | 1050 | Paid |
19 | UA100013 | 4 | 9/22/2023 | 525 | Paid |
20 | UA100014 | 6 | 10/31/2023 | 1050 | Paid |
21 | UA100015 | 2 | 11/16/2023 | 525 | Paid |
22 | UA100016 | 7 | 1/5/2024 | 525 | Paid |
SQL Script
Download SQL Script for Creating and Populating Tables in your own Booking Database
SQL Practice Questions
Q1: How would you retrieve the top 10 customers by total orders?
A1:
SELECT * FROM Customers WHERE Country = 'USA';Q2: Write a query to find the average order value for each customer
A2:
SELECT * FROM Customers WHERE Country = 'USA';Q3: How would you find the customers who have placed more than 4 orders?
A3:
SELECT * FROM Customers WHERE Country = 'USA';Q4: Write a query to find the most popular destination (by number of orders)
A4:
SELECT * FROM Customers WHERE Country = 'USA';Q5: Write a query to find the most popular products (by number of orders)
A5:
SELECT * FROM Customers WHERE Country = 'USA';Q6: How would you find the total number of orders for each product?
A6:
SELECT * FROM Customers WHERE Country = 'USA';Q7: Write a query to find the customers who have not placed an order in the last 6 months
A7:
SELECT * FROM Customers WHERE Country = 'USA';Q8: How would you find the customers who have placed orders in the last 6 months but not in the last 3 months?
A8:
SELECT * FROM Customers WHERE Country = 'USA';Q9: Write a query to find the total sales for each month
A9:
SELECT * FROM Customers WHERE Country = 'USA';Q10: How would you find the products that have never been ordered?
A10:
SELECT * FROM Customers WHERE Country = 'USA';Q11: Write a query to find the average order value for each product category
A11:
SELECT * FROM Customers WHERE Country = 'USA';Q12: Write a query to find the customers who have placed orders in every month of the year
A12:
SELECT * FROM Customers WHERE Country = 'USA';Q13: How would you find the total number of orders for each product category?
A13:
SELECT * FROM Customers WHERE Country = 'USA';Q14: Write a query to find the customers who have placed orders with a total value greater than $1,000
A14:
SELECT * FROM Customers WHERE Country = 'USA';Q15: How would you find the average order value for each customer, grouped by country?
A15:
SELECT * FROM Customers WHERE Country = 'USA';Q16: Write a query to find the top 5 customers by total purchases, grouped by country
A16:
SELECT * FROM Customers WHERE Country = 'USA';Q17: How would you find the products that have been ordered by at least 50 different customers?
A17:
SELECT * FROM Customers WHERE Country = 'USA';Q18: Write a query to find the average order value for each customer, for orders placed in the last 3 months
A18:
SELECT * FROM Customers WHERE Country = 'USA';Q19: How would you find the total number of orders for each product, grouped by month?
A19:
SELECT * FROM Customers WHERE Country = 'USA';Q20: Write a query to find the customers who have placed orders with a total value greater than $500, grouped by country
A20:
SELECT * FROM Customers WHERE Country = 'USA';Q21: How would you find the average order value for each product, grouped by country?
A21:
SELECT * FROM Customers WHERE Country = 'USA';Q22: List the names of all customers who have placed an order
A22:
SELECT * FROM Customers WHERE Country = 'USA';Q23: List the names of all customers who have placed more than one order
A23:
SELECT * FROM Customers WHERE Country = 'USA';Q24: List the names of all customers who have placed an order in the USA