Can These Braces Form a Triangle?
A carpentry shop cuts three boards for every triangular roof brace. Before gluing, the foreman wants to know which sets of cuts can actually form a triangle. Three lengths form a triangle only if each length is strictly less than the sum of the other two. Boards that only lie flat (for example 10, 20, 30) do not count.
Return one row per cut set with the columns set_id, board_a, board_b, board_c, forms_triangle, where forms_triangle is the text 'Yes' or 'No'. Rows may be in any order; an empty table gives no rows.
Tables
brace_cuts — One row per set of three boards cut for a brace.
| Column | Type | Description |
|---|---|---|
set_id (key) | INTEGER | Unique id of the cut set |
board_a | INTEGER | Length of the first board in millimetres (positive) |
board_b | INTEGER | Length of the second board in millimetres (positive) |
board_c | INTEGER | Length of the third board in millimetres (positive); the longest board can be in any column |
Example
brace_cuts
| set_id | board_a | board_b | board_c |
|---|---|---|---|
| 1 | 300 | 400 | 500 |
| 2 | 100 | 200 | 300 |
| 3 | 250 | 250 | 250 |
| 4 | 900 | 200 | 300 |
| 5 | 120 | 500 | 410 |
| 6 | 50 | 60 | 200 |
Expected output
| set_id | board_a | board_b | board_c | forms_triangle |
|---|---|---|---|---|
| 1 | 300 | 400 | 500 | Yes |
| 2 | 100 | 200 | 300 | No |
| 3 | 250 | 250 | 250 | Yes |
| 4 | 900 | 200 | 300 | No |
| 5 | 120 | 500 | 410 | Yes |
| 6 | 50 | 60 | 200 | No |
Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Strings & CASE · problem 51 of 70 · SQLite
The idea
The triangle inequality says three lengths form a triangle only if every pair together is longer than the third. Translate that literally into a CASE expression with three comparisons joined by AND, returning 'Yes' or 'No' for every row.
Two details catch people out. First, the comparison must be strict: when a + b = c the boards lie in a straight line. Second, the longest board can sit in any column, so checking only a + b > c is not enough.
The alternative uses a neat equivalence. Only the longest side can break the rule, and "longest < sum of the other two" is the same as "total > 2 × longest". SQLite's multi-argument scalar max(a, b, c) returns the largest of its arguments, so one comparison does the job.
Solution
SELECT set_id,
board_a,
board_b,
board_c,
CASE
WHEN board_a + board_b > board_c
AND board_a + board_c > board_b
AND board_b + board_c > board_a THEN 'Yes'
ELSE 'No'
END AS forms_triangle
FROM brace_cuts;
Another way
SELECT set_id,
board_a,
board_b,
board_c,
iif(board_a + board_b + board_c > 2 * max(board_a, board_b, board_c),
'Yes', 'No') AS forms_triangle
FROM brace_cuts;
Why common answers fail
Uses >= so flat sets such as 10, 20, 30 are wrongly reported as triangles.
SELECT set_id, board_a, board_b, board_c, CASE WHEN board_a + board_b >= board_c AND board_a + board_c >= board_b AND board_b + board_c >= board_a THEN 'Yes' ELSE 'No' END AS forms_triangle FROM brace_cuts;
Checks only one of the three inequalities, assuming the longest board is always board_c.
SELECT set_id, board_a, board_b, board_c, CASE WHEN board_a + board_b > board_c THEN 'Yes' ELSE 'No' END AS forms_triangle FROM brace_cuts;
What the interviewer is checking
A warm-up to see whether you translate a rule into CASE carefully, including strict boundaries and symmetric conditions.
Mistakes to avoid
- Using >= and accepting flat sets.
- Testing only one inequality.
- Confusing the scalar max(a, b, c) with the aggregate MAX(column).
Follow-ups to expect
- Classify each triangle as equilateral, isosceles or scalene (more WHEN branches, checked in the right order).
- Count how many sets fail (SUM over the CASE).
- What changes if lengths could be NULL? (Comparisons become NULL and fall to ELSE; decide whether that should be 'No' or NULL.)
In real work
Rule-based labelling with CASE is everyday work: flagging invalid measurements, eligibility checks and data-quality columns.