limit ์ซ์
๐ ํ ์ด๋ธ์์ ๋ฐ์ดํฐ '์ซ์' ๊ฐ์ ๋งํผ๋ง ์๋ผ์ ๋ณด์ฌ์ค.
select * from checkins
limit 5
max(ํ๋๋ช ) / min(ํ๋๋ช )
๐ ์ต๋๊ฐ ๊ตฌํ๊ธฐ / ์ต์๊ฐ ๊ตฌํ๊ธฐ.
select week, min(likes) from checkins
group by week
avg(ํ๋๋ช )
๐ ํ๊ท ๊ตฌํ๊ธฐ.
select week,avg(likes) from checkins
group by week
round( , ์ซ์)
๐ ์์ซ์ "์ซ์" ์๋ฆฌ ๊น์ง๋ง ๋์ค๊ฒ ๋ฐ์ฌ๋ฆผ ํด์ค
์ ) round ๊ดํธ ๋์ ์ ํ ์ซ์๊ฐ 2๋ผ๋ฉด, ์์ซ์ 2์งธ ์๋ฆฌ ๊น์ง๋ง ๋์ค๊ฒ ํด์ค.
select week, round(avg(likes),3) from checkins
group by week
count(ํ๋๋ช )
๐ ํด๋น ํ๋์ ๊ฐ์ ๊ตฌํ๊ธฐ. *๋ก ํ๋ฉด ์ ์ฒด ๊ฐ์ ๊ตฌํ๋ ๊ฒ.
๐ count๋ NULL์ ์ธ์ง ์๋๋ค
select week, count(likes) from checkins
group by week
sum(ํ๋๋ช )
๐ ํ๋ ์์ ๋ฐ์ดํฐ๋ค ๋ค ๋ํด์ค. ํฉ๊ณ ๊ตฌํ๊ธฐ.
select week, sum(likes) from checkins
group by week
group by ํ๋์ด๋ฆ
๐ ๋์ผ ํ๋ ๋ด์ ๋ฐ์ดํฐ๋ฅผ ํ๋๋ก ๋ฌถ์ ์ ์์.
name ํ๋์ ๋ฐ์ดํฐ๊ฐ ์ด** ๊น** ์์ผ๋ก ์ ์ฅ๋์ด ์๋ค๋ฉด, group by๋ฅผ ํตํด ๊ฐ์ name์ ๊ฐ๋ ๋ฐ์ดํฐ๋ฅผ ํฉ์นจ.
select * from users u
group by name
order by ํ๋์ด๋ฆ
๐ ๋ฐ์ดํฐ ์ ๋ ฌํ๊ณ ์ ํ ๋ ์. ๋ํดํธ๋ ์ค๋ฆ์ฐจ์. asc
๐ ๋ด๋ฆผ์ฐจ์ desc
๐ ์ซ์, ๋ฌธ์์ด, ์๊ฐ๋ ์ ๋ ฌ์ด ๊ฐ๋ฅํจ
select name, count(*) from users
group by name
order by count(*)desc
์ ๋ ฌ์ ์ธ์ ๋ ๋ง์ง๋ง์ ํ์. ์ด์ฐจํผ ๋งจ ๋ง์ง๋ง์ ์คํ๋จ.
order by ์กฐ๊ฑด์ ์ค๋ณต / group by ์กฐ๊ฑด์ ์ค๋ณต
๐ ์ฌ๋ฌ๊ฐ ์ฐ๊ณ ์ถ์ผ๋ฉด , ์ด์ฉํ๋ฉด ๋๋ค.
select * from orders o
group by payment_method , course_id
order by course_id , payment_method desc
where ์กฐ๊ฑด
๐ ์ํ๋ ์กฐ๊ฑด์ ํด๋นํ๋ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ฌ ์ ์๋ ์์ฃผ ๋๋ํ ์นญ๊ตฌ.
select payment_method, count(*) from orders
where course_title = "์น๊ฐ๋ฐ ์ข
ํฉ๋ฐ"
group by payment_method;
where (ํ๋๋ช ) like '% gmail %'
select name, count(*) from users
where email like '%gmail.com'
group by name;
where (ํ๋๋ช ) between '์ธ์ ๋ถํฐ' and '์ด๊ฑฐ ์ ๊น์ง'
๐ created_at ๋ ์ง๊ฐ 2020-08-01 ๋ถํฐ ~ 2020-10-10 ์ ๊น์ง ํด๋นํ๋ ์๋ฃ๋ง ๊ฐ์ ธ์ ๋ฌ๋ผ๋ ๋ง์ด๋ค.
select * from orders o
where created_at between '2020-08-01' and '2020-10-10'
where ์กฐ๊ฑด or ์กฐ๊ฑด
๐ ๋์ค ํ๋๋ผ๋ ์กฐ๊ฑด์ ํด๋นํ๋ค๋ฉด ๋ณด์ฌ๋ฌ๋ผ๋ ์๋ฏธ์ด๋ค.
select * from orders o
where (created_at between '2020-08-01' and '2020-10-10') or
(created_at between '2020-07-01' and '2020-07-15')
where ํ๋ in (์กฐ๊ฑด)
select * from users
where name in ('์ **', '์ด**')
๐ ํ๋๋ผ๋ ์กฐ๊ฑด์ ํด๋นํ๋ค๋ฉด ๋ณด์ฌ๋ฌ๋ผ๋ ์๋ฏธ์ด๋ค.
๐ or ์ ๊ฐ์ ์ญํ ํจ.
๐ ๊ดํธ ์์ ์กฐ๊ฑด๋ค์ ์ฝค๋ง (,) ๋ก ๊ตฌ๋ถํด์ ๋ฃ์ด์ค๋ค.
๐ in ์ ๊ดํธ ์์ ๋ค๋ฅธ select ๋ฌธ์ ๋ฃ์ ์ ์๋ค.
select * from users u
where u.user_id in (select o.user_id
from orders o
where o.payment_method = 'kakaopay');
where ์กฐ๊ฑด์ ์ค๋ณต
๐ ์ฌ๋ฌ๊ฐ ์ฐ๊ณ ์ถ์ผ๋ฉด and ์ด์ฉํ๋ฉด ๋๋ค.
select payment_method, count(*) from orders
where course_title = "์ฑ๊ฐ๋ฐ ์ข
ํฉ๋ฐ"
and email like '%naver.com'
group by payment_method
alias ๊ธฐ๋ฅ
as ๋ณ์นญ์ง์ด์ฃผ์ธ์
๐ sql์ ์ฟผ๋ฆฌ๋ฌธ์ด ๋ณต์กํด ์ก์๋ ํท๊ฐ๋ฆฌ์ง ์๋๋ก alias ๋ฅผ ํตํด ์ด๋ฆ ์ง์ด๋ ๊ธฐ๋ฅ.
๐ ํ ์ด๋ธ ๋ช , ํ๋๋ช ๋ชจ๋ ๋ถ์ผ ์ ์๋ค.
select * from orders as o
where o.course_title = "์ฑ๊ฐ๋ฐ ์ข
ํฉ๋ฐ";
select name, count(*) as freq from users
group by name
'SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SQL Subquery / with table1 as / distinct (0) | 2023.09.24 |
---|---|
SQL ํ ์ด๋ธ ๋ถ์ด๊ธฐ inner join / left join / NULL (0) | 2023.09.23 |
SQL limit / distinct / count (0) | 2023.09.20 |
SQL where/ between and / like '%' / in() / != ์ด๊ฑฐ ์ ์ธํ๊ณ ๋ณด์ฌ์ค (0) | 2023.09.20 |
SQL ํ๋ก๊ทธ๋๋จธ์ค (0) | 2023.06.04 |