A concise, no-nonsense demonstration of window functions that effectively demystifies standard technical interview patterns. It prioritizes practical logic over academic complexity, making it a solid resource for the career-focused professional.
深度探索
先修知识
- 暂无数据。
后续步骤
- 暂无数据。
深度探索
GOOGLE SQL Interview Question- FAANG SQL Interview Questions and Answers本站添加:
Hello everyone, welcome back to another video on my channel. Today we'll be solving another company based SQL interview question. Today we'll be solving a question that was asked in Google and the difficulty level is medium. Let's begin with the question.
Also, I have created a playlist where I solve company based SQL interview questions, so check it out in case you are preparing for some SQL interview.
So, let's begin. Assume you're given a table with measurement values obtained from a Google sensor over multiple days with measurements taken multiple days within each day.
Write a query to calculate the sum of odd numbered and even numbered measurements separately for a particular day and display the results in two different columns.
Refer to example output below for the desired output.
So, this is our table measurements. It has measurement ID, measurement value, and measurement time. Within a day measurements taken at first, third, and fifth are considered odd numbered measurements and measurements taken at second, fourth, and sixth times are considered even numbered measurements.
So, this is our table and these are the measurement times. For a particular day, let's say 7 10 2022.
There are three records.
9 11 13. So, the first, third, and fifth times are considered odd numbered measurements and the measurements that were taken at second or fourth or sixth time or eighth and so on are considered even numbered measurements and we have to sum it up. So, if you see this output that they have created for 7 10 2022, the odd sum is 2355.75, which means let's say 7 10 2022, this one.
For odd, which means one and three. So, 1109.51 + 1246.24.
The value the first and the third measurement. That will go in odd sum, which is over here, 2355.75.
And even would be 1662.74, which is the case over here. Now, for 7 11, we only have two records. So, the first one will be odd and the second one will be even. 1124.50 is odd and 123.
1234.14 is even sum.
So, let's write the logic. Let's write the query for this one. So, I would request you all to pause the video for some time and then try to write the query on your own and then get back to the video.
So, what we need to do is for all the odd times, we have to sum it together and for all the even times, we have to sum it together.
So, we need to use row number for each particular measurement time, measurement day actually. For each particular day, let's say 7 10 2022, we have to group it together and then we have to number it 1 2 3 and then we have to sum [snorts] it based on whether it is an even number or an odd number. Let's write the solution and then it will be much clearer. So, select star from measurements.
And let's create row number. [snorts] Row number over partition by and we have to partition it by measurement time. Actually, not measurement time, but rather the date.
So, I will use the date function and I will write measurement time.
as RN1. I will give an alias. Let's run this.
So, what this is doing is for for each date let's say 2022 7 10 it is grouping it together and then it is giving row number.
1 2 3. Then for 7 11, we have 1 2 3 4.
For 12, we have 1 2 3 and so on.
So, now we have even numbered it. Now, we need to just sum all the odd records and then sum all the even records. So, we can use a CTE.
We can put this in a CTE.
With CTE _measurements as. So, this is our CTE where we have the row numbers.
Now, we need to just group all the odd records and the even records together. So, select measurement. So, in our output, the first column is measurement day.
So, I will do date of measurement time because we do not need the time, we just need the day.
And I will give an alias measurement day.
Comma, now we need to sum all the odd records and sum all the even records.
So, sum and then I will use a case statement.
Case when RN1 mod two is not equal to zero.
So, whenever the mod two is not equal to zero, it means it is odd.
Then I will take measurement value.
Else zero.
Sum of case when RN1 mod two is not equal to zero, then measurement value else zero.
This will be odd_sum. This will be odd sum.
And let's write similarly for even sum.
When RN1 mod two is equal to zero, then it it means it is an even number. That particular RN is even number.
So, this will be even sum from CT_measurements.
Also also we need to order the row number by measurement time.
So, row number over partition by that particular day and then we have to order it by measurement time in ascending order only.
So, I will not write anything after that because by default it is ascending.
Now, our CTE.
This is where the CTE ends.
This is our CTE.
So, this is our CTE. Our CTE is pulling data from the measurements table and it is also pulling row number. What is row number doing? It is partitioning by that particular day and then it is ordering in measurement time and then it is giving a row number.
Because in our question, within a day measurements taken at first, third, and fifth times are considered odd numbered measurements and measurements taken at second, fourth, and sixth times are considered even number. So, we also need to order it.
Now, in our CTE, we are pulling the measurement day, then the odd sum, even sum from CTE measurements and then we will group it by measurement day.
And then we also will be ordering it by measurement day.
Let's run this.
Also, we need to end the case statement.
End. Let's run this again.
And this is our output.
Let's submit this.
And it has been accepted. So, it's a very simple question. We will be using CTE. You can also use subquery. We will be using row number and then we will order it by measurement time and we will give an alias RN1 and then we will use the row number. We will use the row number to find whether it was uh it was whether the measurement was taken at an odd time or an even time. So, we will use the mod function to find out even or odd number.
And then we will just use case statement along with sum to get that output. And then we'll group it by measurement day and order it by measurement day. And that is our output.
I hope this video was helpful. Thank you so much for watching.
相关推荐
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











