logo

20 Advanced SQL Interview Questions (With Answers!)

Updated on

March 25, 2024

Looking to ace your next SQL interview with confidence? Test yourself with these 20 advanced SQL interview questions, each accompanied by detailed answers to help you impress recruiters and land that dream job. These questions cover everything from complex queries to data manipulation and optimization techniques, and equip you with the knowledge you need to excel in any SQL interview scenario.

How many questions can you solve?

Advanced SQL Interview Questions

Advanced SQL Interview Questions

Interview questions that test practical and conceptual SQL skills are to test the candidate's ability to translate knowledge into effective solutions. The following questions are 20 SQL interview questions pulled from REAL interviews, that helped managers assess candidates' SQL skills.

While we have included sample answers for each question, remember that these are not the only correct answers. There are often multiple ways to achieve the same goal. Focus on writing clean and functional code.

1. Repeated Payments [Stripe SQL Interview Question]

Stripe asked this tricky SQL interview question, about identifying any payments made at the same merchant with the same credit card for the same amount within 10 minutes of each other and reporting the count of such repeated payments.

Input/Output Data

Example Input:

transaction_idmerchant_idcredit_card_idamounttransaction_timestamp
1101110009/25/2022 12:00:00
2101110009/25/2022 12:08:00
3101110009/25/2022 12:28:00
4102230009/25/2022 12:00:00
6102240009/25/2022 14:00:00

Example Output:

payment_count
1

Solution:


Stripe SQL Interview Questions

You can solve this Stripe SQL problem interactively and get a full solution explanation:

Stripe SQL Interview Question

2. Describe how recursive queries work in SQL. Provide an example scenario where a recursive query would be beneficial.

Recursive queries in SQL enable iteration over hierarchical data structures like organizational charts, bill-of-materials, or nested comments. For example, you could use a recursive query to traverse a hierarchical data structure and retrieve all descendants of a specific node.

3. Median Google Search Frequency [Google SQL Interview Question]

Google’s Marketing Team needed to add a simple statistic to their upcoming Superbowl Ad: the median number of searches made per year. You were given a summary table that tells you the number of searches made last year, write a query to report the median searches made per user.

Table:

Column NameType
searchesinteger
num_usersinteger

Input/Output Data:

Example Input:

searchesnum_users
12
22
33
41

Example Output:

median
2.5

Solution:


You can solve this Google SQL problem interactively, and get the full solution explanation.

Enjoyed this one? p.s. here's more Google SQL Interview Questions to practice!

4. What are ACID properties in SQL?

ACID properties are:

  • Atomicity: Changes in the data must be like a single operation.
  • Consistency: The data must be consistent before and after the transaction.
  • Isolation: Multiple transactions can be done without any hindrance.
  • Durability: Transaction gets successful in case of system failures.

ACID Properties

5. Monthly Merchant Balance [Visa SQL Interview Question]

Say you have access to all the transactions for a given merchant account. Write a query to print the cumulative balance of the merchant account at the end of each day, with the total balance reset back to zero at the end of the month. Output the transaction date and cumulative balance.

Table:

Column NameType
transaction_idinteger
typestring ('deposit', 'withdrawal')
amountdecimal
transaction_datetimestamp

Input/Output Data:

Example Input:

transaction_idtypeamounttransaction_date
19153deposit65.9007/10/2022 10:00:00
53151deposit178.5507/08/2022 10:00:00
29776withdrawal25.9007/08/2022 10:00:00
16461withdrawal45.9907/08/2022 10:00:00
77134deposit32.6007/10/2022 10:00:00

Example Output:

transaction_datebalance
07/08/2022 12:00:00106.66
07/10/2022 12:00:00205.16

Solution:


You can solve this Visa SQL problem interactively, and get the full solution explanation.

6. Explain the concept of correlated subqueries in SQL. Can you provide an example of how you can use them?

Correlated subqueries are executed once for each row of the outer query and depend on the outer query for results. For example, you could use a correlated subquery to retrieve data from one table according to conditions in a separate table.

7. Server Utilization Time [Amazon SQL Interview Question]

