Skip to content

LeetCode 610: Triangle Judgement

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:

ColumnTypeMeaning
xintFirst side length
yintSecond side length
zintThird 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:

Triangle

Output columns:

x, y, z, triangle

The triangle column should be:

ValueMeaning
YesThe three sides can form a triangle
NoThe three sides cannot form a triangle

Example

Input:

xyz
131530
102015

For the first row:

13 + 15 = 28

Since 28 <= 30, the sides cannot form a triangle.

For the second row:

10 + 20 > 15
10 + 15 > 20
20 + 15 > 10

All three conditions are true, so the sides can form a triangle.

Output:

xyztriangle
131530No
102015Yes

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

If 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:

  1. Keep x, y, and z.
  2. Check the three triangle inequality conditions.
  3. If all conditions are true, output "Yes".
  4. 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 Triangle

It returns the original side lengths:

SELECT
    x,
    y,
    z

Then it adds a computed column:

CASE
    WHEN ...
    THEN 'Yes'
    ELSE 'No'
END AS triangle

The WHEN condition checks all three triangle inequalities:

x + y > z
AND x + z > y
AND y + z > x

If 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.

MetricValueWhy
TimeO(n)Each row is checked once
SpaceO(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:

xyztriangle
131530No
102015Yes

Additional case:

TRUNCATE TABLE Triangle;

INSERT INTO Triangle (x, y, z) VALUES
    (3, 4, 5),
    (1, 1, 2),
    (7, 7, 7);

Expected output:

xyztriangle
345Yes
112No
777Yes