Shortest Distance in a Line
Problem
Table: Point
+-------------+------+
| Column Name | Type |
+-------------+------+
| x | int |
+-------------+------+
In SQL, x is the primary key column for this table.
Each row of this table indicates the position of a point on the X-axis.
Find the shortest distance between any two points from the Point
table.
Solution
The problem Shortest Distance in a Line
can be solved by joining Point
with Point
to match each points with points further on the X-axis and calculating the shortest distance between them.
Implementation
# Write your MySQL query statement below
SELECT MIN(p2.x - p1.x) AS shortest
FROM
point AS p1
INNER JOIN point AS p2 ON p1.x < p2.x;