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

Programming/SQL

SQL ๊ธฐ๋ณธ

SELECT ~ : ์กฐํšŒํ•  ์ปฌ๋Ÿผ๋ช…์„ ์„ ํƒ

FROM ~ : ์กฐํšŒํ•  ํ…Œ์ด๋ธ”๋ช…์„ ์ง€์ • (์œ„์น˜์™€ ํ…Œ์ด๋ธ”๋ช…์„ ์ž…๋ ฅ)

WHERE ~ : ์งˆ์˜ํ•  ๋•Œ ํ•„์š”ํ•œ ์กฐ๊ฑด์„ ์„ค์ •

GROUP BY ~ : ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃน์„ ์ง€์–ด ์ถœ๋ ฅ

ORDER BY ~ : SELECT ๋‹ค์Œ์— ์˜ค๋Š” ์ปฌ๋Ÿผ ์ค‘ ์ •๋ ฌ์ด ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์ •๋ ฌ (๊ธฐ๋ณธ ์„ค์ • : ์˜ค๋ฆ„์ฐจ์ˆœ)

LIMIT ์ˆซ์ž : Displayํ•˜๊ณ ์ž ํ•˜๋Š” ํ–‰์˜ ์ˆ˜๋ฅผ ์„ค์ •

#ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์ปฌ๋Ÿผ๋ช…์„ ์ถ”์ถœํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ
SELECT * 
FROM ํ…Œ์ด๋ธ” ๋ช…

#์ถ”์ถœํ•˜๊ณ  ์‹ถ์€ ์ปฌ๋Ÿผ๋ช…์„ ์‰ผํ‘œ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๋‚˜์—ด
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ์ปฌ๋Ÿผ3,... 
FROM ํ…Œ์ด๋ธ”๋ช… 

#์„ ํƒํ•œ ์ปฌ๋Ÿผ์˜ ์ „์ฒด ๊ฐ’์ด ์•„๋‹Œ, ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ๊ฐ’๋งŒ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
SELECT DISTINCT ์ปฌ๋Ÿผ1 
FROM ํ…Œ์ด๋ธ”๋ช… 

#์ปฌ๋Ÿผ 1,2,3์˜ ์กฐํ•ฉ ์ค‘์—์„œ uniqueํ•œ ๊ฐ’๋งŒ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
SELECT DISTINCT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2, ์ปฌ๋Ÿผ3 
FROM ํ…Œ์ด๋ธ”๋ช… 

#COUNT(*): ํ…Œ์ด๋ธ” ์ „์ฒด์˜ row ์ˆ˜๋ฅผ ์ถœ๋ ฅ, COUNT(์ปฌ๋Ÿผ): ํ•ด๋‹น์ปฌ๋Ÿผ์˜ ์ „์ฒด row ์ˆ˜๋ฅผ ์ถœ๋ ฅ
SELECT COUNT(*), COUNT(์ปฌ๋Ÿผ) 
FROM ํ…Œ์ด๋ธ”๋ช… 

#ํ•ด๋‹น ์ปฌ๋Ÿผ์˜ ์ „์ฒด row์ˆ˜๊ฐ€ ์•„๋‹Œ, ์ค‘๋ณต ๊ฐ’์„ ์ œ๊ฑฐํ•œ row ์ˆ˜ ์ถœ๋ ฅ
SELECT(DISTINCT ์ปฌ๋Ÿผ) 
FROM ํ…Œ์ด๋ธ”๋ช… 

#์ปฌ๋Ÿผ์˜ ๋‚˜์—ด๊ณผ COUNT๊ฐ€ ํ•จ๊ป˜์“ฐ์ด๋ฉด GROUP BY ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์ค˜์•ผํ•จ
SELECT ์ปฌ๋Ÿผ1, COUNT(*) 
FROM ํ…Œ์ด๋ธ”๋ช… 
GROUP BY ์ปฌ๋Ÿผ1 

#์ปฌ๋Ÿผ1*์ปฌ๋Ÿผ2์˜ ์กฐํ•ฉ์— ๋Œ€ํ•œ ์ปฌ๋Ÿผ3์˜ row์ˆ˜๋ฅผ ์ถœ๋ ฅ
SELECT ์ปฌ๋Ÿผ1, ์ปฌ๋Ÿผ2,COUNT(DISTINCT ์ปฌ๋Ÿผ3) 
FROM ํ…Œ์ด๋ธ”๋ช… 
GROUP BY ์ปฌ๋Ÿผ 1, ์ปฌ๋Ÿผ2

์ค‘๋ณต์ œ๊ฑฐ

#1. GROUP BY๋กœ ์ค‘๋ณต์ œ๊ฑฐ
SELECT ์ด๋ฆ„ FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ 
GROUP BY ์ด๋ฆ„;

#2. DISTINCT๋กœ ์ค‘๋ณต์ œ๊ฑฐ
SELECT DISTINCT ์ด๋ฆ„ FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ;

์ •๋ ฌ

#์˜ค๋ฆ„์ฐจ์ˆœ
SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ
ORDER BY ID ;

#๋‚ด๋ฆผ์ฐจ์ˆœ
SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ 
ORDER BY ID DESC ;

๋ช‡๊ฐœ์˜ row๋งŒ ์กฐํšŒ

SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ LIMIT 5 ;

