Recent Posts

HackerRank: [SQL Aggregation] (17/17) Weather Observation Station-20 | round, row_number, ceil, floor & '@' operator in SQL

HackerRank: [SQL Aggregation - 17/17] Weather Observation Station-20 | ROUND, ROW_NUMBER, CEIL, FLOOR & '@' operator 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:

A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to 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 ROUND, ROW_NUMBER, CEIL Function (MySQL Query):

SELECT t.lat
FROM(
    SELECT ROUND(LAT_N, 4) as lat, 
    ROW_NUMBER() OVER(ORDER BY LAT_N) AS ROW_NUM
    FROM STATION
    ORDER BY ROW_NUM
  ) t
WHERE t.ROW_NUM = CEIL((SELECT COUNT(*) + 1 FROM STATION) / 2);



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. ROW_NUMBER function is used to assign a unique number to each row in the table in the specified order.
  3. CEIL function used to get the nearest integer of the original number passed to the function. (The output will be always greater than or equal to the original number).
    Eg. CEIL(2.34) will return 3
  4. To calculate the Median of any column, We have to sort the column in ascending order and then select the middle number.
  5. Here, CEIL function will select the ((N/2)+1) th number.  

Solution-2: Using ROUND, ROW_NUMBER, FLOOR Function (MySQL Query):

SELECT t.lat 
FROM(
    SELECT ROUND(LAT_N, 4) as lat,
    ROW_NUMBER() OVER(ORDER BY LAT_N) AS ROW_NUM
    FROM STATION
    ORDER BY ROW_NUM
  ) t
WHERE t.ROW_NUM IN (FLOOR((SELECT COUNT(*) + 1 FROM STATION) / 2), FLOOR((SELECT COUNT(*) + 2 FROM STATION) / 2));


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. ROW_NUMBER function is used to assign a unique number to each row in the table in the specified order.
  3. FLOOR function is used to round down the decimal number to the nearest integer (smaller than or equal to the input number.)
    Eg.
    FLOOR(2.7) will return 2
  4. To calculate the Median of any column, We have to sort the column in ascending order and then select the middle number.
  5. Here, the FLOOR function will select the number on the lower side of the middle number.
    Eg. In the above code,
    • If N is Odd, (N=499), then FLOOR will select the 250th Number of the ascending sorted column.
    • If N is Even, (N=500), then FLOOR will select the 250th & 251st Number of the ascending sorted column.
    • IF required, you can take AVG of the 250th and 251st number OR
    • you can use FLOOR to select the 250th number OR
    • CEIL to select the 251st number.

Solution-3: Using ROUND, FLOOR Function (MySQL Query):

SELECT ROUND(t.LAT_N, 4)
FROM(
    SELECT s.LAT_N,
      @rownum := @rownum + 1 as `row_number`,
      @total_rows := @rownum
    FROM STATION s, (SELECT @rownum := 0) r
    ORDER BY s.LAT_N
  ) as t
WHERE t.row_number IN (FLOOR((@total_rows + 1) / 2), FLOOR((@total_rows + 2) / 2));


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. FLOOR function is used to round down the decimal number to the nearest integer (smaller than or equal to the input number.)
    Eg.
    FLOOR(2.7) will return 2
  3. To calculate the Median of any column, We have to sort the column in ascending order and then select the middle number.
  4. Here, the FLOOR function will select the number on the lower side of the middle number.
    Eg. In the above code,
    • If N is Odd, (N=499), then FLOOR will select the 250th Number of the ascending sorted column.
    • If N is Even, (N=500), then FLOOR will select the 250th & 251st Number of the ascending sorted column.
    • IF required, you can take AVG of the 250th and 251st number OR
    • you can use FLOOR to select the 250th number OR
    • CEIL to select the 251st number.

Solution-4: Using ROUND, FLOOR Function (MySQL Query):

SELECT ROUND(t.LAT_N, 4)
FROM(
    SELECT s.LAT_N,
      @rownum := @rownum + 1 as `row_number`
    FROM STATION s, (SELECT @rownum := 0) r
    ORDER BY s.LAT_N
  ) as t
WHERE t.row_number IN (FLOOR((@rownum + 1) / 2), FLOOR((@rownum + 2) / 2));



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. FLOOR function is used to round down the decimal number to the nearest integer (smaller than or equal to the input number.)
    Eg.
    FLOOR(2.7) will return 2
  3. To calculate the Median of any column, We have to sort the column in ascending order and then select the middle number.
  4. Here, the FLOOR function will select the number on the lower side of the middle number.
    Eg. In the above code,
    • If N is Odd, (N=499), then FLOOR will select the 250th Number of the ascending sorted column.
    • If N is Even, (N=500), then FLOOR will select the 250th & 251st Number of the ascending sorted column.
    • IF required, you can take AVG of the 250th and 251st number OR
    • you can use FLOOR to select the 250th number OR
    • CEIL to select the 251st number.



Expected Output:

83.8913



--------------------------------------------------------------------------------
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

No comments