This question was asked in Facebook interview, Ask is to provide Running Total for Different Genders.
Source: LeetCode
Difficulty: Medium
Company: Facebook
Q. Write an SQL query to find the total score for each gender at each day. Order the result table by gender and day.
SELECT *,
SUM(score_points) over(Partition By gender Order by gender, day) as running_total
FROM PlayerScores;
The solution for this is pretty straightforward. We will add another column to calculate the running total of scores.
This will be done using the SUM
function with a WINDOW
clause. We’ll partition the data by gender and order it by gender, day.
Essentially, we are using a window function here, as it allows us to group (partition) by gender and calculate the running total while maintaining the interval order.
Ask is to calculate the running total for the amount column.
SELECT *,
SUM(amount) over(order by product_id rows between unbounded preceding and current row) as running_total
FROM products;
Here by using rows between unbounded preceding and current row
SQL is notified to do not consider the 2 value and same. Treat them individual and generate a running total.
I hope this explanation was helpful to you, considering sharing it with your friends, thank you.
Read more
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Python Practice Questions & Solutions Day 5 of Learning Python for Data Science Welcome back…
Day 5 of Learning Python for Data Science: Data Types, Typecasting, Indexing, and Slicing Understanding…
Python Practice Questions & Solutions Day 4 of Learning Python for Data Science Welcome back…
Day 4 of Learning Python for Data Science Day 4 of Learning Python for Data…
Test your Python skills with these 20 practice questions and solutions from Day 3 of…
Understanding Python’s conditional statements is essential for controlling the flow of a program. Today, we…