SELECT
--COUNT(id) as pokemon_cnt
FROM dataset.pokemon
WHERE
--type2 IS NULL
WHERE 절에서 여러 조건을 연결하고 싶은 경우에 사용한다
AND 조건 : A AND B
OR 조건 : (A) OR (B)
SELECT
--type1,
--COUNT(id) AS type1_cnt
FROM dataset.pokemon
WHERE
--type2 is null
--AND type1 = "Fire"
SELECT
--type1,
--COUNT(id) AS type1_cnt
FROM dataset.pokemon
WHERE
--(type2 is null)
--OR (type1 = "Fire")
WHERE 절에서 특정 단어가 포함된 row를 추출할 때 사용한다
주로 문자열 컬럼에 많이 적용한다
컬럼 LIKE "특정 단어%"
"%a" : a로 끝나는 단어, "a%" : a로 시작하는 단어, "%a%" : a가 들어간 단어
SELECT
--kor_name
FROM dataset.pokemon
WHERE
--kor_name LIKE "%파%"
=SELECT
--*
FROM dataset.trainer
WHERE
--(name = "Iris)
--OR (name = "Whitney")
--OR (name = "Cynthia")
SELECT
--*
FROM dataset.trainer
WHERE
--name IN ("Iris", "Whitney", "Cynthia")
COUNT(DISTINCT user_id) AS dau
조건이 들어간 컬럼의 수를 집계할 때 사용한다
연습문제
트레이너 별로 풀어준 포켓몬의 비율이 20%가 넘는 포켓몬 트레이너는 누구인지 알 수 있는 쿼리를 작성하시오.
풀어준 포켓몬의 비율=(풀어준 포켓몬 수/전체 포켓몬의 수)
/테이블: trainer_pokemon, 조건: 풀어준 포켓몬의 비율이 20%가 넘어야 한다, 컬럼: trainer_id, 집계: COUNTIF/
SELECT
--trainer_id,
--COUNTIF(status="Released") as released_cnt,
--COUNT(pokemon_id) as pokemon_cnt,
--COUNTIF(status="Released")/COUNT(pokemon_id) as released_ratio
FROM basic.trainer_pokemon
GROUP BY
--trainer_id
HAVING
--released_ratio >= 0.2