## Recent Posts

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.

SQL Problem Statement:

Consider P1(a,c) and P2(b,d) to be two points on a 2D plane where (a,b) are the respective minimum and maximum values of Northern Latitude(LAT_N) and (c,d) are the respective minimum and maximum values of Western Longitude(LONG_W) in STATION.

Query the Euclidean Distance between points P1 and P2 and format your answer to display 4 decimal digits.

Input Format:

The STATION table is described as follows:

 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, POWER, SQRT & ROUND 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(SQRT(POWER(@a-@c,2) + POWER(@b-@d,2)),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. POWER function returns the value of a number raised to the power of another number.
6. SQRT function returns the square root of the number.

### Solution-2: Using MIN, MAX, POWER, SQRT & ROUND Function (MySQL Query):

```SELECT ROUND(SQRT(POWER(MIN(LAT_N)-MAX(LAT_N),2) + POWER(MIN(LONG_W)-MAX(LONG_W),2)),4)
FROM STATION;```

### Expected Output:

`184.1616`

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