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

쿄코코

페이지 맨 위로 올라가기

쿄코코

얼레벌레 생활🤯

HackerRank-Japanese Cities' Attributes, Japanese Cities' Names, Weather Observation Station 1, Weather Observation Station 3, Weather Observation Station 4 (COUNT(*))

  • 2022.08.28 00:47
  • SQL/HackerRank
    반응형

    Japanese Cities' Attributes📝 

    ➡️ CITY 테이블, 조건: COUNTRYCODE=  'JPN'

     

    Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN. 

    The CITY table is described as follows: 

    SELECT * FROM city WHERE countrycode='JPN';

     


    Japanese Cities' Names📝

    ➡️ CITY 테이블, 조건: COUNTRYCODE=  'JPN' , 출력 : name

     

    Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN. 
    The CITY table is described as follows: 

     

    SELECT name FROM city WHERE countrycode='JPN';

     


    Weather Observation Station 1📝

    ➡️ STATION 테이블 -> 출력 : CITY,STATE

     

    Query a list of CITY and STATE from the STATION table. 
    The STATION table is described as follows: 

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

    SELECT city,state FROM station;

     


    Weather Observation Station 3📝

    ➡️ STATION 테이블,조건: ID를 2로 나눌 수 있을 때 -> 출력 : 중복되지 않는 CITY 

     

    Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer. 
    The STATION table is described as follows:

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

    SELECT DISTINCT(city) FROM station WHERE MOD(id,2)=0;

     


     

    Weather Observation Station 4(수정)📝

     

    ➡️ STATION 테이블에서 ( 전체 CITY의 개수 - 중복되지 않는 CITY의 개수 )

     

     

    Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table. 
    The STATION table is described as follows:

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

    For example, if there are three records in the table with CITY values 'New York', 'New York', 'Bengalaru', there are 2 different city names: 'New York' and 'Bengalaru'. The query returns , because .

    SELECT COUNT(*)-COUNT(DISTINCT(city)) FROM station; -- 이렇게 할 경우 null인 경우 달라짐
    -- 이문제의 경우 null이 없어서 괜찮지만 null이 있을 경우 이렇게 하면 안 될 것 같다
    SELECT COUNT(city)-COUNT(DISTINCT(city) FROM station;

    📌 COUNT(city)-COUNT(DISTINCT(city)), COUNT(*)-COUNT(DISTINCT(city))

    COUNT(*) - null을 포함한 행의 개수

    COUNT(city) - null을 포함하지 않는 행의 개수

     

    이렇게 생긴 테이블이 있는 경우, COUNT(*)과 COUNT(city)를 할 경우 밑과 같이 COUNT(*) = 6, COUNT(city) = 4가 나오게 된다.

     

    하지만 COUNT(DISTINCT(city))를 할 경우 DISTINCT(city)는 null을 포함하여서 분류하지만 COUNT(DISTINCT(city))를 할 경우에는 null을 제외한 행의 수를 센다. 따라서 2가 나오게 된다. 

    (오른쪽)SELECT DISTINCT(city) FROM station,(왼쪽)SELECT COUNT(DISTINCT(city)) FROM station

     

    그렇기에 SELECT COUNT(city)-COUNT(DISTINCT(city)) FROM station, COUNT(*)-COUNT(DISTINCT(city)) FROM station은 이런식으로 다르게 나오게 된다.

     

    반응형

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

    Employee Salaries,Type of Traingle,The PADS,Revising Aggregations - (The Count Function,The Sum Function)  (0) 2022.09.12
    HackerRank- Weather Observation Station 10,11,12,Higher Than 75 Marks,Employee Names  (0) 2022.09.12
    HackerRank- Weather Observation Station 6,7,8,9  (0) 2022.09.04
    HackerRank - Revising the Select Query I, Revising the Select Query II, Select All, Select By ID  (0) 2022.08.23
    HackerRank SQL 정리표 🗓  (0) 2022.08.23

    댓글

    이 글 공유하기

    • 구독하기

      구독하기

    • 카카오톡

      카카오톡

    • 라인

      라인

    • 트위터

      트위터

    • Facebook

      Facebook

    • 카카오스토리

      카카오스토리

    • 밴드

      밴드

    • 네이버 블로그

      네이버 블로그

    • Pocket

      Pocket

    • Evernote

      Evernote

    다른 글

    • HackerRank- Weather Observation Station 10,11,12,Higher Than 75 Marks,Employee Names

      HackerRank- Weather Observation Station 10,11,12,Higher Than 75 Marks,Employee Names

      2022.09.12
    • HackerRank- Weather Observation Station 6,7,8,9

      HackerRank- Weather Observation Station 6,7,8,9

      2022.09.04
    • HackerRank - Revising the Select Query I, Revising the Select Query II, Select All, Select By ID

      HackerRank - Revising the Select Query I, Revising the Select Query II, Select All, Select By ID

      2022.08.23
    • HackerRank SQL 정리표 🗓

      HackerRank SQL 정리표 🗓

      2022.08.23
    다른 글 더 둘러보기

    정보

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

    쿄코코

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

    검색

    메뉴

    • 홈

    카테고리

    • 분류 전체보기 (168)
      • 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📚 (25)
        • (운영체제) - 블로그 및 강의 참고 (0)
        • 김영한👨🏻‍🏫의 스프링 부트와 JPA 실무 완전 .. (5)
        • (알고리즘)- [이코테] 이것이 코딩테스트다 정리 (10)
        • 그림으로 배우는 Http&Network Basic (10)
      • 까먹을까봐 적는 것들 (4)
      • 테스트 보고 난 후..🤔 (0)
      • kt 에이블스쿨 (18)

    최근 글

    인기 글

    댓글

    공지사항

    아카이브

    태그

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

    나의 외부 링크

    정보

    쿄코코의 쿄코코

    쿄코코

    쿄코코

    블로그 구독하기

    • 구독하기
    • RSS 피드

    방문자

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

    티스토리

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

    티스토리툴바