테이블로부터 회사 코드 | 설립자 | Lead Manager 총원 | Senior Manager 총원 | Manager 총원 | Employee 총원 을 계산한 테이블을 출력하고, 회사 코드 기준으로 오름차순 정렬
풀이
1. Employee 테이블에 설립자를 제외한 각 직급별 데이터가 다 나와 있으므로, Employee 테이블과 Company 테이블만 JOIN하여 COUNT
SELECT
C.company_code,
C.founder,
COUNT(DISTINCT E.lead_manager_code),
COUNT(DISTINCT E.senior_manager_code),
COUNT(DISTINCT E.manager_code),
COUNT(DISTINCT E.employee_code)
FROM Company C
LEFTJOIN Employee E ON C.company_code = E.company_code
GROUPBY
C.company_code,
C.founder
ORDERBY C.company_code
-- 출력
C1 Angela 12513
C10 Earl 1123
C100 Aaron 12410
C11 Robert 1111
C12 Amy 12614
C13 Pamela 12514
C14 Maria 1135
C15 Joe 1123
C16 Linda 1135
C17 Melissa 1237
C18 Carol 1256
C19 Paula 1247
C2 Frank 1113
C20 Marilyn 1122
C21 Jennifer 1137
C22 Harry 1136
C23 David 1112
C24 Julia 1126
C25 Kevin 1125
C26 Paul 1113
C27 James 1113
C28 Kelly 1259
C29 Robin 1249
C3 Patrick 1225
C30 Ralph 1125
C31 Gloria 1113
C32 Victor 1248
C33 David 12512
C34 Joyce 12610
C35 Donna 12612
C36 Michelle 12511
C37 Stephanie 1125
C38 Gerald 1246
C39 Walter 1137
C4 Lisa 1111
C40 Christina 1136
C41 Brandon 1237
C42 Elizabeth 1248
C43 Joseph 1246
C44 Lawrence 1134
C45 Marilyn 1113
C46 Lori 1239
C47 Matthew 1234
C48 Jesse 1133
C49 John 1138
C5 Kimberly 1239
C50 Martha 1125
C51 Timothy 12512
C52 Christine 1122
C53 Anthony 1111
C54 Paula 1247
C55 Kimberly 1223
C56 Louise 1113
C57 Martin 1125
C58 Paul 1248
C59 Antonio 1124
C6 Bonnie 1126
C60 Jacqueline 1112
C61 Diana 1111
C62 John 12511
C63 Dorothy 1257
C64 Evelyn 1112
C65 Phillip 1248
C66 Evelyn 12411
C67 Debra 1113
C68 David 1259
C69 Willie 1113
C7 Michael 1112
C70 Brandon 1247
C71 Ann 12510
C72 Emily 1237
C73 Dorothy 1112
C74 Jonathan 1247
C75 Dorothy 1124
C76 Marilyn 12512
C77 Norma 12510
C78 Nancy 1237
C79 Andrew 1122
C8 Todd 1113
C80 Keith 1112
C81 Benjamin 1139
C82 Charles 1123
C83 Alan 1234
C84 Tammy 1113
C85 Anna 1248
C86 James 1135
C87 Robin 1235
C88 Jean 1123
C89 Andrew 1247
C9 Joe 1136
C90 Roy 1123
C91 Diana 1222
C92 Christina 1113
C93 Jesse 1122
C94 Joyce 12513
C95 Patricia 1135
C96 Gregory 1122
C97 Brian 1111
C98 Christine 1125
C99 Lillian 1126
2. Employee 테이블에는 Lead Manager나 Senior Manager, Manager 중 하위 직급이 없는 사람에 대한 정보가 없으므로 완벽한 정보를 가지지 않을 수 있으므로, 상위 직급의 테이블도 연쇄적으로 JOIN하여 누락을 방지할 필요가 있음
SELECT
C.company_code,
C.founder,
COUNT(DISTINCT L.lead_manager_code),
COUNT(DISTINCT S.senior_manager_code),
COUNT(DISTINCT M.manager_code),
COUNT(DISTINCT E.employee_code)
FROM Company C
LEFTJOIN Lead_Manager L ON C.company_code = L.company_code
LEFTJOIN Senior_Manager S ON L.company_code = S.company_code
LEFTJOIN Manager M ON L.company_code = M.company_code
LEFTJOIN Employee E ON L.company_code = E.company_code
GROUPBY
C.company_code,
C.founder
ORDERBY C.company_code
-- 출력
C1 Angela 12513
C10 Earl 1123
C100 Aaron 12410
C11 Robert 1111
C12 Amy 12614
C13 Pamela 12514
C14 Maria 1135
C15 Joe 1123
C16 Linda 1135
C17 Melissa 1237
C18 Carol 1256
C19 Paula 1247
C2 Frank 1113
C20 Marilyn 1122
C21 Jennifer 1137
C22 Harry 1136
C23 David 1112
C24 Julia 1126
C25 Kevin 1125
C26 Paul 1113
C27 James 1113
C28 Kelly 1259
C29 Robin 1249
C3 Patrick 1225
C30 Ralph 1125
C31 Gloria 1113
C32 Victor 1248
C33 David 12512
C34 Joyce 12610
C35 Donna 12612
C36 Michelle 12511
C37 Stephanie 1125
C38 Gerald 1246
C39 Walter 1137
C4 Lisa 1111
C40 Christina 1136
C41 Brandon 1237
C42 Elizabeth 1248
C43 Joseph 1246
C44 Lawrence 1134
C45 Marilyn 1113
C46 Lori 1239
C47 Matthew 1234
C48 Jesse 1133
C49 John 1138
C5 Kimberly 1239
C50 Martha 1125
C51 Timothy 12512
C52 Christine 1122
C53 Anthony 1111
C54 Paula 1247
C55 Kimberly 1223
C56 Louise 1113
C57 Martin 1125
C58 Paul 1248
C59 Antonio 1124
C6 Bonnie 1126
C60 Jacqueline 1112
C61 Diana 1111
C62 John 12511
C63 Dorothy 1257
C64 Evelyn 1112
C65 Phillip 1248
C66 Evelyn 12411
C67 Debra 1113
C68 David 1259
C69 Willie 1113
C7 Michael 1112
C70 Brandon 1247
C71 Ann 12510
C72 Emily 1237
C73 Dorothy 1112
C74 Jonathan 1247
C75 Dorothy 1124
C76 Marilyn 12512
C77 Norma 12510
C78 Nancy 1237
C79 Andrew 1122
C8 Todd 1113
C80 Keith 1112
C81 Benjamin 1139
C82 Charles 1123
C83 Alan 1234
C84 Tammy 1113
C85 Anna 1248
C86 James 1135
C87 Robin 1235
C88 Jean 1123
C89 Andrew 1247
C9 Joe 1136
C90 Roy 1123
C91 Diana 1222
C92 Christina 1113
C93 Jesse 1122
C94 Joyce 12513
C95 Patricia 1135
C96 Gregory 1122
C97 Brian 1111
C98 Christine 1125
C99 Lillian 1126
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
풀이
1. 직업 별 이름들을 서브쿼리로 추출해서 JOIN
- row_number() over(ORDER BY Name) 열 추가: 이름 오름차순 정렬 + JOIN 기준으로 하여, 빈 셀에 Null 삽입
- JOIN 방향은 이름 개수가 가장 많은 Professor 열 기준: 다른 열 기준 시 Professor의 이름이 잘리기 때문
SELECT d.Name, p.Name, s.Name, a.Name
FROM
(SELECT Name, row_number() over(ORDERBY Name) as r
FROM OCCUPATIONS
WHERE Occupation ='Doctor') AS d
RIGHTJOIN
(SELECT Name, row_number() over(ORDERBY Name) as r
FROM OCCUPATIONS
WHERE Occupation ='Professor') AS p
ON d.r = p.r
LEFTJOIN
(SELECT Name, row_number() over(ORDERBY Name) as r
FROM OCCUPATIONS
WHERE Occupation ='Singer') AS s
ON p.r = s.r
LEFTJOIN
(SELECT Name, row_number() over(ORDERBY Name) as r
FROM OCCUPATIONS
WHERE Occupation ='Actor') AS a
ON s.r = a.r
-- 출력
Aamina Ashley Christeen Eve
Julia Belvet Jane Jennifer
Priya Britney Jenny Ketty
NULL Maria Kristeen Samantha
NULL Meera NULLNULLNULL Naomi NULLNULLNULL Priyanka NULLNULL
2. 각 직업의 수를 세서 "There are a total of 3 doctors"와 같이 출력(직업 count 오름차순, 직업명 오름차순)
Sample Output
Ashely(P)
Christeen(P)
Jane(A)
Jenny(D)
Julia(A)
Ketty(P)
Maria(A)
Meera(S)
Priya(S)
Samantha(D)
There are a total of 2 doctors.
There are a total of 2 singers.
There are a total of 3 actors.
There are a total of 3 professors.
2. 각 직업의 수를 세서 "There are a total of 3 doctors"를 직업 수 오름차순, 직업명 오름차순으로 출력
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM OCCUPATIONS
GROUPBY Occupation
ORDERBYCOUNT(Occupation), Occupation;
-- 출력
There are a total of3 doctors.
There are a total of4 actors.
There are a total of4 singers.
There are a total of7 professors.
3. 두 쿼리를 UNION으로 결합하여 한 줄로 출력하려고 했지만 MySQL에서 UNION을 사용할 때 ORDER BY가 적용되지 않아 오류 발생
SELECT CONCAT(Name, '(', SUBSTR(Occupation,1,1),')')
FROM OCCUPATIONS
ORDERBY Name
UNIONSELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM OCCUPATIONS
GROUPBY Occupation
ORDERBYCOUNT(Occupation), Occupation
-- 출력-- ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual-- that corresponds to your MySQL server version for the right syntax to use near 'UNION-- SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occup' at line 4
4. 각 쿼리를 서브쿼리에 넣어 UNION 시도했지만 MySQL에서 UNION하면 ORDER BY로 한 정렬이 풀려버림
SELECT a.*FROM (SELECT CONCAT(Name, '(', SUBSTR(Occupation,1,1),')')
FROM OCCUPATIONS
ORDERBY Name) AS a
UNIONSELECT b.*FROM (SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM OCCUPATIONS
GROUPBY Occupation
ORDERBYCOUNT(Occupation), Occupation) AS b
-- 출력
Ashley(P)
Samantha(A)
Julia(D)
Britney(P)
Maria(P)
Meera(P)
Priya(D)
Priyanka(P)
Jennifer(A)
Ketty(A)
Belvet(P)
Naomi(P)
Jane(S)
Jenny(S)
Kristeen(S)
Christeen(S)
Eve(A)
Aamina(D)
There are a total of3 doctors.
There are a total of4 actors.
There are a total of4 singers.
There are a total of7 professors.
정답
5. 두 쿼리를 UNION으로 묶지않고 ";"로 각 쿼리를 끝내 두 번 출력하는 방법으로 변경
SELECT CONCAT(Name, '(', SUBSTR(Occupation,1,1),')')
FROM OCCUPATIONS
ORDERBY Name;
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM OCCUPATIONS
GROUPBY Occupation
ORDERBYCOUNT(Occupation), Occupation;
-- 출력
Aamina(D)
Ashley(P)
Belvet(P)
Britney(P)
Christeen(S)
Eve(A)
Jane(S)
Jennifer(A)
Jenny(S)
Julia(D)
Ketty(A)
Kristeen(S)
Maria(P)
Meera(P)
Naomi(P)
Priya(D)
Priyanka(P)
Samantha(A)
There are a total of3 doctors.
There are a total of4 actors.
There are a total of4 singers.
There are a total of7 professors.