Loading
Lesson 29
Courses / HackerRank SQL Problem Solving Solution Challenges
Type of Triangle | SQL Advanced Select | HackerRank Solution

Summary

Summary of Triangle Type Determination Lesson

In this lesson, the instructor discusses the SQL challenge "Type of Triangle" from HackerRank, which involves determining the type of triangle based on the lengths of its sides, labeled A, B, and C. The key steps and concepts are summarized below:

Problem Overview

  • The task is to assess each row of a table containing triangle side lengths and classify each triangle as:
    • Equilateral: All sides equal
    • Isosceles: Two sides equal
    • Scalene: All sides different
    • Not a Triangle: Fails the triangle inequality theorem

Triangle Inequality Theorem

To determine if three lengths can form a triangle, the following conditions (inequalities) must hold:

  1. A + B > C
  2. A + C > B
  3. B + C > A

If any of these conditions do not hold, the lengths do not form a triangle.

SQL Implementation

The instructor explains how to use a CASE statement in SQL to output the triangle type based on the conditions outlined. The implementation involves:

  1. Checking if the lengths form a triangle using the triangle inequality theorem.
  2. Classifying the triangle based on the equality of side lengths:
    • If all sides are equal, classify as Equilateral.
    • If two sides are equal, classify as Isosceles.
    • If no sides are equal, classify as Scalene.

SQL Code Outline

SELECT
  CASE
    WHEN (A + B <= C OR A + C <= B OR B + C <= A) THEN 'Not a triangle'
    WHEN (A = B AND B = C) THEN 'Equilateral'
    WHEN (A = B OR A = C OR B = C) THEN 'Isosceles'
    ELSE 'Scalene'
  END AS TriangleType
FROM triangles;

Conclusion

The lesson concludes by running the code, which correctly classifies the type of triangle for each set of side lengths provided in the table. The instructor confirms that the output aligns with the expected results for each row.


This concise summary captures the essence of the lesson while providing necessary details about the concepts involved and how to apply them in SQL.

Video Transcript

Hey everybody welcome to another lesson I will do type of triangle from the advanced select subsection of the sequel section hack rank. Here we're given this table here of triangles and each column is the size of one of the size of the triangle we have ABC and here's an example on the right hand left hand side and we're asked to pretty much tell for each row we have to say what kind of triangle it is and if it's not a triangle say it's not your triangle and here's an example output so basically we're going to use a case for the select so case according to some conditions we're going to output isosceles case occurrence of conditions equilateral scalene or not a triangle so we have four conditions here to check and that will determine what appears for each row and to understand what we should do about these things we have to understand some mathematics about the triangle inequality theorem and here's a page from my boyfriend you can get you can read about this but what matters is the sum of the lengths of any two sides of the triangle is greater than the length of the remaining side so we're going to keep that in mind as we work with the conditions for our case statement in the select and here's another website math warehouse.com that has a nice diagram and we have a triangle with the sides A B and C and this is what the inequality theorem says so these conditions three conditions must be met at all times for it to be a triangle so the sides A plus B has to be greater than the length of C and the sides B plus C has to be greater than the length of A and the A plus C has to be greater than the length of B so keep that in mind as we work through our case statement for the select okay and finally I want to show you this diagram for what kind of triangles so if the triangle has all the sides equal it's an equilateral and if one of the sides is not equal that's isosceles that is have two sides that are equal but the third one isn't and if the sides are all different in length it's a scalene like this one so keep that in mind visualize what we're doing in this problem so let's get back to it so here I'm going to use my sequel okay so we're going to select something that I'll write later from triangle stable now this something we have to output for each row the type of triangle or or not a triangle so I'm gonna use a case statement here okay case and I'm gonna add the end there to start and then we're gonna add when something and then okay when something then it's gonna be something when something there's gonna be something when something there's gonna be something okay and then finally I would do an else for whatever that what if it doesn't match in every cases okay so these cases will determine if it's a collateral isosceles, scalene, not a triangle so I want to start off by right off the bat seeing if it's a triangle or not so for this case I'm gonna do a then not a triangle so if this condition in the when matches what's gonna be output for that column value is this not a triangle so how can we determine what when it's not a triangle when it doesn't obey the inequality theorem if you remember this thing here so I'm gonna see if any of the sum of the sides is less right if you take a side if it's equal or less then the other side because the theorem says it has to be greater if it's not greater that means it's not a triangle so here I'm gonna say we take the side a add to the side b now if that is less than or equal to side c that's definitely not a triangle and we got to do for the other combination right or a plus c less than or equal to b or right b plus c less than or equal to a so this is like not a triangle if any of these is true now if this case condition here if this doesn't match that means it's a triangle so it could be either all the sides are equal equilateral or only two are equal the societies or all of them are different length scaleen so let's do the equilateral one then equilateral what's the condition for a collateral so we have to have all of the side the lengths equal so a has to be equal to b and a has to be equal to c and b has to be equal to c right so all of them are equal all the sides equal equilateral now if this case is not true it goes to the next case the when here so that means it's either two sides equal or no side length equal so in this case how can we say for the is Saucer least Saucer list case that's also this case we have to check if one combination of sides is equal so if a is equal to b or a is equal to c or b is equal to c because we already checked before previously and we are sure that's not a collateral that's when we can do this we have we can say for sure that if at least one of them two sides are equal then it's going to be I saw Salis now finally for the else case we're going to say okay we checked everything is not a collateral not as Saucer this has to be scalene all of the sides have different length so that's the outskirts there let's check that's it out I'll click run code and there you go let me click submit code okay and you can see for each row it outputs what kind of a triangle it is or not a triangle if it's not a triangle for that combination of column values for the size of the triangle and this was type of triangle
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: