SELECT de.name Department
, em.name Employee
, em.salary Salary
FROM employee em
JOIN (
SELECT departmentID, MAX(salary) max
FROM employee
GROUP BY departmentID) hi ON hi.departmentID = em.departmentID
AND hi.max = em.salary
JOIN department de ON em.departmentID = de.id
- 윈도우 함수 MAX() 이용 풀이
SELECT d.name AS Department
, s.name AS Employee
, maxsalary AS Salary
FROM(
SELECT name
, salary
, departmentID
, MAX(salary) OVER (PARTITION BY departmentID) AS maxsalary
FROM employee) s
INNER JOIN department d ON d.id = s.departmentID
WHERE s.salary = s.maxsalary;
Query analphabetically orderedlist of all names inOCCUPATIONS, 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), andASingerName(S).
Query the number of ocurrences of each occupation inOCCUPATIONS. Sort the occurrences inascending order, and output them in the following format: where[occupation_count]is the number of occurrences of an occupation inOCCUPATIONSand[occupation]is thelowercaseoccupation name. If more than oneOccupationhas the same[occupation_count], they should be ordered alphabetically.
There are a total of [occupation_count] [occupation]s.
문제정리
쿼리 두개 작성
1. 이름을 알파벳 순으로 정렬하고 이름 옆에 직업의 맨 앞 글자만 괄호쳐서 나타내는 쿼리 작성
2. 직업의 수와 직업이름을 소문자로 바꿔 결과값을
"There are a total of [occupation_count] [occupation]s." 이 문장으로 나타내는 쿼리 작성
직업수로 내림차순 정렬하되, 동일한 값이면 직업이름순 정렬
[풀이]
- SUBSTR으로 문자열 앞 글자만 자름
- 문자열 붙이는 CONCAT 함수 사용
-소문자로 바꾸는 LOWER함수 사용
SELECT CONCAT(name, '(', SUBSTR(occupation, 1, 1), ')')
FROM occupations
ORDER BY name;
SELECT CONCAT('There are a total of ', COUNT(*), ' ', LOWER(occupation),'s.')
FROM occupations
GROUP BY occupation
ORDER BY COUNT(*), occupation;
비교적 수월하게 두 쿼리를 작성했는데 계속 에러가 나서 문제가 뭔가 했더니 해결책은 ; 세미콜론 이었다.
Amber's conglomerate corporation just acquired some new companies. Each of the companies follows this hierarchy:
Given the table schemas below, write a query to print thecompany_code,foundername, total number ofleadmanagers, total number ofseniormanagers, total number ofmanagers, and total number ofemployees. Order your output by ascendingcompany_code.
Note:
The tables may contain duplicate records.
Thecompany_codeis string, so the sorting should not benumeric. For example, if thecompany_codesareC_1,C_2, andC_10, then the ascendingcompany_codeswill beC_1,C_10, andC_2.
- 각 회사코드, 창립자이름, 리드매니저 총수, 시니어 매니저 총수, 매니저 총수, 직원 총수를 프린트하는 쿼리 작성 - 회사코드로 내림차순 정렬 - 레코드는 중복될 수 있음 - 회사코드는 숫자가 아닌 문자 C_1,C_10, C_2 순서로 정렬되어야 함
[풀이]
SELECT co.company_code
,co.founder
,COUNT(DISTINCT lm.lead_manager_code)
,COUNT(DISTINCT sm.senior_manager_code)
,COUNT(DISTINCT ma.manager_code)
,COUNT(DISTINCT em.employee_code)
FROM company co
LEFT JOIN lead_manager lm ON lm.company_code = co.company_code
LEFT JOIN senior_manager sm ON sm.company_code = lm.company_code
LEFT JOIN manager ma ON ma.company_code = sm.company_code
LEFT JOIN employee em ON em.company_code = ma.company_code
GROUP BY co.company_code ,co.founder
ORDER BY co.company_code
A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to 4 decimal places.
=> LAT_N의 중앙값을 구하고 소수점 4자리까지 나타태는 쿼리 작성
[풀이]
MYSQL
- LAT_N 보다 큰 데이터 수와 LAT_N보다 작은 데이터 수가 같을 때 => 중앙값
SELECT ROUND(LAT_N,4)
FROM station s
WHERE (SELECT COUNT(*)
FROM STATION
WHERE LAT_N < s.LAT_N)
=(SELECT COUNT(*)
FROM STATION
WHERE LAT_N > s.LAT_N);
- 윈도우함수 PERCENT_RANK 사용
PERCENT_RANK 함수
: 맨 위 행을 0, 맨 아래(끝) 행을 1로 놓고
각 행에 대해 현재 행이 위치하는 백분위 순위값 반환.
반환값의 범위는 0보다 크거나 같고 1보다 작거나 같다. (0 <= x <=1)
반환 값 = (rank - 1) / (total row - 1)
SELECT ROUND(LAT_N,4)
FROM (SELECT LAT_N,
PERCENT_RANK() OVER (ORDER BY LAT_N ASC) per
FROM STATION) s2
WHERE per = 0.5
1)생년월일이 20120101 에서 20151231 사이인 쌍둥이 자녀를 가진 임직원을 구하세요.
[주어진 질의결과]
[SQL 코드]
SELECT f1.emp_no AS 직원번호
,f1.fam_nm AS 쌍둥이1
,f2.fam_nm AS 쌍둥이2
FROM
(SELECT *
FROM fam_c
WHERE birth_ymd BETWEEN '20120101' AND '20151231'
AND rel_type_cd = 'A27') f1
,fam_c f2
WHERE f1.emp_no = f2.emp_no
AND f1.birth_ymd = f2.birth_ymd
AND f1.fam_nm <> f2.fam_nm;
[제가 한 질의결과]
2) . 20210321 기준으로 재직중인 임직원이며. 생년월일이 20120101 에서 20151231인 자녀를 가진 임직원을 구하는 sql을 작성하세요.
[주어진 질의결과]
[SQL 코드]
SELECT e.emp_no AS 직원번호
,f.fam_nm AS 자녀성명
,f.birth_ymd AS 자녀생년월일
FROM emp_c e
INNER JOIN fam_c f on e.emp_no = f.emp_no
WHERE e.retire_ymd >= '20210321'
AND f.birth_ymd BETWEEN '20120101' AND '20151231'
AND f.rel_type_cd = 'A27';
[제가 한 질의결과]
3) . 20210321 기준으로 재직중인 임직원이며. 사내부부인 임직원의 직원번호를 구하는 sql을 작성하세요.
[주어진 질의결과]
[SQL 코드]
SELECT e.emp_no AS EMP_NO
FROM emp_c e
INNER JOIN fam_rel_c fam_r ON e.emp_no = fam_r.emp_no
WHERE e.retire_ymd >= '20210321'
AND fam_r.end_ymd >= '20210321'
AND fam_r.rel_type_cd IN ('A02', 'A18')
[제가 한 질의결과]
3-2) . 현재 날짜 기준 재직중인 “이씨 성"을 가진 직원의 자녀명수가 몇 명인지 리스트를 만드세요.
[SQL 코드]
SUBSTR 사용
SELECT emp.emp_no AS 직원번호
, emp.emp_nm AS 직원명
, COUNT(*) AS 자녀명수
FROM emp_c emp
INNER JOIN fam_c fam ON emp.emp_no = fam.emp_no
WHERE SUBSTR(emp.emp_nm, 1, 1) = '이'
AND SYSDATE BETWEEN HIRE_YMD AND RETIRE_YMD
AND rel_type_cd = 'A27'
GROUP BY emp.emp_no, emp.emp_nm ;
LIKE 사용
SELECT emp.emp_no AS 직원번호
, emp.emp_nm AS 직원명
, COUNT(*) AS 자녀명수
FROM emp_c emp
INNER JOIN fam_c fam ON emp.emp_no = fam.emp_no
WHERE emp.emp_nm LIKE '이%'
AND SYSDATE BETWEEN HIRE_YMD AND RETIRE_YMD
AND rel_type_cd = 'A27'
GROUP BY emp.emp_no, emp.emp_nm ;
SELECT e.emp_no AS 직원번호
,e.emp_nm AS 직원성명
,COUNT(*) AS 자녀명수
FROM emp_c e
INNER JOIN fam_c f ON e.emp_no = f.emp_no
WHERE f.rel_type_cd = 'A27'
GROUP BY e.emp_no, e.emp_nm
HAVING COUNT(*) >= 2;
[제가 한 질의결과]
3-4) . 현재 기준 조직의 부서별 직원이 몇 명인지 구하는 sql을 작성하세요. (조직코드, 조직명, 직원수)
[SQL 코드]
SELECT org.org_cd AS 조직코드
, org.org_nm AS 조직명
, COUNT(emp.emp_no) AS 직원수
FROM org_c org
INNER JOIN emp_c emp ON emp.org_cd = org.org_cd
WHERE SYSDATE BETWEEN STA_YMD AND END_YMD
GROUP BY org.org_cd, org.org_nm;