Fleets of servers power Amazon Web Services (AWS). Senior management has requested data-driven solutions to optimize server usage.

Write a query that calculates the total time that the fleet of servers was running. The output should be in units of full days.

Assumptions:

  • Each server might start and stop several times.
  • The total time in which the server fleet is running can be calculated as the sum of each server's uptime.

Table:

Column NameType
server_idinteger
status_timetimestamp
session_statusstring

Input/Output Data:

Example Input:

server_idstatus_timesession_status
108/02/2022 10:00:00start
108/04/2022 10:00:00stop
208/17/2022 10:00:00start
208/24/2022 10:00:00stop

Example Output:

total_uptime_days
21

Solution:


You can solve this Amazon SQL problem interactively, and get the full solution explanation.

Looking for more Amazon SQL Interview Questions? Check out our Amazon SQL Interview Guide for access to even more questions and solutions!

8. Explain how indexing works in a relational database. What are the benefits and potential drawbacks of indexing?

Indexing in a relational database involves creating data structures (indexes) that allow for faster data retrieval by providing direct access to rows based on the indexed columns. The benefits of indexing include improved query performance and faster data retrieval, especially for large datasets. However, indexing can also introduce overhead during data modification operations (such as INSERT, UPDATE, and DELETE) and increased storage requirements.

9. Uniquely Staffed Consultants [Accenture SQL Interview Questions]

As a Data Analyst on the People Operations team at Accenture, you are tasked with understanding how many consultants are staffed to each client, and how many consultants are exclusively staffed to a single client.

Write a query that displays the outputs of client name and the number of uniquely and exclusively staffed consultants ordered by client name.

Table:

Column NameType
employee_idinteger
engagement_idinteger

Input/Output Data:

Example Input:

employee_idengagement_id
10011
10012
10021
10033
10044

Table:

Column NameType
engagement_idinteger
project_namestring
client_namestring

Example Input:

engagement_idproject_nameclient_name
1SAP Logistics ModernizationDepartment of Defense
2Oracle Cloud MigrationDepartment of Education
3Trust & Safety OperationsGoogle
4SAP IoT Cloud IntegrationGoogle

Example Output:

client_nametotal_staffedexclusive_staffed
Department of Defense21
Department of Education10
Google22

Solution:


You can solve this Accenture SQL problem interactively, and get the full solution explanation.

10. Compare and contrast stored procedures and user-defined functions in SQL. When would you choose to use one over the other?

Stored procedures and user-defined functions (UDFs) are both reusable pieces of code in SQL, but they have some key differences. Stored procedures can perform DML operations and transaction management, while UDFs are limited to returning values and cannot perform DML operations. Stored procedures are typically used for procedural tasks and complex business logic, while UDFs are often used for calculations and data transformations.

11. Event Friends Recommendation [Facebook SQL Interview Questions]

Facebook wants to recommend new friends to people who show interest in attending 2 or more of the same private events.

Sort your results in order of user_a_id and user_b_id (refer to the Example Output below). Table:

Column NameType
user_a_idinteger
user_b_idinteger
statusenum ('friends', 'not_friends')

Each row of this table indicates the status of the friendship between user_a_id and user_b_id.

Input/Output Data:

Example Input:

user_a_iduser_b_idstatus
111333not_friends
222333not_friends
333222not_friends
222111friends
111222friends
333111not_friends

Table:

Column NameType
user_idinteger
event_idinteger
event_typeenum ('public', 'private')
attendance_statusenum ('going', 'maybe', 'not_going')
event_datedate

Example Input:

user_idevent_idevent_typeattendance_statusevent_date
111567publicgoing07/12/2022
222789privategoing07/15/2022
333789privatemaybe07/15/2022
111234privatenot_going07/18/2022
222234privategoing07/18/2022
333234privategoing07/18/2022

Example Output:

user_a_iduser_b_id
222333
333222

Users 222 and 333 who are not friends have shown interest in attending 2 or more of the same private events.

Solution:


Facebook SQL Interview Questions

