728x90
๐ก select
: ์กฐ๊ฑด์ ๋ง๋ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์
select ์ปฌ๋ผ1, ์ปฌ๋ผ2 ... from <ํ
์ด๋ธ๋ช
> [where <์กฐ๊ฑด์>]
-- '์ฐ๊ณต'๊ณผ 4ํ๋
ํ์๋ค์ ์ด๋ฆ์ ์ฐพ์๋ผ
select sname from student
where dept = '์ฐ๊ณต' and year = 4;
-- ๊ธฐ๋ง๊ณ ์ฌ ๋๋ ์ค๊ฐ๊ณ ์ฌ์ ์ฑ์ ์ด 90์ ์ด์์ธ ํ์๋ค์ ํ๋ฒ์ ์ฐพ์๋ผ
select sno from enrol
where midterm < finterm;
๐ก select distinct
: ์ค๋ณต๋ ๋ ์ฝ๋๋ ํ ๋ฒ๋ง ์ถ๋ ฅ
๐ก order by
: select ๊ฒฐ๊ณผ๋ฅผ ์ ๋ ฌํ์ฌ ์ถ๋ ฅ
asc
: ์ค๋ฆ์ฐจ์desc
: ๋ด๋ฆผ์ฐจ์
select * from <ํ
์ด๋ธ๋ช
> order by ์ปฌ๋ผ1 [asc|desc];
select * from STUDENT order by year asc, sno desc;
๐ก like
: ๋ฌธ์์ด ํจํด์ ๊ฒ์
%
: ์์์ ๊ธธ์ด ๋ฐ ์์์ ๋ฌธ์\_
: ๋จ์ผํ ์์์ ๋ฌธ์
select * from COURSE where cname like '%๊ตฌ์กฐ%' -- ๊ณผ๋ชฉ ์ด๋ฆ์ '๊ตฌ์กฐ'๋ผ๋ ๋ฌธ์์ด์ด ํฌํจ๋ ๋ชจ๋ ๊ณผ๋ชฉ ๊ฒ์
select * from STUDENT where sname like '๊น%' -- ์ฑ์ด '๊น'์ธ ๋ชจ๋ ํ์ ๊ฒ์
select * from STUDENT where sname like '์ธ\_' -- ์ฑ์ด '์ธ'์ธ ๋ ๊ธ์ ํ์ ๊ฒ์
๐ก in
: ์ฃผ์ด์ง ๊ฐ๋ค์ ํฌํจ๋๋ ๊ฒ๋ง ๊ฒ์
select * from STUDENT where year in (1,2,3); -- year๊ฐ 1, 2, 3 ์ธ ์ปฌ๋ผ ๊ฒ์
๐ก between
: ๊ฐ์ด ๋ฒ์์ ๋ฐ๋ฅธ ๊ฒ์
select * from STUDENT
where year between 1 and 3;
๐ก alias
: ํ
์ด๋ธ ๋๋ ์ปฌ๋ผ์ ์ด๋ฆ์ ์ฃผ์ด์ง ์ด๋ฆ์ผ๋ก ์ง์
select sno as ํ๋ฒ, sname as ์ด๋ฆ from STUDENT;
๐ก ์ง๊ณํจ์
avg()
: ํ๊ทcount()
: ๊ฐฏ์max()
: ์ต๋๊ฐmin()
: ์ต์๊ฐsum()
: ํฉ๊ณ
-- ๊ธฐ๋ง๊ณ ์ฌ ์ต๊ณ ์ ์ ๊ฒ์
select max(finterm) from ENROL;
-- 'C413' ๊ณผ๋ชฉ์ ์ต์ ์ ์ ๊ฒ์
select min(finterm) from ENROL where con = 'C413';
-- ๊ธฐ๋ง๊ณ ์ฌ ํ๊ท ๊ฒ์
select avg(finterm) from ENROL
๐ก group by
: ์ปฌ๋ผ๊ฐ์ด ๊ฐ์ ๊ฒ๋ค์ ๋ฌถ์ด ์ง๊ณํจ์๋ฅผ ์ ์ฉ
-- ํ๊ณผ๋ณ๋ก ์์ ํ์์ ์ ๊ฒ์
select count(*) from STUDENT group by dept;
-- ๊ณผ๋ชฉ๋ณ๋ก ๊ธฐ๋ง๊ณ ์ฌ์ ํ๊ท ๊ฒ์
select avg(finterm) from ENROL group by cno;
๐ก having
: ๊ทธ๋ฃน์ ๋ํ ์กฐ๊ฑด์ ์ ์ฉ
- group by ์ ์ด ์๋ ๊ฒฝ์ฐ์๋ ์ ์ฒด ํ ์ด๋ธ์ด ๋์
-- 3๋ช
์ด์ ์๊ฐํ๋ ๊ณผ๋ชฉ์ ๊ธฐ๋ง ํ๊ท ๊ฒ์
select avg(finterm) from ENROL
group by cno
having cnout(*) >= 3;
728x90
'Database > SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] View (0) | 2021.10.17 |
---|---|
[SQL] DML/SELECT (Advanced) (0) | 2021.10.15 |
[SQL] DML/INSERT, UPDATE, DELETE (0) | 2021.10.11 |
[SQL] DDL (0) | 2021.10.11 |
[SQL] SQL ๊ฐ์ (0) | 2021.10.11 |