# LeetCode 610: Triangle Judgement

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

```sql
Triangle
```

Output columns:

```sql
x, y, z, triangle
```

The `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:

```text
13 + 15 = 28
```

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

For the second row:

```text
10 + 20 > 15
10 + 15 > 20
20 + 15 > 10
```

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

```sql
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

```sql
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:

```sql
FROM Triangle
```

It returns the original side lengths:

```sql
SELECT
    x,
    y,
    z
```

Then it adds a computed column:

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

The `WHEN` condition checks all three triangle inequalities:

```sql
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`.

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

```sql
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:

```sql
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:

```sql
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 |

