๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿฆ• ๊ณต๋ฃก์ด ๋˜์ž!

SQL ๋ณธ๋ฌธ

Data/Database_MySQL

SQL

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;
๋ฐ˜์‘ํ˜•

'Data > Database_MySQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

SQL...2  (0) 2021.12.13
MacOS MySQL ์„ค์น˜  (0) 2021.12.09
Comments