Kirok Kim
2021. 12. 10. 22:24
Select ์ปฌ๋ผ from ์คํค๋ง.ํ
์ด๋ธ
where population between 1000000 and 2000000
์ซ์ ์ฐ์ฐ์
= '! = ' <> > <
- null ๊ฐ์ =๋ก ์ธ์์ด ๋ถ๊ฐํ๊ณ is null๋ก ํด์ค์ผํจ
- ์์
select*from world.country
where inDepyear is null;
๋ฌธ์์ฐ์ฐ์
select count(*)from city where name LIKE'___'
select count(*)from city where name LIKE'%m'
- %๋ ์์ ๋ฌธ์๊ฐ ์ ํ์์ด _๋ ํ ๊ฐ
์๋ธ์ฟผ๋ฆฌ
select *
from city
where population>(select population from city where name ='Seoul');
select *
from city
where population > ANY ( select *
from city
where CountryCode ='Kor');
- ANY ์ ์๋ธ ์ฟผ๋ฆฌ ๊ฐ์ ์ ์ผ ์์ ๊ฐ๋ณด๋ค ์ปค์ผํจ
select name
from city
where Population =(select avg(population)
from city);
- foreign key
- ํ์ ๋ ๋์ผํด์ผํ๋ค
- distinct ์ค๋ณต์ ์ธ
select distinct CountryCode
from city;
select count(countrycode)/count(distinct countrycode)
from city;
- count ๋ด๋ถ์ ์์ฑ์ ํด์ผ ์ค๋ณต์ด ์๋๋ค.
- group by
select countrycode, count(name),sum(population)
from city
group by countrycode;
select countrycode, count(name),sum(population)
from city
group by countrycode;
- ๋ณด์ฌ์ค ๊ฒ๋ค์ select๋ก ์ ์ group by๋ ๊ธฐ์ค์ด ๋ ๊ฒ ์ ์
- group by๋ where์ ๋ชป์ ๋์ having ์ ์ ์ด๋ค
- having ์
select countrycode as '๊ตญ๊ฐ์ฝ๋',
count(name) as '๋ฑ๋ก๋์์',
sum(population) as '์ด์ธ๊ตฌ์',
avg(population) as 'ํ๊ท ์ธ๊ตฌ์',
district
from city
group by CountryCode
Having Sum(population)>1000000
order by count(name) DESC,sum(population);
- ๋ฌด์กฐ๊ฑด group by ์ ๋ค์ ๋์์ผํ๋ค.
- order by
select countrycode as '๊ตญ๊ฐ์ฝ๋',
count(name) as '๋ฑ๋ก๋์์',
sum(population) as '์ด์ธ๊ตฌ์',
avg(population) as 'ํ๊ท ์ธ๊ตฌ์'
from city
group by CountryCode
order by count(name),sum(popualation);
-- DESC(๋ด๋ฆผ์ฐจ์)
- ์ ๋ ฌ ๊ธฐ์ค
- ์คํํ๋ ๋ช ๋ น์ด?
-
count(*) sum() avg() min() max() AS
์ฐ์ ์ด๋ฏธ์ง๋ฅผ ๋ณด๊ณ ํ๋ฉด ์ดํด๊ฐ ์ฝ๋ค.
- Join
select*from sales
INNER JOIN member
on sales.mem_id = member.id;
select sales.*,member.name,member.addr from sales
inner join member
on sales.mem_id = member.id;
- inner join ์ฐ๊ฒฐ(๊ต์งํฉ)
select distinct actor.first_name
from film_actor
inner join actor
on film_actor.actor_id=actor.actor_id;
select distinct film.title
from film_actor
inner join film
on film_actor.film_id=film.film_id;
select film.title,actor.first_name
from film_actor
inner join film
on film_actor.film_id=film.film_id
inner join actor
on film_actor.actor_id=actor.actor_id;
- outer join
SELECT * FROM hellodb.sales;
select member.*, sales.*
from member
left outer join sales
on member.id = sales.mem_id;
select film.*
from film_actor
right outer join film
on film.film_id=film_actor.film_id
where film_actor.actor_id is null;
๋ฐ์ํ