You can solve this Facebook SQL problem interactively, and get the full solution explanation.

Looking for more Facebook SQL Interview Questions? Check out our Facebook SQL Interview Guide for access to even more questions and solutions!

12. Explain the purpose of the MERGE statement in SQL. Provide an example.

The MERGE statement performs INSERT, UPDATE, or DELETE operations on a target table based on the results of a join with a source table.

Example:


13. 3-Topping Pizzas [McKinsey SQL Interview Question]

You’re a consultant for a major pizza chain that will be running a promotion where all 3-topping pizzas will be sold for a fixed price, and are trying to understand the costs involved.

Given a list of pizza toppings, consider all the possible 3-topping pizzas, and print out the total cost of those 3 toppings. Sort the results with the highest total cost on the top followed by pizza toppings in ascending order.

Break ties by listing the ingredients in alphabetical order, starting from the first ingredient, followed by the second and third.

** Table:**

Column NameType
topping_namevarchar(255)
ingredient_costdecimal(10,2)

Input/Output Data:

Example Input:

topping_nameingredient_cost
Pepperoni0.50
Sausage0.70
Chicken0.55
Extra Cheese0.40

Example Output:

pizzatotal_cost
Chicken,Pepperoni,Sausage1.75
Chicken,Extra Cheese,Sausage1.65
Extra Cheese,Pepperoni,Sausage1.60
Chicken,Extra Cheese,Pepperoni1.45

Solution:


McKinsey SQL Interview Questions

You can solve this McKinsey SQL problem interactively AND see alternate solutions, and get the full solution explanation.

14. Describe the differences between clustered and non-clustered indexes in SQL.

A clustered index determines the physical order of data in a table, while a non-clustered index does not. A table can have only one clustered index but multiple non-clustered indexes.

15. Follow-Up Airpod Percentage [Apple SQL Interview Questions]

The Apple retention team needs your help to investigate buying patterns. Write a query to determine the percentage of buyers who bought AirPods directly after they bought iPhones. Round your answer to a percentage (i.e. 20 for 20%, 50 for 50) with no decimals.

Table:

Column NameType
transaction_idinteger
customer_idinteger
product_namevarchar
transaction_timestampdatetime

Input/Output Data:

Example Input:

transaction_idcustomer_idproduct_nametransaction_timestamp
1101iPhone08/08/2022 00:00:00
2101AirPods08/08/2022 00:00:00
5301iPhone09/05/2022 00:00:00
6301iPad09/06/2022 00:00:00
7301AirPods09/07/2022 00:00:00

Example Output:

follow_up_percentage
50

Solution:


You can solve this Apple SQL problem interactively, and get the full solution explanation.

Looking for more Apple SQL Interview Questions? Check out our Apple SQL Interview Guide for access to even more questions and solutions!

16. What is the purpose of the LAG and LEAD functions in SQL? Provide an example.

The LAG and LEAD functions allow accessing data from previous or subsequent rows in a result set.

Example:


17. Marketing Touch Streak [Snowflake SQL Interview Question]

As a Data Analyst on Snowflake's Marketing Analytics team, your objective is to analyze customer relationship management (CRM) data and identify contacts that satisfy two conditions:

  1. Contacts who had a marketing touch for three or more consecutive weeks.
  2. Contacts who had at least one marketing touch of the type 'trial_request'.

Marketing touches, also known as touch points, represent the interactions or points of contact between a brand and its customers.

Your goal is to generate a list of email addresses for these contacts.

Table:

Column NameType
event_idinteger
contact_idinteger
event_typestring ('webinar', 'conference_registration', 'trial_request')
event_datedate

Input/Output Data:

Example Input:

event_idcontact_idevent_typeevent_date
11webinar4/17/2022
21trial_request4/23/2022
31whitepaper_download4/30/2022
42handson_lab4/19/2022
52trial_request4/23/2022
62conference_registration4/24/2022
73whitepaper_download4/30/2022
84trial_request4/30/2022
94webinar5/14/2022

Table:

Column NameType
contact_idinteger
emailstring

Example Input:

Example Output:

