☑️ Weather Observation Station 3
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.
SELECT DISTINCT(CITY) FROM STATION WHERE ID % 2 = 0
ㄴ id 번호가 짝수인 도시 추출
ㄴ 2로 나눠서 나머지가 0인 것 (짝수) 추출
ㄴ mod 함수 - n을 m으로 나눈 나머지 반환
- SELECT MOD(N, M);
- SELECT N MOD M;
- SELECT N % m;
☑️ Weather Observation Station 7
Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.
<내가 처음 작성한 답안> select distinct(city) from station where city like in ('%a','%e','%i','%o','%u') <정답> select distinct(city) from station where city like '%a' or city like '%e' or city like '%i' or city like '%o' or city like '%u' <정규표현식 사용> select distinct city from station where city REGEXP '.*[aeiou]$'
ㄴ 특정문자 포함 LIKE '%문자%'
☑️ Weather Observation Station 8
Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.
(이 문제는 A로 시작하고 A로 끝나는 것만 출력해야 한다면 LIKE'A%A'라고 쓸 수 있을 것.
하지만 여기선 A로 시작했지만 끝나는 것은 A외에 I,E,O,U 중에 어떤 것으로 끝나든 상관 없다.
이런 식으로 경우의 수가 너무 많은 경우 LIKE연산자를 쓰기 어렵다..)
SELECT DISTINCT CITY FROM STATION WHERE LEFT(CITY,1) IN ('A','E','I','O','U') AND RIGHT(CITY,1)IN ('A','E','I','O','U') ㄴ CITY의 가장 첫번째 글자를 LEFT함수로 떼어내고, 모음이 해당하는지를 IN연산자로 검사 ㄴ 마찬가지로 가장 마지막 글자는 RIGHT함수로 떼어내고 IN 연산자로 검사 [정규표현식] select distinct city from station where city REGEXP '^[aeiou].*[aeiout]$'
☑️ Population Census
Given the CITY and COUNTRY tables,
query the sum of the populations of all cities where the CONTINENT is 'Asia'.
<내가 작성한 답안> SELECT SUM(POPULATION) FROM CITY INNER JOIN COUNTRY ON CITY.COUNTRYCODE = COUNTRY.Code GROUP BY CITY.POPULATION WHERE CITY.CONTINENT = 'Asia' <정답> SELECT SUM(CITY.POPULATION) FROM CITY INNER JOIN COUNTRY ON CITY.COUNTRYCODE = COUNTRY.Code WHERE COUNTRY.CONTINENT = 'Asia'
ㄴ 쿼리 순서
1) SELECT - FROM - WHERE - GROUP BY : 조건 처리 후 컬럼 그룹화
2) SELECT - FROM - GROUP BY - HAVING : 컬럼 그룹화 후 조건 처리
3) SELECT - FROM - WHERE - GROUP BY - HAVING : 조건 처리 후 컬럼 그룹화 후 조건 처리
4) SELECT - FROM - WHERE - GROUP BY - HAVING - ORDER BY
ㄴ GROUP BY
ㄴ 조건절(HAVING) 사용