๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

RDB๐Ÿ—‚๏ธ

SQL : ๋ฐ์ดํ„ฐ ์กฐํšŒ

728x90

์ถœ์ฒ˜ : ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

 

SELECT ~ FROM

USE

USE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค_์ด๋ฆ„;

Select ๋ฌธ์˜ ๊ธฐ๋ณธ ํ˜•์‹

SELECT select_expr
	[FROM table_references]
	[WHERE where_condision]
	[GROUP BY {col_name | expr | position}]
	[HAVING where_condition]
	[ORDER BY {col_name | expr | position}]
	[LIMIT {[offset,] row_count | row_count OFFSET offset}]

[] ๋ถ€๋ถ„์€ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ˆœ์„œ๋Š” ์ง€์ผœ์•ผ ํ•œ๋‹ค.

 

SELECT ~ FROM

SELECT ์—ด_์ด๋ฆ„ FROM ํ…Œ์ด๋ธ”_์ด๋ฆ„

from ๋’ค ํ…Œ์ด๋ธ”๊ณผ ๊ทธ ์ค‘ ์—ด_์ด๋ฆ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ฟผ๋ฆฌ๋ฌธ, ๋ชจ๋“  ์—ด์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด * ๋ฅผ ์‚ฌ์šฉ, ์—ฌ๋Ÿฌ ์—ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„

SELECT addr, debut_date, mem_name FROM member;

์—ด์˜ ์ด๋ฆ„์— ๋ผ๋ฒจ์„ ๋ถ™์ผ ์ˆ˜๋„ ์žˆ๋‹ค : ์—ด_์ด๋ฆ„ + " " + '๋ถ™์ด๊ณ  ์‹ถ์€ ๋ผ๋ฒจ'

SELECT addr ์ฃผ์†Œ, debut_date '๋ฐ๋ท” ์ผ์ž', mem_name FROM member;

+ WHERE : ์กฐ๊ฑด๋ฌธ

SELECT * FROM member WHERE mem_name = '๋ธ”๋ž™ํ•‘ํฌ';

ํ‰๊ท  ํ‚ค(height)๊ฐ€ 162 ์ดํ•˜์˜ ๋ฉค๋ฒ„ ์กฐํšŒ

SELECT mem_id, mem_name FROM member WHERE height <= 162;

ํ‰๊ท  ํ‚ค(height)๊ฐ€ 165 ์ด์ƒ์ด๋ฉด์„œ ์ธ์›(mem_number)๊ฐ€ 6๋ช… ์ดˆ๊ณผ์ธ ๋ฉค๋ฒ„ ์กฐํšŒ

SELECT mem_name, height, mem_number 
	FROM member
	WHERE height >= 165 AND mem_number > 6;

ํ‰๊ท  ํ‚ค(height)๊ฐ€ 163์ด์ƒ 165์ดํ•˜์ธ ๋ฉค๋ฒ„ ์กฐํšŒ

 

SELECT * 
	FROM member
	WHERE height >= 163 AND height > 165;

+ BETWEEN ~ AND

SELECT * 
	FROM member
	WHERE height BETWEEN height 163 AND 165;

IN() : ๋ฌธ์ž๋กœ ํ‘œํ˜„๋˜๋Š” ์กฐ๊ฑด์‹์„ ์กฐํ•ฉํ•  ๋•Œ

SELECT mem_name, addr
	FROM member
	WHERE addr = '๊ฒฝ๊ธฐ' OR addr = '์ „๋‚จ' OR addr ='๊ฒฝ๋‚จ';
SELECT mem_name, addr
	FROM member
	WHERE IN('๊ฒฝ๊ธฐ', '์ „๋‚จ', '๊ฒฝ๋‚จ');

๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

LIKE : ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€ ๊ธ€์ž๋ฅผ ์กฐํšŒํ•  ๋•Œ

% : ๋ฌด์—‡์ด๋“  ํ—ˆ์šฉ

_ : ํ•œ๊ธ€์ž ๋งค์น˜

ex) ์ด๋ฆ„์ด '์šฐ'๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฉค๋ฒ„ ์กฐํšŒ ์‹œ

