-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHackerRank - 2 Advanced Select.sql
40 lines (35 loc) · 2.5 KB
/
HackerRank - 2 Advanced Select.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
-- 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:
-- 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.
-- Not A Triangle: The given values of A, B, and C don't form a triangle.
SELECT CASE
WHEN A<=0 OR B <= 0 OR C <= 0
OR A+B <= C OR B+C <= A OR A+C <= B THEN 'Not A Triangle'
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B XOR B = C XOR A = C THEN 'Isosceles'
WHEN A != B AND B != C THEN 'Scalene'
END FROM triangles;
-- Generate the following two result sets:
-- Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
-- Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
-- There are a total of [occupation_count] [occupation]s.
-- where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
SELECT CONCAT(name,'(',LEFT(occupation,1),')') FROM occupations ORDER BY name;
SELECT 'There are a total of ', COUNT(OCCUPATION), CONCAT(lower(occupation),'s.') FROM OCCUPATIONS GROUP BY OCCUPATION ORDER BY COUNT(OCCUPATION), OCCUPATION;
-- Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
SET @r1=0, @r2=0, @r3=0, @r4=0;
SELECT MIN(Doctor), MIN(Professor), MIN(Singer), MIN(Actor)
from(
SELECT CASE WHEN Occupation='Doctor' THEN (@r1:=@r1+1)
WHEN Occupation='Professor' THEN (@r2:=@r2+1)
WHEN Occupation='Singer' THEN (@r3:=@r3+1)
WHEN Occupation='Actor' THEN (@r4:=@r4+1) END AS RowNumber,
CASE WHEN Occupation='Doctor' THEN Name END AS Doctor,
CASE WHEN Occupation='Professor' THEN Name END AS Professor,
CASE WHEN Occupation='Singer' THEN Name END AS Singer,
CASE WHEN Occupation='Actor' THEN Name END AS Actor
FROM OCCUPATIONS
ORDER BY Name
) Temp
GROUP BY RowNumber