0%

210304_TIL(DB_1)

오늘 배운 것

DB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use world;

# SELECT: 데이터 조회
select * from country;

select code, name, continent, population from country;

select countrycode as code, name from city;

# where : 조건으로 데이터를 검색
# 인구가 1억명이 넘는 국가의 국가코드와 국가이름, 인구수를 출력

select code, name, population from country where population >= 100000000;

# mysql 주석
-- 한줄 주석alter
/*
여러줄 주석
*/

# 연산자 : 산술, 비교, 논리
# 산술 : +. -. *, /, %, DIV(몫)

# 인구밀도
select name, surfaceArea, population, population / surfacearea as pps from country;

# 국가별 1인당 GNP 출력
select name, gnp, population, gnp / population * 1000000 as gpp from country;

# 비교 연산자
# =, !=, >, <, >=, <=
select name, population from city where population >= 800*10000;

# 논리연산자 : AND, OR
select name, population from country where population >= 800*10000 and population <= 1000*10000;
# BETWEEN : 범위 연산 시 사용
select name, population from country where population between 800*10000 and 1000*10000;

# 아시아와 아프리카가 대륙의 국가 데이터 출력
select name, continent from country where continent = 'asia' OR continent = 'africa';

# IN, NOT IN : 컬럼에 여러개의 데이터를 포함하는 데이터를 출력할 때
select name, continent from country where continent in ('asia', 'africa');

# LIKE : 특정한 문자열이 포함된 데이터를 출력
# governmentform 이 Republic 포함된 데이터를 출력
select name, governmentform from country where govermentform like "%Republic%";

# 국가 코드가 ar로 시작하는 국가의 이름을 출력
select code, name from country where code like "ar%";

# ORDER BY : 결과 데이터 정렬
# asc: 오름차순(생략 가능)
select code, name, gnp from country order by gnp asc;

# desc: 내림차순
select code, name, gnp from country order by gnp desc;

# 국가 코드 순으로 내림차순, 같은 국가 코드는 인구수 순으로 오름차순 정렬
select countrycode, name, population from city order by countrycode desc, population asc;

# LIMIT : 조회하는 데이터 수를 제한
# 인구가 많은 상위 5개 국가의 이름, 인구수 출력
select name, population from country order by population desc limit 5;

# 인구수가 상위 5위에서 8위까지 출력
select name, population from country order by population desc limit 5, 4;

# DISTINCT : 출력되는 데이터에서 중복을 제거해서 출력
select distinct continent from country

Nyong’s GitHub