A SQL guide for checking whether three side lengths can form a valid triangle using the triangle inequality.
Problem Restatement
We are given a table called Triangle.
Each row contains three integer side lengths:
| Column | Type | Meaning |
|---|---|---|
x | int | First side length |
y | int | Second side length |
z | int | Third side length |
For each row, we need to report whether the three side lengths can form a valid triangle.
The result should contain the original side lengths and a new column called triangle, whose value is either "Yes" or "No".
The problem asks us to report whether every group of three line segments can form a triangle.
Input and Output
Input table:
TriangleOutput columns:
x, y, z, triangleThe triangle column should be:
| Value | Meaning |
|---|---|
Yes | The three sides can form a triangle |
No | The three sides cannot form a triangle |
Example
Input:
| x | y | z |
|---|---|---|
| 13 | 15 | 30 |
| 10 | 20 | 15 |
For the first row:
13 + 15 = 28Since 28 <= 30, the sides cannot form a triangle.
For the second row:
10 + 20 > 15
10 + 15 > 20
20 + 15 > 10All three conditions are true, so the sides can form a triangle.
Output:
| x | y | z | triangle |
|---|---|---|---|
| 13 | 15 | 30 | No |
| 10 | 20 | 15 | Yes |
First Thought: Use the Triangle Inequality
Three positive lengths can form a triangle only when every pair of sides has a sum greater than the remaining side.
So the required conditions are:
x + y > z
AND x + z > y
AND y + z > xIf all three are true, return "Yes".
Otherwise, return "No".
Key Insight
This is a row-by-row classification problem.
We do not need joins, grouping, sorting, or window functions.
For each row, we only apply a conditional expression.
SQL provides CASE WHEN for this exact pattern.
Algorithm
For each row in Triangle:
- Keep
x,y, andz. - Check the three triangle inequality conditions.
- If all conditions are true, output
"Yes". - Otherwise, output
"No".
SQL Solution
SELECT
x,
y,
z,
CASE
WHEN x + y > z
AND x + z > y
AND y + z > x
THEN 'Yes'
ELSE 'No'
END AS triangle
FROM Triangle;Code Explanation
The query reads each row from the table:
FROM TriangleIt returns the original side lengths:
SELECT
x,
y,
zThen it adds a computed column:
CASE
WHEN ...
THEN 'Yes'
ELSE 'No'
END AS triangleThe WHEN condition checks all three triangle inequalities:
x + y > z
AND x + z > y
AND y + z > xIf one condition fails, the row cannot form a valid triangle, so the result is "No".
Correctness
For any row, the three numbers x, y, and z can form a triangle exactly when the sum of every two sides is strictly greater than the third side.
The query returns "Yes" exactly when all three inequalities are true.
If any inequality is false, then one side is too long, or exactly equal to the sum of the other two sides. In either case, the three segments cannot form a non-degenerate triangle, so the query returns "No".
Therefore, each row is classified correctly.
Complexity
Let n be the number of rows in Triangle.
| Metric | Value | Why |
|---|---|---|
| Time | O(n) | Each row is checked once |
| Space | O(1) | No extra table-sized structure is required |
Alternative: MySQL IF
In MySQL, we can write the same logic with IF.
SELECT
x,
y,
z,
IF(
x + y > z
AND x + z > y
AND y + z > x,
'Yes',
'No'
) AS triangle
FROM Triangle;The CASE WHEN version is more portable across SQL dialects.
Testing
Sample data:
CREATE TABLE Triangle (
x INT,
y INT,
z INT,
PRIMARY KEY (x, y, z)
);
INSERT INTO Triangle (x, y, z) VALUES
(13, 15, 30),
(10, 20, 15);Expected output:
| x | y | z | triangle |
|---|---|---|---|
| 13 | 15 | 30 | No |
| 10 | 20 | 15 | Yes |
Additional case:
TRUNCATE TABLE Triangle;
INSERT INTO Triangle (x, y, z) VALUES
(3, 4, 5),
(1, 1, 2),
(7, 7, 7);Expected output:
| x | y | z | triangle |
|---|---|---|---|
| 3 | 4 | 5 | Yes |
| 1 | 1 | 2 | No |
| 7 | 7 | 7 | Yes |