The Rectangles Area SQL problem asks you to compute the area of each rectangle defined by (p1, q1) and (p2, q2) coordinate pairs, filtering out degenerate cases (area = 0). Return only non-zero area rectangles with their computed area. The database interview pattern demonstrates computed column SQL.
Twitter/X asks this to test SQL arithmetic on coordinate data — computing (abs(p2-p1) × abs(q2-q1)) with a non-zero filter. It validates basic SQL math operations and WHERE clause filtering.
Computed column + filter.
SELECT p1, q1, p2, q2, ABS(p2-p1) * ABS(q2-q1) AS area
FROM Rectangle
WHERE ABS(p2-p1) * ABS(q2-q1) > 0
ORDER BY area DESC
Or equivalently: WHERE p1 <> p2 AND q1 <> q2.
Row (0,0,2,3): area=2*3=6. Row (0,0,1,1): area=1. Row (0,0,0,5): area=0 (degenerate — same x). Exclude last row.
SQL arithmetic problems require wrapping coordinate differences in ABS() to handle negative coordinate differences. The zero-area filter can use either WHERE area > 0 or WHERE p1 <> p2 AND q1 <> q2. Practice similar "computed geometry SQL" problems: "compute distances between points," "compute polygon areas from vertex tables."
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Student Number in Departments | Medium | Solve | |
| Tree Node | Medium | Solve | |
| Investments in 2016 | Medium | Solve | |
| Active Businesses | Medium | Solve | |
| Active Users | Medium | Solve |