HackerRank: [SQL Aggregation] (15/17) Weather Observation Station-18 | set, round, min, max, abs function in SQL

HackerRank: [SQL Aggregation - 15/17] Weather Observation Station-18 | SET, ROUND, MIN, MAX, ABS Function in SQL
I started studying SQL from a very famous site - HackerRank. Here I will try to provide multiple approaches & solutions to the same problem. It will help you learn and understand SQL in a better way.

Please make use of my blog posts for learning purpose only and feel free to ask your questions in the comment box below in case of any doubt.

Click Here for the previous blog-post in the series.


SQL Problem Statement:

Consider P1(a,b) and P2(c,d) to be two points on a 2D plane. 

  • a happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
  • b happens to equal the minimum value in Western Longitude (LONG_W in STATION).
  • c happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
  • d happens to equal the maximum value in Western Longitude (LONG_W in STATION).

Query the Manhattan Distance between points P1 and P2 and round it to a scale of 4 decimal places.



Input Format:

The STATION table is described as follows:

Table: STATION
Table: STATION
where LAT_N is the northern latitude and LONG_W is the western longitude.

Sample data in Table (STATION):
ID CITY STATE LAT_N LONG_W
794 Kissee Mills MO 140 73
824 Loma Mar CA 49 131
603 Sandy Hook CT 72 148
478 Tipton IN 34 98
619 Arlington CO 75 93
711 Turner AR 50 101
839 Slidell LA 85 152
.
.
.
754 South Haven MI 145 53
144 Eskridge KS 1



Solution-1: Using SET, MIN, MAX & ABS Function (MySQL Query):

SET @a := (SELECT MIN(LAT_N) FROM STATION);
SET @b := (SELECT MIN(LONG_W) FROM STATION);
SET @c := (SELECT MAX(LAT_N) FROM STATION);
SET @d := (SELECT MAX(LONG_W) FROM STATION);

SELECT ROUND(ABS(@a - @c) + ABS(@b - @d), 4);

NOTE: 
  1. SET keyword is used to assign value to a variable in SQL.
  2. ROUND Function is used to round the decimal numbers up to mentioned length after the decimal point. Here, 4 is passed because we want output scale up to 4 decimal points.
    Eg.
    ROUND(2.7685, 2) will return 2.77
    ROUND(2.3327, 0) will return 2.33
  3. MIN is an aggregation function used to get the least (minimum) value of all the records in the column name passed to the function.
  4. MAX is an aggregation function used to get the largest (maximum) value of all the records in the column name passed to the function.
  5. ABS is a function used to get the absolute value of all the records in the column name passed to the function.



Solution-2: Using MIN, MAX & ABS Function (MySQL Query):

SELECT ROUND(ABS(MIN(LAT_N) - MAX(LAT_N)) + ABS(MIN(LONG_W) - MAX(LONG_W)), 4)
FROM STATION;

NOTE: 
  1. ROUND Function is used to round the decimal numbers up to mentioned length after the decimal point. Here, 4 is passed because we want output scale up to 4 decimal points.
    Eg.
    ROUND(2.7685, 2) will return 2.77
    ROUND(2.3327, 0) will return 2.33
  2. MIN is an aggregation function used to get the least (minimum) value of all the records in the column name passed to the function.
  3. MAX is an aggregation function used to get the largest (maximum) value of all the records in the column name passed to the function.
  4. ABS is a function used to get the absolute value of all the records in the column name passed to the function.



Expected Output:

259.6859



--------------------------------------------------------------------------------
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks & Regards,
-Akshay P Daga
إرسال تعليق (0)
أحدث أقدم