CMD Guide
HomeDatabasesSQL Practice Problems

Triangle Judgement

Problem

Table: Triangle

+-------------+------+
| Column Name | Type |
+-------------+------+
| x           | int  |
| y           | int  |
| z           | int  |
+-------------+------+
In SQL, (x, y, z) is the primary key column for this table.
Each row of this table contains the lengths of three line segments.

Report for every three line segments whether they can form a triangle.

Return the result table in any order.

Example

Image
Image

Output

Image
Image

Try it YourSelf

sql
-- TODO: Write your user queries here

Solution

To assess the validity of triangles defined by sets of three values (x, y, z), the approach involves employing a SQL query on the 'Triangle' table. The query utilizes a CASE statement to evaluate the conditions of the triangle inequality theorem, determining whether the sum of any two sides is greater than the length of the third side. The result will be a clear classification of each set as either forming a valid ('Yes') or invalid ('No') triangle, providing a straightforward analysis of the geometric properties within the 'Triangle' dataset."

SELECT x, y, z, CASE WHEN x + y > z AND y + z > x AND x + z > y THEN 'Yes' ELSE 'No' end triangle FROM Triangle

Let's break down the SQL query step by step.

Step 1: SELECT Statement

This query selects columns x, y, and z from the triangle table and adds a new column named triangle. The CASE statement checks whether the given set of values (x, y, z) satisfies the conditions for forming a triangle.

Step 2: Evaluate the CASE statement

The conditions within the WHEN clause check whether the sum of any two sides is greater than the third side. The conditions are:

CASE WHEN x + y > z AND y + z > x AND x + z > y THEN 'Yes' ELSE 'No'

If all three conditions are true, then the CASE statement evaluates to 'Yes,' indicating that the given values can form a valid triangle.

If any of the conditions is false, the ELSE clause is triggered, and the CASE statement evaluates to 'No,' indicating that the values cannot form a valid triangle.

Final Output:

+----+----+----+----------+ | x | y | z | triangle | +----+----+----+----------+ | 13 | 15 | 30 | No | | 10 | 20 | 15 | Yes | +----+----+----+----------+

Pattern: row-local CASE predicate (derived label column)

Name: encode a pure per-row geometric/business rule as a CASE expression that labels each row without grouping or joining.

Rule (strict triangle inequality): three lengths form a non-degenerate triangle iff each pair sum is strictly greater than the third: x+y>z AND y+z>x AND x+z>y.

xyzx+y>zy+z>xx+z>ylabel
13153028>30 FTTNo (flat / impossible)
102015TTTYes
551010>10 FTTNo — degenerate (collinear points); strict > rejects it

Strict vs non-strict: using >= would accept zero-area "triangles." The problem's sample and standard geometry interview expect strict >.

NULL sides: any NULL in x/y/z makes comparisons UNKNOWN → CASE falls to ELSE 'No' (safe default). Overflow: very large INT sides can overflow on x+y in some engines; use BIGINT or compare carefully (x > z - y with care about signs).

When-NOT: do not GROUP BY — each triple is independent. Do not use joins to a geometry table; the predicate is closed-form.

Drill: Label rows as 'Degenerate' when equality holds on one limb and the other two strict inequalities hold. Write the CASE.

🎯 STRICT STANDOUT: Why / mental model / when-not / worked / failure / hostile panel — Triangle Judgement

Why this concept exists (judgment layer)

Pattern: row-local CASE encoding a closed-form predicate. Geometry interviews and SQL both test strict triangle inequality and degenerate (zero-area) edges.

Mental model (install this intuition)

Three lengths form a non-degenerate triangle iff each pair sum is strictly greater than the third. CASE labels Yes/No per row — no GROUP BY, no join.

Worked example with numbers or traced steps

(13,15,30): 13+15=28 < 30 → No
(10,20,15): all three inequalities hold → Yes
(5,5,10): 5+5=10 not > 10 → No (degenerate line)
Pattern name: derived label column via CASE
NULL side → comparisons UNKNOWN → ELSE 'No'

When NOT to use / named alternative

Do not GROUP BY sides. Do not use >= if problem wants positive area. Do not solve with self-joins — predicate is local. Overflow: use wider types if sums may exceed INT.

Failure mode & ops fingerprint

Fingerprint: accepting collinear points with >=; wrong sample when only one inequality checked; silent NULL labeled Yes if CASE order wrong.

Hostile-panel drills (defend the decision)

Q1. Strict vs non-strict inequality?
Model answer: Strict > rejects zero-area; >= accepts degenerate 'triangles.' Problem sample needs strict.

Q2. Pattern name?
Model answer: Row-local CASE predicate / derived classification column — pure per-row rule.

Q3. How to label Degenerate separately?
Model answer: WHEN all three >= and at least one equality THEN 'Degenerate' WHEN all > THEN 'Yes' ELSE 'No'.

🤖 Don't fully get this? Learn it with Claude

Stuck on Triangle Judgement? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Triangle Judgement** (Databases) and want to truly understand it. Explain Triangle Judgement from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Triangle Judgement** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Triangle Judgement** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Triangle Judgement** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes