When you're interviewing for a data scientist or data analyst role, it's highly likely you'll encounter SQL questions in your interview. Additionally, it's likely one or more of those SQL questions will require a window function to be solved. Window functions are a core concept in intermediate/advanced SQL, and mastering them will put you one step closer to landing your analytics role. In this post we'll give a quick overview of what window functions are, and then we'll dive into an example interview question along with our solution.
What are SQL window functions?
A window function defines a frame or ‘window’ of rows with a given length around the current row, and performs a calculation across the set of data in the window.
If you’re struggling with the definition above, don’t worry, it should become more clear as you put it into practice. Below we'll step through an example window function interview question that uses a simple aggregation.
Example question - RANK() in SQL window function
Suppose you're given the following tables called 'orders' and 'order_info'. The table 'orders' shows revenue values for unique orders along with the associated channel ('online' or 'in_store') while the table 'order_info' shows the order's ID along with its location.
Using these tables, write a SQL query to return the top 3 'online' orders and their associated locations based on revenue generated. You can assume that each order has a unique revenue value, but you should be able to highlight the implications of ties in revenue values and how you would handle that.
Click here to view these tables in an interactive SQL fiddle.
Click here to view this solution in an interactive SQL fiddle.
# SQL query to return the top
# 3 'online' orders and their
# associated locations based on revenue generated
# Pulling from our subquery below,
# filtering on just the top 3 records
SELECT * FROM (
# Subquery to pull online orders and
# add ranking next to each based on
# revenue generated
o.order_id as order_id,
o.revenue as revenue,
oi.location as location,
# Add a ranking in descending order,
# highest revenue first
RANK() OVER (ORDER BY sum(o.revenue) DESC) as my_rank
FROM orders o
# Join in order info (for location)
# on order_id
LEFT JOIN order_info oi
ON o.order_id = oi.order_id
# Filter where the channel is online
WHERE o.channel = "online"
group by 1,2,3) stg
WHERE my_rank <= 3
Lastly, note that if there were multiple unique order ids with the same revenue values, our rank query could return more than 3 rows, since there could be ties (e.g. multiple rows ranked '2'). So, to handle that, one could simply add an ORDER by my_rank ASC LIMIT 3 statement to the end of the query instead of the WHERE clause. Example SQL fiddle here.