๊ธฐ๋ณธ SQL

8262 ๋‹จ์–ด SQLdatebasetech

CREATE TABLE


ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค ๋•Œcreate table ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
id
name
gender
1
yamada
male
2
hanako
female
์ด๋Ÿฐ ํƒ์ž๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์˜ sql๋ฅผ ์จ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
CREATE TABLE user(
    id VARCHAR(4) NOT NULL,
    name VARCHAR(20) NOT NULL,
    gender VARCHAR(20) NOT NULL,
    PRIMARY KEY (id);
)

INSERT


๋ ˆ์ฝ”๋“œ๋ฅผ ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
INSERT INTO users(name, gender)
VALUES('its', 'male');
id
name
gender
1
yamada
male
2
hanako
female
3
its
male

SELECT


ํ…Œ์ด๋ธ”์„ ๊ฐ€์ ธ์˜ฌ ์—ด์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.*๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „์ฒด ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

FROM


๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

์ฑ…์ƒ ์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ๊ฐ€์ ธ์˜ค๋„ˆ๋ผ


SELECT *
FROM users;
id
name
gender
1
yamada
male
2
hanako
female
3
its
male

ํ…Œ์ด๋ธ” ์—ด์—์„œ ์ง€์ •


SELECT name
FROM users;
name
yamada
hanako
its

ํ…Œ์ด๋ธ” ์—ด์—์„œ ์ง€์ •


SELECT name,gender
FROM users;
name
gender
yamada
male
hanako
female
its
male

DELETE


์ง€์ •ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์‚ฌ์šฉWhere์„ ํ†ตํ•ด ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ” ์‚ญ์ œ


DELETE FROM users;

ํ…Œ์ด๋ธ”์—์„œ ์ง€์ • ์‚ญ์ œ


DELETE FROM users
WHERE name = 'its';
id
name
gender
1
yamada
male
2
hanako
female

UPDATE


ํ…Œ์ด๋ธ” ์ปจํ…ํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ DELETE๋Š” Where๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์„ ์ฒจ๋ถ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
UPDATE users
SET gender = 'female'
WHERE name = 'yamada';
id
name
gender
1
yamada
female
2
hanako
female

AS


๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์—ด ์ด๋ฆ„(ํ…Œ์ด๋ธ” ์ด๋ฆ„๋„ ํฌํ•จ)์„ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
SELECT name asใ€€'ๅๅ‰', gender asใ€€'ๆ€งๅˆฅ'
FROM users
์ด๋ฆ„:
์„ฑ๋ณ„ ๋ช…์‚ฌ
yamada
male
hanako
female

EXISTS

EXISTSboolean์œผ๋กœ ํ•˜์œ„ ๊ฒ€์ƒ‰์˜ ๊ฐ’์„ ๋˜๋Œ๋ ค์ฃผ๊ณ  ์ฃผ ๊ฒ€์ƒ‰์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œuserํ‘œ์˜name์—yamada๊ฐ€ ์กด์žฌํ•˜๋ฉดuserํ‘œ๋กœ ๋Œ์•„๊ฐ‘๋‹ˆ๋‹ค.
SELECT * FROM users
WHERE EXISTS
(SELECT * FROM users
WHERE name = 'yamada');
id
name
gender
1
yamada
male
2
hanako
female

DISTINCT


์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
์ด๋ฆ„์ด ๊ฐ™์€ ๋‘ ์‚ฌ๋žŒ์ด ์žˆ์–ด๋„
id
name
gender
1
yamada
male
2
hanako
female
3
hanako
female
SELECT distinct name
FROM users
name
yamada
hanako

ORDER BY


๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ๋ฐฐ์—ดํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
๊ฐ ์—ด์—๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ(ASC) ๋‚ด๋ฆผ์ฐจ์ˆœ(DESC)์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.๊ธฐ๋ณธ๊ฐ’์€ ์˜ค๋ฆ„์ฐจ์ˆœ์ž…๋‹ˆ๋‹ค.
SELECT *
FROM users
ORDER BYใ€€id DESC;
id
name
gender
2
hanako
female
1
yamada
female

COUNT

COUNT ๊ธฐ๋ก์ˆ˜๋ฅผ ์–ป์—ˆ๋‹ค.
SELECT COUNT(*)
FROM users
-- 2
id
name
gender
1
yamada
male
2
hanako
female

GROUP BY


์ง€์ •ํ•œ ์—ด์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„๊ณ , ํ•ฉ๊ณ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ข…ํ•ฉํ•˜์—ฌ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.
id
name
gender
1
yamada
male
2
hanako
female
3
its
male
SELECT gender, COUNT(*) as count
FROM users GROUP BY gender
gender
count
male
2
female
1

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