Solution:


You can solve this Snowflake SQL problem interactively, and get the full solution explanation.

Looking for more Snowflake SQL Interview Questions? Check out these 10 Snowflake SQL Interview Questions for access to even more questions and solutions!

18. Explain the purpose of the TRUNCATE statement in SQL. How is it different from DELETE?

The TRUNCATE statement removes all rows from a table but does not log individual row deletions, making it faster than DELETE, which removes rows one by one and logs each deletion. However, TRUNCATE cannot be rolled back, and it resets identity columns.

19. Bad Delivery Rate [DoorDash SQL Interview Question]

The Growth Team at DoorDash wants to ensure that new users, who make orders within their first 14 days on the platform, have a positive experience. However, they have noticed several issues with deliveries that result in a bad experience.

These issues include:

  • Orders being completed incorrectly, with missing items or wrong orders.
  • Orders not being received due to incorrect addresses or drop-off spots.
  • Orders being delivered late, with the actual delivery time being 30 minutes later than the order placement time. Note that the is automatically set to 30 minutes after the .

Write a query that calculates the bad experience rate for new users who signed up in June 2022 during their first 14 days on the platform. The output should include the percentage of bad experiences, rounded to 2 decimal places. Table:

Column NameType
order_idinteger
customer_idinteger
trip_idinteger
statusstring ('completed successfully', 'completed incorrectly', 'never received')
order_timestamptimestamp

Input/Output Data:

Example Input:

order_idcustomer_idtrip_idstatusorder_timestamp
7274248472100463completed successfully06/05/2022 09:12:00
2425132341100482completed incorrectly06/05/2022 14:40:00
1413671314100362completed incorrectly06/07/2022 15:03:00
5821935421100657never_received07/07/2022 15:22:00
2536131314100213completed successfully06/12/2022 13:43:00

Table:

Column NameType
dasher_idinteger
trip_idinteger
estimated_delivery_timestamptimestamp
actual_delivery_timestamptimestamp

Example Input:

dasher_idtrip_idestimated_delivery_timestampactual_delivery_timestamp
10110046306/05/2022 09:42:0006/05/2022 09:38:00
10210048206/05/2022 15:10:0006/05/2022 15:46:00
10110036206/07/2022 15:33:0006/07/2022 16:45:00
10210065707/07/2022 15:52:00-
10310021306/12/2022 14:13:0006/12/2022 14:10:00

Table:

Column NameType
customer_idinteger
signup_timestamptimestamp

Example Input:

customer_idsignup_timestamp
847205/30/2022 00:00:00
234106/01/2022 00:00:00
131406/03/2022 00:00:00
143506/05/2022 00:00:00
542106/07/2022 00:00:00

Example Output:

bad_experience_pct
75.00

Solution:


You can solve this DoorDash SQL problem interactively, and get the full solution explanation.

Looking for more DoorDash SQL Interview Questions? Check out these 8 DoorDash SQL Interview Questions for access to even more questions and solutions!

20. Describe the difference between a view and a materialized view in SQL.

A view is a virtual table based on the result of a SELECT query, while a materialized view is a physical copy of the result set of a query that is stored and updated periodically.

SQL Interview Tips

The best way to prepare for the SQL interview is to practice, practice, practice. Besides solving the earlier SQL interview questions, you should also solve the 200+ SQL coding questions which come from companies like FAANG tech companies and tech startups.

DataLemur Questions

Each exercise has hints to guide you, step-by-step solutions and best of all, there is an interactive coding environment so you can right online code up your SQL query and have it checked. However, if your SQL skills are weak, forget about going right into solving questions – refresh your SQL knowledge with this DataLemur SQL Tutorial.

SQL tutorial for Data Scientists & Analysts

Data Science Interview Tips

To prepare for the Data Science interviews read the book Ace the Data Science Interview because it's got:

  • 201 Interview Questions from Facebook, Google & startups
  • A Crash Course covering Product Analytics, SQL & ML
  • Amazing Reviews (900+ 5-star reviews on Amazon)

Ace the Data Science Interview Book on Amazon