SELECT * FROM member WHERE mem_name LIKE '์šฐ%';

์ด๋ฆ„์ด **ํ•‘ํฌ์ธ ๋ฉค๋ฒ„ ์กฐํšŒ

SELECT * FROM member WHERE mem_name LIKE '__ํ•‘ํฌ';

์„œ๋ธŒ์ฟผ๋ฆฌ

SELECT ์•ˆ์˜ ๋˜ ๋‹ค๋ฅธ SELECT

๋‘ ๋ฒˆ์˜ ์กฐํšŒ๋ฅผ ํ•œ ๋ฒˆ์˜ ์ฟผ๋ฆฌ๋กœ ๊ฐ€๋Šฅ

SELECT mem_name, height FROM member 
	WHERE height > (SELECT height FROM member WHERE mem_name = '์—์ดํ•‘ํฌ');

ORDER BY

ORDER BY : ๊ฒฐ๊ณผ ๊ฐ’์ด๋‚˜ ๊ฐœ์ˆ˜์— ๋Œ€ํ•ด ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์ง€๋งŒ, ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์ˆœ์„œ๋ฅผ ์กฐ์ ˆ

๋ฐ๋ท”๋‚ ์งœ ์˜ค๋ฆ„์ฐจ์ˆœ

SELECT mem_id, mem_name, debut_date FROM member ORDER BY debut_date;

๋ฐ๋ท”๋‚ ์งœ ๋‚ด๋ฆผ์ฐจ์ˆœ

SELECT mem_id, mem_name, debut_date FROM member ORDER BY debut_date DESC;

์—ฌ๋Ÿฌ ์—ด์˜ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

ex) ํ‰๊ท  ํ‚ค๊ฐ€ ํฐ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ , ๋ฐ๋ท” ๋‚ ์งœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

SELECT mem_id, mem_name, debut_date, height
	FROM member
	WHERE height >= 164
	ORDER BY height DESC;

LIMIT : ์ถœ๋ ฅ ๊ฐœ์ˆ˜์˜ ์ œํ•œ

LIMIT(์‹œ์ž‘, ๊ฐœ์ˆ˜) ~ ex) LIMIT 3 == LIMIT 0, 3 == LIMIT 3 OFFSET 0

SELECT mem_name, debut_date
	FROM member
	ORDER BY debut_date
	LIMIT 3;

DISTINCT : ์กฐํšŒ ๊ฒฐ๊ณผ์—์„œ ์ค‘๋ณต๋˜๋Š” ๊ฐ’๋“ค ์ค‘ 1๊ฐœ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค

GROUP

์ถœ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ๋Š”๋‹ค, ๋‹ค์Œ์˜ ์ง‘๊ณ„ํ•จ์ˆ˜์™€ ์ฃผ๋กœ ์‚ฌ์šฉ

SUM() ํ•ฉ๊ณ„
AVG() ํ‰๊ท 
MIN() ์ตœ์†Œ๊ฐ’
MAX() ์ตœ๋Œ€๊ฐ’
COUNT() ํ–‰์˜ ๊ฐœ์ˆ˜
COUNT(DISTINCT) ํ–‰์˜ ๊ฐœ์ˆ˜(์ค‘๋ณต ์ œ๊ฑฐ)

๊ฐ ๋ฉค๋ฒ„์˜ ๊ตฌ๋งคํ•œ ๊ฐœ์ˆ˜์˜ ํ•ฉ์„ ์ถœ๋ ฅ

SELECT mem_id, SUM(amount) FROM buy GROUP BY mem_id;

๊ตฌ๋งค๊ธˆ์•ก์˜ ์ดํ•ฉ ์ถœ๋ ฅ

SELECT mem_id, SUM(price*amount) FROM buy GROUP BY mem_id;

HAVING

๊ธฐ์กด์˜ ์กฐ๊ฑด๋ฌธ์ด ์œ„์น˜ํ•˜๋˜ WHERE์—๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

๊ทธ ์ž๋ฆฌ์— HAVING์„ ์‚ฌ์šฉ

** ๊ผญ GRUOP BY์™€๋งŒ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค

 

728x90