이 영역을 누르면 첫 페이지로 이동
쿄코코 블로그의 첫 페이지로 이동

쿄코코

페이지 맨 위로 올라가기

쿄코코

얼레벌레 생활🤯

African Cities, Average Population of Each Continent, Weather Observation Station 5, Binary Tree Nodes, New Companies

  • 2022.10.31 02:13
  • SQL/HackerRank
    반응형

     

    African Cities 📝 

    https://www.hackerrank.com/challenges/african-cities/problem?isFullScreen=true 

    ➡️ city,country 테이블 

    • 출력 : name 출력 (city.name or country.name)
    • 조건 1 : 테이블 JOIN city.countrycode = country.code
    • 조건 2 : continent = 'Africa'
     

    Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'. 

    Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

    Input Format

    The CITY and COUNTRY tables are described as follows:

    SELECT city.name
    FROM city JOIN country ON city.countrycode = country.code
    WHERE continent = 'Africa';

     


     

    Average Population of Each Continent 📝 

    https://www.hackerrank.com/challenges/average-population-of-each-continent/problem?isFullScreen=true 

    ➡️ city,country 테이블 

    • 출력 : counrty.continent,city.population 평균(정수로 버림)
    • 조건 1 : 테이블 JOIN city.countrycode = country.code
    • 조건 2 : country.continent로 group
     

    Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.

    Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

    Input Format

    The CITY and COUNTRY tables are described as follows:

    SELECT country.continent,FLOOR(AVG(city.population))
    FROM city JOIN country ON city.countrycode = country.code
    GROUP BY country.continent;

     


     

    Weather Observation Station 5📝

     

    https://www.hackerrank.com/challenges/weather-observation-station-5/problem?isFullScreen=true 

    ➡️ station 테이블 

    • 출력 : city 길이 중 가장 짧은 것 + 가장 긴 것
    • 조건 1 : 동일한 city 길이를 가질 경우 알파벳으로 정렬해서 제일 위에 있는 것 추출

     

    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. 
    The STATION table is described as follows:

    where LAT_N is the northern latitude and LONG_W is the western longitude.

    Sample Input

    For example, CITY has four entries: DEF, ABC, PQRS and WXY.

    Sample Output

    ABC 3
    PQRS 4
    

    Explanation

    When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with lengths  and . The longest name is PQRS, but there are  options for shortest named city. Choose ABC, because it comes first alphabetically.

    Note 
    You can write two separate queries to get the desired output. It need not be a single query.

    -- SQL
    SELECT city,LENGTH(city) FROM STATION ORDER BY LENGTH(city),city LIMIT 1;
    SELECT city,LENGTH(city) FROM STATION ORDER BY LENGTH(city) DESC,city LIMIT 1;
    -- 오라클
    SELECT * FROM (SELECT city,LENGTH(city) FROM STATION ORDER BY LENGTH(city),city) WHERE ROWNUM<=1;
    SELECT * FROM (SELECT city,LENGTH(city) FROM STATION ORDER BY LENGTH(city) DESC,city) WHERE ROWNUM<=1;

     


     

    Binary Tree Nodes 📝 

    https://www.hackerrank.com/challenges/binary-search-tree-1/problem?isFullScreen=true 

    ➡️ bst 테이블 

    • 첫번쨰로 Root와 (Leaf & Inner) 둘을 나누는 기준 : b.p null인 경우 -> Root, b.p가 Null이 아닌 경우 -> Leaf & Inner
    • 두번째로 Leaf와 Inner 둘을 나누는 기준
         b.n = p인 값들의 개수 = 0 -> b.n이 Inner가 될 수 없고 Leaf. 
         b.n = p인 값들의 개수 0이아닌 경우 -> Inner
     

    You are given a table, BST, containing two columns: N and P, where Nrepresents 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.

    Sample Input

    Sample Output

    1 Leaf
    2 Inner
    3 Leaf
    5 Root
    6 Leaf
    8 Inner
    9 Leaf
    


    Explanation

    The Binary Tree below illustrates the sample:

    SELECT b.n,
    CASE
        WHEN b.p IS NOT null THEN
            CASE
                WHEN (SELECT COUNT(*) FROM bst WHERE p = b.n)=0 THEN 'Leaf'
                ELSE 'Inner'
            END
        ELSE
            'Root'
    END
    FROM bst b
    ORDER BY b.n;

     


     

    New Companies 📝 

    https://www.hackerrank.com/challenges/the-company/problem?isFullScreen=true 

    ➡️ company,lead_manger,senior_manager, manager, employee 테이블

    • company,lead_manger,senior_manger,mangager,employee 테이블 INNER JOIN 
    • company_code,founder로 group 지은 후 직원들의 수를 count
    • 조건 : order by company_code (데이터타입 : string ) -> C1,C10,C100 이런식으로 오름차순 정리
     
     

    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 seniormanagers, total number of managers, and total number of employees. Order your output by ascending company_code.

    Note:

    • The tables may contain duplicate records.
    • The company_code is string, so the sorting should not be numeric. For example, if the company_codes are C_1, C_2, and C_10, then the ascending company_codes will be C_1, C_10, and C_2.
     

    Input Format

    The following tables contain company data:

    • Company: The company_code is the code of the company and founder is the founder of the company. 
    • Lead_Manager: The lead_manager_code is the code of the lead manager, and the company_code is the code of the working company.
    •  
    • Senior_Manager: The senior_manager_code is the code of the senior manager, the lead_manager_code is the code of its lead manager, and the company_code is the code of the working company. 
    • Manager: The manager_code is the code of the manager, the senior_manager_code is the code of its senior manager, the lead_manager_code is the code of its lead manager, and the company_code is the code of the working company.
    •  
    • Employee: The employee_code is the code of the employee, the manager_code is the code of its manager, the senior_manager_code is the code of its senior manager, the lead_manager_code is the code of its lead manager, and the company_code is the code of the working company.
    •  
     

    Sample Input

    Company Table: 

     Lead_Manager Table: 

     Senior_Manager Table: 

    Manager Table: 

    Employee Table: 

     

    Sample Output

    C1 Monika 1 2 1 2
    C2 Samantha 1 1 2 2
    

    Explanation

    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.

    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
        LEFT JOIN lead_manager l ON l.company_code = c.company_code
        LEFT JOIN senior_manager s ON s.lead_manager_code = l.lead_manager_code
        LEFT JOIN manager m ON m.senior_manager_code = s.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;

     

     

    반응형

    'SQL > HackerRank' 카테고리의 다른 글

    Ollivander's Inventory, Challenges,Contest Leaderboard  (0) 2022.11.22
    African Cities, The Report, Top Competitors  (0) 2022.11.15
    Weather Observation Station 16,17,18,19 | Population Census  (0) 2022.10.24
    HackerRank - Top Earners,Weather Observation Station 2, 13, 14, 15  (1) 2022.10.03
    Revising Aggregations - Averages,Average Population,Japan Population,Population Density Difference,The Blunder  (0) 2022.09.17

    댓글

    이 글 공유하기

    • 구독하기

      구독하기

    • 카카오톡

      카카오톡

    • 라인

      라인

    • 트위터

      트위터

    • Facebook

      Facebook

    • 카카오스토리

      카카오스토리

    • 밴드

      밴드

    • 네이버 블로그

      네이버 블로그

    • Pocket

      Pocket

    • Evernote

      Evernote

    다른 글

    • Ollivander's Inventory, Challenges,Contest Leaderboard

      Ollivander's Inventory, Challenges,Contest Leaderboard

      2022.11.22
    • African Cities, The Report, Top Competitors

      African Cities, The Report, Top Competitors

      2022.11.15
    • Weather Observation Station 16,17,18,19 | Population Census

      Weather Observation Station 16,17,18,19 | Population Census

      2022.10.24
    • HackerRank - Top Earners,Weather Observation Station 2, 13, 14, 15

      HackerRank - Top Earners,Weather Observation Station 2, 13, 14, 15

      2022.10.03
    다른 글 더 둘러보기

    정보

    쿄코코 블로그의 첫 페이지로 이동

    쿄코코

    • 쿄코코의 첫 페이지로 이동

    검색

    메뉴

    • 홈

    카테고리

    • 분류 전체보기 (172)
      • Python (24)
        • 😈 99클럽 코테 스터디 4기 TIL (23)
        • 궁금한거 정리 (1)
      • SQL (16)
        • HackerRank (15)
      • [백준] Python,Java로 풀기📖 (71)
        • 정렬(Sorting) (6)
        • 그리디 (5)
        • 문자열 (7)
        • 수학 (3)
        • DFS&BFS (10)
        • 구현 (4)
        • 다이나믹 (17)
        • 이분탐색 (1)
        • 자료구조 (10)
        • 최단거리 (5)
        • 인덱스트리 (0)
      • [프로그래머스]Python,Java로 풀기 (6)
        • Level 1 (4)
        • Level 2 (2)
      • Study Platform📚 (28)
        • 김영한👨🏻‍🏫의 스프링 부트와 JPA 실무 완전 .. (5)
        • (알고리즘)- [이코테] 이것이 코딩테스트다 정리 (10)
        • 그림으로 배우는 Http&Network Basic (10)
        • AWS SAA C03공부하기 (3)
      • 까먹을까봐 적는 것들 (5)
      • 테스트 보고 난 후..🤔 (0)
      • kt 에이블스쿨 (18)

    최근 글

    인기 글

    댓글

    공지사항

    아카이브

    태그

    • 티스토리챌린지
    • 백준
    • 항해99
    • 99클럽
    • 프로그래머스
    • 오블완
    • 코딩테스트준비
    • TiL

    나의 외부 링크

    정보

    쿄코코의 쿄코코

    쿄코코

    쿄코코

    블로그 구독하기

    • 구독하기
    • RSS 피드

    방문자

    • 전체 방문자
    • 오늘
    • 어제

    티스토리

    • 티스토리 홈
    • 이 블로그 관리하기
    • 글쓰기
    Powered by Tistory / Kakao. © 쿄코코. Designed by Fraccino.

    티스토리툴바