Triangle Judgement | LeetCode 610 | Crack SQL Interviews in 50 Qs
This comprehensive study note covers LeetCode Problem 610: Triangle Judgement, exploring the mathematical rules of geometric shapes in relational databases, implementing conditional logic using SQL IF statements, and handling multi-condition boolean evaluations.
1. Problem Statement & Context
In this 32nd question of our SQL interview series, we tackle a classic geometry problem framed inside a database context. We are given a single database table named Triangle that stores the lengths of three line segments (x, y, and z) for multiple rows.
The Database Schema
- Table Name:
Triangle - Columns:
x,y,z(all integer data types) - Primary Key: The combination of
(x, y, z)forms the unique identifier for each row.
Objective
For every row of line segments, report whether they can form a valid triangle. The output must include the original side lengths (x, y, z) alongside a new calculated column named triangle containing either 'Yes' or 'No'. The result table can be in any order.
2. Core Mathematical Mental Model: The Triangle Inequality Theorem
Before writing any SQL code, we must recall a fundamental geometric rule learned in school: The Triangle Inequality Theorem.
Real-Life Analogy: The Wooden Sticks Rule
Analogy: Imagine you are given three wooden sticks of lengths
a,b, andc, and you want to lay them flat on a table to form a closed triangular shape. If you pick any two sticks and lay them end-to-end, their combined length must be strictly greater than the length of the third remaining stick. If the sum of the two smaller sticks equals or is shorter than the third stick, they will never meet at the top to form a closed vertex.
Mathematically, for three sides a, b, and c to form a valid triangle, all three of the following conditions must simultaneously hold true:
Translating this to our column names (x, y, z), the conditional requirements become:
3. Sample Data Walkthrough
Let us examine how sample data behaves under the Triangle Inequality Theorem.
4. Naive Approach vs. Production Conditional Logic
When tackling conditional columns in SQL, developers often consider multiple approaches.
Approach Options in SQL
- Subqueries / CASE Expressions: Standard SQL
CASE WHENstatements are universally supported across database engines (MySQL, PostgreSQL, SQL Server, Oracle). - Engine-Specific
IFFunctions: MySQL provides a shorthandIF(condition, true_result, false_result)function, which keeps queries concise for simple binary checks.
5. Step-by-Step Query Construction
Let's build the SQL query step by step.
Step 1: Selecting the Source Table
Since all data resides in a single table, our FROM clause is straightforward:
FROM Triangle
Step 2: Selecting All Input Columns
To include all existing side length columns (x, y, z) in our final output, we can use SELECT *:
SELECT *, ...
FROM Triangle;
Step 3: Implementing Conditional Logic with MySQL IF()
We need to evaluate our three simultaneous inequalities using the logical AND operator and wrap them inside an IF statement:
IF(
x + y > z
AND y + z > x
AND x + z > y,
'Yes',
'No'
)
Step 4: Aliasing the Calculated Column
The problem requires the output column to be named triangle. We use the AS keyword to alias our conditional expression:
AS triangle
6. Complete Production SQL Solution
Putting all the pieces together, here is the complete, clean MySQL query that passes LeetCode:
SELECT
x,
y,
z,
IF(
x + y > z
AND y + z > x
AND x + z > y,
'Yes',
'No'
) AS triangle
FROM
Triangle;
Alternatively, using standard ANSI SQL CASE WHEN (compatible with PostgreSQL, SQL Server, etc.):
SELECT
x,
y,
z,
CASE
WHEN x + y > z AND y + z > x AND x + z > y THEN 'Yes'
ELSE 'No'
END AS triangle
FROM
Triangle;
7. Execution Flow Walkthrough
8. Summary and Key Takeaways
- Geometric Theorem: Validating a triangle requires checking that the sum of any two sides is strictly greater than the third side ().
- Simultaneous Conditions: All three inequality combinations must be true concurrently, requiring the logical
ANDoperator. - MySQL
IF()Function: A concise conditional shorthandIF(condition, true_value, false_value)is ideal for binary outcome columns. - ANSI Standard Alternative:
CASE WHEN ... THEN ... ELSE ... ENDprovides cross-platform database portability. - Column Aliasing: Use the
ASkeyword to assign clean, required output names (AS triangle) to derived columns. - Data Types: String literals in SQL ('Yes', 'No') must always be enclosed in single quotes.
- Simplicity in Design: Single-table problems do not require complex
JOINoperations; focus on clean projection and row-level transformation.