Loading
Lesson 14
Courses / HackerRank SQL Problem Solving Solution Challenges
Weather Observation Station 14 | SQL Aggregation | HackerRank Solution

Video Transcript

Hey everybody, welcome to another lesson. I will do WebDor observation station 14 from the section aggregation from SQL in Hacker Rank. So this question we have the station table with ID city state of lat underscore n and longer underscore w columns. And we're asked to query the greatest value of lat underscore n that is less than 137.2345 truncate to four decimal places. So I got three things to keep in mind, right? So filter out meaning you only take the values for the purpose of this that's less than 137.2345 for lat underscore n. And at the end we should also truncate to four decimal places, meaning we drop everything after the fourth digit to the right of the dot. It's different from round, so we use truncate instead of round to be technically correct. And yeah, greatest value just means taking the maximum value. So there are many different ways to do this problem, I'll do with the max function. So I always like to start here, let me choose my SQL select star from station to visualize what the data looks like and have a better idea of what to do. Okay, so the purposes of this question, we care about the lat underscore n column here, I see these values. So trying to find the greatest one that's less than 137.2345. So let's go here. First let's add a where lat underscore n has to be less than 137.2345. But the value is greater than that, we don't care about it, we won't consider it. And then instead of star here, I'm going to first say lat n, but that will just take us all the rows of the lat n values, but we have to take the maximum one. So one way of doing this, it could order by the value itself and take the very first row if you are descending for the greatest, but we can also use the max function here. So if I say max parenthesis of lat n, that will take the maximum value that is the greatest that it can find within the selection range that for this where here. And because we want to truncate, meaning you want to drop all the digits to the right of the fourth one after the dot coming from the left, right? We have to say truncate, past max lat n comma four, that means only keep four decimal places. And that's different from round round around up or down truncate just drops the digits without rounding. Let's run the code. And there you go. We got 137.0193 and we got truncated. We only have four digits to the right hand side of the dots. And that's Watter observation station 14.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: