Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5ad68d3

Browse files
committed
More sql
1 parent483e90f commit5ad68d3

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

‎hackerrank/Sql/README.md

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,45 +179,102 @@ LIMIT 1;
179179
```
180180

181181
###Weather Observation Station 15
182-
182+
Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than 137.2345. Round your answer to 4 decimal places.
183183
```sql
184184
-- mysql
185-
185+
SELECT ROUND(LONG_W,4)
186+
FROM station
187+
WHERE LAT_N<137.2345
188+
ORDER BY LAT_NDESC
189+
LIMIT1;
186190
```
187191

188192
###Weather Observation Station 16
189-
193+
Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7880. Round your answer to 4 decimal places.
190194
```sql
191195
-- mysql
192-
196+
SELECT ROUND(LAT_N,4)
197+
FROM station
198+
WHERE LAT_N>38.7880
199+
ORDER BY LAT_NASC
200+
LIMIT1;
193201
```
194202

195203
###Weather Observation Station 17
196-
204+
Query the Western Longitude (LONG_W) where the smallest Northern Latitude (LAT_N) in STATION that is greater than 38.7880. Round your answer to 4 decimal places.
197205
```sql
198206
-- mysql
199-
207+
SELECT ROUND(LONG_W,4)
208+
FROM station
209+
WHERE LAT_N>38.7880
210+
ORDER BY LAT_NASC
211+
LIMIT1;
200212
```
201213

202214
###Weather Observation Station 18
215+
Consider P1(a, b) and P2(c,d) to be two points on a 2D plane.
216+
- a - happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
217+
- b - happens to equal the minimum value in Western Longitude (LONG_W in STATION).
218+
- c - happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
219+
- d - happens to equal the maximum value in Western Longitude (LONG_W in STATION).
203220

221+
Query the Manhattan Distance between points P1 and P2 and round it to a scale of 4 decimal places.
204222
```sql
205223
-- mysql
206-
224+
SELECT
225+
ROUND(
226+
ABS(MIN(LAT_N)-MAX(LAT_N))+ ABS(MIN(LONG_W)-MAX(LONG_W)),
227+
4
228+
)AS manhattan_distance
229+
FROM
230+
station;
207231
```
208232

209233
###Weather Observation Station 19
210-
234+
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.
235+
Query the Euclidean Distance between points P1 and P2 and round it to a scale of 4 decimal places.
211236
```sql
212237
-- mysql
213-
238+
SELECT
239+
ROUND(
240+
SQRT(POW(MIN(LAT_N)-MAX(LAT_N),2)+ POW(MIN(LONG_W)-MAX(LONG_W),2)),
241+
4
242+
)AS euclidean_distance
243+
FROM
244+
station;
214245
```
215246

216247
###Weather Observation Station 20
217-
248+
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.
218249
```sql
219250
-- mysql
220-
251+
WITH OrderedLatitudesAS (
252+
SELECT LAT_N
253+
FROM station
254+
ORDER BY LAT_N
255+
),
256+
RowNumberedAS (
257+
SELECT LAT_N, ROW_NUMBER() OVER ()AS row_num
258+
FROM OrderedLatitudes
259+
),
260+
CountedAS (
261+
SELECTCOUNT(*)AS total_count
262+
FROM station
263+
)
264+
SELECT ROUND(
265+
CASE
266+
-- Odd number of records: pick the middle value
267+
WHEN total_count %2=1 THEN
268+
(SELECT LAT_NFROM RowNumberedWHERE row_num= (total_count+1)/2)
269+
-- Even number of records: average the two middle values
270+
ELSE
271+
(SELECTAVG(LAT_N)
272+
FROM RowNumbered
273+
WHERE row_numIN (total_count/2, total_count/2+1))
274+
END,
275+
4
276+
)AS median_lat_n
277+
FROM Counted;
221278
```
222279

223280

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp