Closest Mileposts on a Trail
Problem
A hiking trail has mileposts placed at integer positions along a straight path.
```
Milepost
+----------+------+
| Column | Type |
+----------+------+
| position | int |
+----------+------+
position is the primary key; each milepost sits at a distinct point.
```
Report the shortest distance between any two distinct mileposts. Name the output column `nearest`.
Tables
Example rows — the live problem includes the full dataset.
Milepost
| position |
|---|
| 5 |
| 9 |
| 12 |
Expected output
Your answer should return 1 row with the columns nearest.
Starter code (SQL)
SELECT *
FROM Milepost;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock