-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvanced Select.sql
84 lines (73 loc) · 2.81 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- Type of Triangle
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 B=C or A=C) then 'Isosceles'
else 'Scalene'
End as "triangle_type"
from triangles;
-- The PADS
select CONCAT_WS('',name,'(',left(occupation,1),')')
from occupations
order by name;
select CONCAT_WS("","There are a total of ",count(occupation)," ",LOWER(occupation),"s.")
from occupations
group by occupation
order by count(occupation), occupation;
-- Occupations
set @d=0, @p=0, @s=0, @a=0; -- initial counters
select min(Doctor), min(Professor), min(Singer), min(Actor)
from(
SELECT case
when Occupation='Doctor' then (@d:=@d+1)
when Occupation='Professor' then (@p:=@p+1)
when Occupation='Singer' then (@s:=@s+1)
when Occupation='Actor' then (@a:=@a+1) end as counter,
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 counter;
-- Binary Tree Nodes
select N,case
when P is NULL then 'Root'
WHEN EXISTS (SELECT B.P FROM BST B WHERE B.P = BT.N) THEN 'Inner'
else 'Leaf' end as node_type
from BST as BT order by N;
-- New Companies
--- With left join
SELECT
c.company_code,c.founder,
count(distinct lm.lead_manager_code),
count(distinct sm.senior_manager_code),
count(distinct m.manager_code),
count(distinct e.employee_code)
FROM
Company c
left join Lead_Manager lm on c.company_code=lm.company_code
left join Senior_Manager sm on sm.lead_manager_code=lm.lead_manager_code
left join Manager m on m.senior_Manager_code=sm.senior_Manager_code
left join Employee e on e.manager_code=m.manager_code
GROUP BY
c.company_code,c.founder
ORDER BY
c.company_code ASC
--- without left join
SELECT
c.company_code,c.founder,
count(distinct lm.lead_manager_code),
count(distinct sm.senior_manager_code),
count(distinct m.manager_code),
count(distinct e.employee_code)
FROM
Company c, Lead_Manager lm, Senior_Manager sm, Manager m, Employee e
WHERE
c.company_code=lm.company_code AND
lm.lead_manager_code=sm.lead_manager_code AND
sm.senior_manager_code=m.senior_manager_code AND
m.manager_code=e.manager_code
GROUP BY
c.company_code,c.founder
ORDER BY
c.company_code ASC;