CodingSpace

[HackerRank/SQL] Basic Select - Weather Observation Station 4 (feat. COUNT) 본문

HackerRank/SQL

[HackerRank/SQL] Basic Select - Weather Observation Station 4 (feat. COUNT)

개발자_조이킴 2022. 4. 23. 23:11

Problem. Basic Select - Weather Observation Station 4


Link.

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

 

Weather Observation Station 4 | HackerRank

Find the number of duplicate CITY names in STATION.

www.hackerrank.com


Description.

Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

 

STATION 테이블에서 전체 도시의 수와 중복되지 않은 도시(unique)의 수를 뺀 값을 반환하는 SQL문을 작성하시오.

예) CITY = 'New York', 'New York', 'Bengalaru'

전체 도시의 수(total number of records): 3

중복되지 않은 도시의 수(number of unique city): 2

반환 값: 1

 

STATION 테이블


Key Point. 

COUNT 함수는 특정 필드의 데이터의 수를 반환하는 함수이다.

중복된 데이터를 제거를 위해서는 DISTINCT가 사용된다.


My Answer. 

SELECT (COUNT(CITY) - COUNT(DISTINCT CITY)) 
FROM STATION;

References. 

COUNT함수: https://extbrain.tistory.com/54

 

[MySQL] 데이터 갯수 가져오기 (COUNT 함수)

▶MySQL 데이터 갯수 가져오기 (COUNT 함수) ▶설명 테이블에 존재하는 데이터 갯수를 가져오고 싶을 때가 있습니다. 이 때 사용하는 함수가 COUNT 함수입니다. COUNT 함수는 테이블에 컬럼의 데이터

extbrain.tistory.com

DISTINCT: https://extbrain.tistory.com/109

 

[MySQL] 범주 조회 (DISTINCT)

▶MySQL 범주 조회 (DISTINCT) ▶설명 MySQL에서 범주를 확인할 때 SELECT DISTINCT를 사용하는 것입니다. 하나, 예를 들어보겠습니다. 테이블에 카테고리라는 컬럼이 존재할 때, 이 카테고리 값이 테이블

extbrain.tistory.com

 

Comments