Lesson 17
Weather Observation Station 17 | SQL Aggregation | HackerRank Solution
Summary
Summary of Weather Observation Station 17
In this lesson, we focus on querying data from the "station" table as part of HackerRank's weather observation station task.
Task Overview
- Objective: Retrieve the
long_W
value for the row that has the smallestlat_n
greater than 38.778. - Columns in the Station Table:
ID
city
state
lat_n
long_W
Steps to Solve the Problem
- Initial Query: Begin by visualizing the data with a simple SQL query:
SELECT * FROM station;
- Filtering Data: Use a
WHERE
clause to filter out rows wherelat_n
is greater than 38.778. - Ordering Results: Use
ORDER BY lat_n ASC
to sort the results from smallest to largest. - Limiting Output: Use
LIMIT 1
to return only the first row, which would have the smallestlat_n
greater than 38.778. - Selecting the Desired Value: Finally, select the
long_W
value and round it to four decimal places using theROUND
function:SELECT ROUND(long_W, 4) FROM station WHERE lat_n > 38.778 ORDER BY lat_n ASC LIMIT 1;
- Final Output: After running the query, the output displays the rounded value of
long_W
(70.1378) for the required row.
Conclusion
This walkthrough demonstrates how to efficiently query specific data from a table using SQL by applying filtering, ordering, and rounding functions. This completes the requirement for Weather Observation Station 17.
Video Transcript
Hey everybody, welcome to another lesson. I will do weather observation station 17 from the subsection aggregation
Section sequel from hacker rank. So we're given the table station that has five columns ID city state lat underscore n and long underscore
W so we're asked to query the long underscore W value for the row that has the smallest lat underscore n
That's greater than 38778 that is the lat underscore n has with it be within that range of
Greater than 38.778 and make sure to round to four decimal places
So let's get started. I choose my sequel and I like to visualize the data initially. So I select star from station
Click run code
Okay, so if you see these values here for the lat underscore n if you have greater than
38.778 that means this value here wouldn't be included
but you for example, you would have to take the lowest and
Then from that row you have to take the value from
Long underscore W. So to do this. I'm gonna use order by
the value of
lat underscore and I'm gonna find a row that has the
Smallest and from that row I'm gonna select the along W. So here. I'm gonna say first
Let's add a where the lat underscore w is greater than 38.778 and
Then I'm gonna do order by
The value of lat underscore in and in this case it has to be from the lowest to the highest
So I can take the very first row that's gonna be the smallest. So this is by default ASC
I it's implicit. You don't have to say it
but if I want to be explicit as right here ASC ascending and
I limit one to only take the very first row out of all this these rows because I know the first one is gonna have the
smallest value and
Then from that we have to select the value of long and short W. So that's what we put in select
But because we want to round it to four decimal places
We have to call the round function past the long W value here comma four decimal places close parentheses
run code now
And there you go have seventy point thirteen seventy eight has been routed to four decimal places submit code
And
That's weather observation station 17
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: