Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
(SELECT CITY, LENGTH(CITY)) FROM STATION
➡STATION의 두 도시에 각각의 길이(즉, 이름에 있는 문자 수
WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) FROM STATION)
➡가장 짧은 CITY 이름을 쿼리
ORDER BY CITY LIMIT 1) ➡ 가장 먼저 오는 도시 선택을 위해
UNION ALL ➡ 짧고 긴 CITY의 이름을 합치기 위해
(SELECT CITY, LENGTH(CITY)) FROM STATION
WHERE LENGTH(CITY) = (SELECT MAX(LENGTH(CITY)) FROM STATION)
➡ 가장 긴 CITY 이름을 쿼리
ORDER BY CITY LIMIT 1) ➡ 가장 먼저 오는 도시 선택을 위해
You are given a table, BST, containing two columns: N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.
Write a query to find the node type of Binary Tree ordered by the value of the node. Output one of the following for each node:
Root: If node is root node.
Leaf: If node is leaf node.
Inner: If node is neither root nor leaf node.
✅루트 노드를 기점으로 찾고자 하는 값의 크기에 따라 왼쪽 서브 트리나 오른쪽 서브 트리로 이동하여 노드를 탐색한다.
SELECT N, ➡ N을 선택하여
CASE WHEN P IS NULL THEN 'Root' ➡ CASE문을 이용해 부모가 없으면 Root 노드
WHEN N IN (SELECT P FROM BST) THEN 'Inner'
➡ N이 부모노드를 가진다면 'Inner'
ELSE 'Leaf' END AS TREE FROM BST ➡ 제일 마지막 노드
ORDER BY 1
In company C1, the only lead manager is LM1. There are two senior managers, SM1 and SM2, under LM1. There is one manager, M1, under senior manager SM1. There are two employees, E1 and E2, under manager M1.
In company C2, the only lead manager is LM2. There is one senior manager, SM3, under LM2. There are two managers, M2 and M3, under senior manager SM3. There is one employee, E3, under manager M2, and another employee, E4, under manager, M3.
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 the company_code, founder name, total number of lead managers, total number of senior managers, total number of managers, and total number of employees. Order your output by ascending company_code.
C1사의 리드 매니저는 LM1뿐입니다.
LM1 산하에 SM1과 SM2 두 명의 시니어 매니저가 있습니다.
SM1 산하에 M1이라는 매니저가 한 명 있습니다.
M1 산하에 E1과 E2라는 두 명의 직원이 있습니다.
C2사의 리드 매니저는 LM2뿐이며, LM2 산하에 SM3라는 선임 매니저가 한 명 있습니다.
SM3 부장 밑에는 M2 부장과 M3 부장 두 명이 있으며,
M2 부장 밑에는 E3 사원 한 명과 M3 부장 밑에 E4 사원 한 명이 있습니다.
select c.company_code, c.founder,
count(distinct lm.lead_manager_code),
count(distinct sm.senior_manager_code),
count(distinct mg.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 lm.company_code = sm.company_code
left join Manager mg on sm.company_code = mg.company_code
left join Employee e on mg.company_code = e.company_code
group by c.company_code, c.founder
order by c.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 decimal places.
Input Format
The STATION table is described as follows:
STATION에서 북위도 중앙값(LAT_N)를 쿼리하고 소수점으로 답을 반올림합니다.
SELECT ROUND(MEDIAN(LAT_N), 4) FROM STATION;
You are given two tables: Students and Grades. Students contains three columns ID, Name and Marks
Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8. The report must be in descending order by grade -- i.e. higher grades are entered first. If there is more than one student with the same grade (8-10) assigned to them, order those particular students by their name alphabetically. Finally, if the grade is lower than 8, use "NULL" as their name and list them by their grades in descending order. If there is more than one student with the same grade (1-7) assigned to them, order those particular students by their marks in ascending order.
Write a query to help Eve.
SELECT CASE WHEN G.GRADE < 8 THEN NULL ➡ 8등급 미만
ELSE S.NAME END, G.GRADE, S.MARKS ➡
FROM STUDENTS AS S ➡ STUDENTS 테이블을 S로 별칭부여
JOIN GRADES AS G ➡ GRADE 테이블을 G로 별칭부여
ON S.MARKS BETWEEN G.MIN_MARK AND G.MAX_MARK ➡
ORDER BY G.GRADE DESC, S.NAME, S.MARKS ➡ 알파벳 순으로 출력
Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.
줄리아는 코딩 대회를 막 마쳤고, 그녀는 리더보드를 조립하는 데 당신의 도움이 필요합니다! 쿼리를 작성하여
두 번 이상의 도전에 대해 만점을 획득한 각 hacker_id 및 해커의 이름을 출력합니다.
해커가 전체 점수를 획득한 총 도전 수에 따라 출력을 내림차순으로 정렬합니다.
둘 이상의 해커가 동일한 수의 도전에서 만점을 받은 경우
hacker_id를 오름차순으로 정렬합니다.
SELECT s.hacker_id, h.name
FROM Submissions s
INNER JOIN Challenges c ON s.challenge_id = c.challenge_id
INNER JOIN Difficulty d ON c.difficulty_level = d.difficulty_level
INNER JOIN Hackers h ON s.hacker_id = h.hacker_id
WHERE d.score = s.score
GROUP BY s.hacker_id, h.name
HAVING count(s.challenge_id) > 1
ORDER BY count(s.challenge_id) DESC, s.hacker_id