Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of triangle in MySQL

Tags:

mysql

Problem statement:

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:

  • Not A Triangle: The given values of A, B, and C don't form a triangle.
  • Equilateral: It's a triangle with sides of equal length.
  • Isosceles: It's a triangle with sides of equal length.
  • Scalene: It's a triangle with sides of differing lengths. Input Format

The TRIANGLES table is described as follows:

Each row in the table denotes the lengths of each of a triangle's three sides.

Sample Input
------------
A  B  C
20 20 23
20 20 20
20 21 22
13 14 30

Sample Output
-------------
Isosceles
Equilateral
Scalene
Not A Triangle

Attempt that did not work:

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"
        when A<>B and B<>C then "Scalene"

    end as triangles_type
    from TRIANGLES;
like image 378
zathura Avatar asked Jul 25 '16 07:07

zathura


People also ask

What are the types of triangles in SQL?

SELECT CASE WHEN A = B AND B = C THEN 'Equilateral' WHEN A = B OR A = C OR B = C THEN 'Isosceles' WHEN (A + B)

What is SQL triangle?

Equilateral: It's a triangle with sides of equal length. Isosceles: It's a triangle with sides of equal length. Scalene: It's a triangle with sides of differing lengths.

What is the meaning of <> in MySQL?

The symbol <> in MySQL is same as not equal to operator (!=). Both gives the result in boolean or tinyint(1). If the condition becomes true, then the result will be 1 otherwise 0.

How many kinds of triangles are there according to angles?

Types of Triangles Based on Angles Triangles can be classified into three types with respect to their interior angles which are: Acute-angled. Obtuse-angled. Right-angled.


1 Answers

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'
    WHEN A <> B and B <> C THEN 'Scalene'
  END tuple
FROM TRIANGLES;
  1. By using a case statement, check if a given input is a triangle or not.
  2. If it is a triangle then check if all sides are same. If true the triangle type is 'Equilateral'.
  3. If not, then check if any two sides are equal. If true, the triangle type is 'Isosceles'
  4. In the case of not equal, any sides the triangle type is 'Scalene'. We can directly use ELSE also.
like image 200
Anurag garg Avatar answered Sep 19 '22 19:09

Anurag garg