๋ฌธ์žํ˜•์˜ ํŠน์ • ๋ถ€๋ถ„์„ ๋–ผ์–ด ๋‚ด๋Š” ํ•จ์ˆ˜

  • LEFT(๋ฌธ์žํ˜•์ปฌ๋Ÿผ,๊ธธ์ด) : ์™ผ์ชฝ๋ถ€ํ„ฐ ์›ํ•˜๋Š” ๊ธธ์ด๋งŒํผ ์ž๋ฅด๋Š” ํ•จ์ˆ˜
  • RIGHT(๋ฌธ์žํ˜•์ปฌ๋Ÿผ,๊ธธ์ด) : ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์›ํ•˜๋Š” ๊ธธ์ด๋งŒํผ ์ž๋ฅด๋Š” ํ•จ์ˆ˜
  • SUBSTR(๋ฌธ์žํ˜•์ปฌ๋Ÿผ,๊ธธ์ด) or SUBSTRING() : ์ผ์ • ์˜์—ญ๋งŒํผ์„ ์ž๋ผ๋Š” ํ•จ์ˆ˜
#6์ผ -> 6
#๋งˆ์ง€๋ง‰ ์ž๋ฆฌ๋งŒ ์‚ญ์ œ
SELECT *, SUBSTR(๋Œ€์ถœ์ผ์ˆ˜, 1, (length(๋Œ€์ถœ์ผ์ˆ˜)-1)) AS ๋Œ€์ถœ์ผ์ˆ˜_์ˆ˜์ • 
FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ2;

๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€๊ฒฝ: CAST()

SELECT *, CAST(SUBSTR(๋Œ€์ถœ์ผ์ˆ˜, 1, (length(๋Œ€์ถœ์ผ์ˆ˜)-1)) AS INT) AS ๋Œ€์ถœ์ผ์ˆ˜_์ˆ˜์ • 
FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ2 ;

ํ‰๊ท  : AVG()

SELECT ID, ์ด๋ฆ„, ๋Œ€์ถœ๋…„์›”
, AVG(CAST(SUBSTR(๋Œ€์ถœ์ผ์ˆ˜, 1, (length(๋Œ€์ถœ์ผ์ˆ˜)-1)) AS INT)) AS ๋Œ€์ถœ์ผ์ˆ˜_ํ‰๊ท  
FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ2 
GROUP BY 1,2,3;

WHERE ์ ˆ

SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ2
WHERE 
    ์กฐ๊ฑด1 
AND ์กฐ๊ฑด2 
AND ์กฐ๊ฑด3 
AND (์กฐ๊ฑด 4 OR ์กฐ๊ฑด5);
  1. ํŠน์ • ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” row๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ
SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ2 
WHERE ์ด๋ฆ„ LIKE "๋ฌธ%" ;
  1. ํŠน์ • ๊ธฐ๊ฐ„ ํ˜น์€ ํŠน์ • ๋‚ ์งœ์˜ ์ „ ๋˜๋Š” ์ดํ›„์˜ row๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ
SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ
WHERE ๋Œ€์ถœ์ผ >= "2020-06-01" 
AND ๋Œ€์ถœ์ผ <= "2020-06-07" ;
SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ
WHERE ๋Œ€์ถœ์ผ BETWEEN "2020-06-01" AND "2020-06-07" ;
  1. ํŠน์ • ์ˆซ์ž ์ด์ƒ ๋˜๋Š” ์ดํ•˜์˜ row๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์„ ๋•Œ
SELECT *
, CAST(SUBSTR(๋Œ€์ถœ์ผ์ˆ˜, 1, (length(๋Œ€์ถœ์ผ์ˆ˜)-1)) AS INT) AS ๋Œ€์ถœ์ผ์ˆ˜_์ˆ˜์ • 
FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ2 
WHERE  ๋Œ€์ถœ์ผ์ˆ˜_์ˆ˜์ • > 5 ;

NULL

  1. NULL ์ด ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋งŒ
SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ
WHERE ๋ฐ˜๋‚ฉ์ผ IS NOT NULL;
  1. NULL ์ธ ๋ฐ์ดํ„ฐ๋งŒ
SELECT * FROM ๋„์„œ๋Œ€์ถœ๋‚ด์—ญ
WHERE ๋ฐ˜๋‚ฉ์ผ IS NULL ;

JOIN

  1. INNER JOIN
SELECT A.*, B.๋„์„œ๋ช… 
FROM ๋Œ€์ถœ๋‚ด์—ญ AS A 
INNER JOIN ๋„์„œ๋ช… AS B
ON A.๋„์„œID = B.๋„์„œID;
  1. LEFT JOIN
SELECT A.*, B.๋„์„œ๋ช… 
FROM ๋Œ€์ถœ๋‚ด์—ญ AS A 
LEFT JOIN ๋„์„œ๋ช… AS B
ON A.๋„์„œID = B.๋„์„œID;
  1. FULL OUTER JOIN
SELECT A.*, B.๋„์„œ๋ช… 
FROM ๋Œ€์ถœ๋‚ด์—ญ AS A 
FULL OUTER JOIN ๋„์„œ๋ช… AS B
ON A.๋„์„œID = B.๋„์„œID
ORDER BY ๋„์„œID;

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

[HackerRank] Basic Select : Japanese Cities' Attributes (MySQL)  (0) 2022.02.03
[HackerRank] Basic Select : Select By ID (MySQL)  (0) 2022.02.03
[HackerRank] Basic Select : Select All (MySQL)  (0) 2022.02.03
SQLite  (2) 2021.11.16
MySQL ์ •๋ฆฌ  (0) 2021.01.25