๐Ÿ’พ SELECT๋ฌธ - WHERE, ORDER BY, GROUP BY

suRanยท2022๋…„ 7์›” 13์ผ
0

๐Ÿ’พ MySQL

๋ชฉ๋ก ๋ณด๊ธฐ
2/9
post-thumbnail

๋ณธ ํฌ์ŠคํŒ…์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฏธ๋‹ˆ ๋ฐ๋ธŒ ์ฝ”์Šค๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ
ํ•™์Šต์„ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์œผ๋กœ ์ž‘์„ฑ๋œ ๊ธ€์ž…๋‹ˆ๋‹ค.

1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ ํƒ ๋ช…๋ น์–ด

SQL์€ ์ผ์ข…์˜ ๋ถ„๋ฅ˜์ฒด๊ณ„์ฒ˜๋Ÿผ 2๊ณ„์ธต์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค.
์ƒ์œ„ ๊ณ„์ธต์—๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(์Šคํ‚ค๋งˆ)๊ฐ€ ์žˆ๊ณ  ํ•˜์œ„ ๊ณ„์ธต์—๋Š” ํ…Œ์ด๋ธ”์ด ์žˆ๋‹ค.
๊ทธ๋ž˜์„œ ๋ฐ์ดํ„ฐ์˜ ์„ฑ๊ฒฉ์— ๋งž๊ฒŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋“ค์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.


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

SHOW DATABASES; ๋Š” ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ช…๋ น์–ด๋‹ค.
USE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„; ์€ ์•ž์œผ๋กœ ํ…Œ์ด๋ธ” ์ƒ์„ฑ์€ ๋ช…์‹œํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.


SHOW TABLES;

USE๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋ช…์‹œํ•ด์ค€ ๋’ค SHOW TABLES; ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ช…์‹œํ•ด์ค€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์กด์žฌํ•˜๋Š” ํ…Œ์ด๋ธ”๋“ค์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.




2. SELECT๋ฌธ

SELECT๋Š” ๊ฒ€์ƒ‰์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์กฐ์ž‘์–ด(DML)์ด๋‹ค.
DML์€ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰, ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋ฉฐ
DML์—๋Š” SELECT, INSERT, DELETE, UPDATE๋ฌธ ๋“ฑ์ด ์žˆ๋‹ค.
๊ทธ ์ค‘ SELECT๋ฌธ์€ ํŠน๋ณ„ํžˆ ์งˆ์˜์–ด๋ผ๊ณ  ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

๐Ÿ’ฟ ๊ธฐ๋ณธ ๊ตฌ์กฐ

SELECT ํ•„๋“œ์ด๋ฆ„1, ํ•„๋“œ์ด๋ฆ„2, โ€ฆ
FROM ํ…Œ์ด๋ธ”์ด๋ฆ„
WHERE ์„ ํƒ์กฐ๊ฑด
GROUP BY ํ•„๋“œ์ด๋ฆ„1, ํ•„๋“œ์ด๋ฆ„2, โ€ฆ
ORDER BY ํ•„๋“œ์ด๋ฆ„ [ASC|DESC] -- ํ•„๋“œ ์ด๋ฆ„ ๋Œ€์‹ ์— ์ˆซ์ž ์‚ฌ์šฉ ๊ฐ€๋Šฅ
LIMIT N;

  • LIMIT ๋Š” ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๋ ˆ์ฝ”๋“œ์˜ ์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•ด์ค€๋‹ค.
    ์˜ˆ๋ฅผ ๋“ค์–ด LIMIT 10;์ด๋ผ๋Š” ์งˆ์˜๋ฅผ ์ž…๋ ฅํ–ˆ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๋ ˆ์ฝ”๋“œ๋Š” 10๊ฐœ๊ฐ€ ๋œ๋‹ค.


๐Ÿ’ฟ DISTINCT

SELECT DISTINCT ํ•„๋“œ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”๋ช…

  • DISTINCT ๋Š” ์ค‘๋ณต์ด ์•„๋‹Œ ๊ฐ’์„ ๋ณด์—ฌ์ค€๋‹ค.
    ์–ด๋–ค ๊ฐ’์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ์ง€ ์‰ฝ๊ฒŒ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.
  • DISTINCT๋Š” ํ•œ ํ•„๋“œ์—๋งŒ ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.
    ๋’ค๋”ฐ๋ผ์˜ค๋Š” ๋ชจ๋“  ํ•„๋“œ์— ์ ์šฉ๋œ๋‹ค.


๐Ÿ’ฟ COUNT()

SELECT COUNT(*)
FROM ํ…Œ์ด๋ธ”๋ช…

  • SELECT๋œ ํ•„๋“œ์˜ ๋ ˆ์ฝ”๋“œ ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
    WHERE๋ฌธ์ด ์žˆ๋‹ค๋ฉด WHERE๋ฌธ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํŠน์ • ๋ ˆ์ฝ”๋“œ์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
    COUNT() ๋‚ด๋ถ€์˜ ๊ฐ’์ด NULL๋งŒ ์•„๋‹ˆ๋ผ๋ฉด ์–ด๋–ค ๊ฐ’์„ ๋„ฃ๋“  COUNT()๋Š” ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.


COUNT ๋™์ž‘์— ๋Œ€ํ•œ ์ดํ•ด

์˜ˆ์ œ
"COUNT() ๋‚ด๋ถ€์˜ ๊ฐ’์ด NULL๋งŒ ์•„๋‹ˆ๋ผ๋ฉด ์–ด๋–ค ๊ฐ’์„ ๋„ฃ๋“  COUNT()๋Š” ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค."
์ด๊ฒŒ ๋Œ€์ฒด ๋ฌด์Šจ ๋ง์ผ๊นŒ? ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ํ•™์Šตํ•ด๋ณด์•˜๋‹ค.


prod.count_test ํ…Œ์ด๋ธ”์˜ value ํ•„๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž.
ํ…Œ์ด๋ธ”์— ๋Œ€ํ•œ ๋‹ค์Œ ๋‹ค์„ฏ ๊ฐ€์ง€์˜ ์งˆ์˜๊ฐ€ ์žˆ๋‹ค.

SELECT COUNT(1) 
FROM prod.count_test;

SELECT COUNT(0) 
FROM prod.count_test;

SELECT COUNT(NULL) 
FROM prod.count_test;

SELECT COUNT(value) 
FROM prod.count_test;

SELECT COUNT(DISTINCT value) 
FROM prod.count_test;

๊ฐ ์งˆ์˜์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋Š” ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?


SELECT COUNT(1) 
FROM prod.count_test;

SELECT COUNT(0) 
FROM prod.count_test;

COUNT()๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ SELECT๋œ ๋ ˆ์ฝ”๋“œ์˜ ์ˆ˜๋ฅผ ์„ผ๋‹ค.
COUNT()๊ฐ€ ์ˆ˜๋ฅผ ์„ธ๋Š” ๋ฐฉ์‹์€ COUNT()์˜ ์ธ์ž์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.
COUNT()์˜ ์ธ์ž๊ฐ€ NULL์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ๋ฌด์กฐ๊ฑด 1์”ฉ ์นด์šดํŠธํ•œ๋‹ค.
NULL์ธ ๊ฒฝ์šฐ์—๋Š”? ์•„์˜ˆ ์นด์šดํŠธํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋‘ ์งˆ์˜์—์„œ๋Š” COUNT ์ธ์ž๊ฐ€ NULL๊ฐ’์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—
value ํ•„๋“œ์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์นด์šดํŠธํ•œ๋‹ค.

๋”ฐ๋ผ์„œ, ๊ฒฐ๊ณผ๋Š” 7์ด๋‹ค.

SELECT COUNT(NULL) 
FROM prod.count_test;

์ด ์งˆ์˜์—์„œ๋Š” COUNT ์ธ์ž๊ฐ€ NULL๊ฐ’์ด๋‹ค.
์ด ๊ฒฝ์šฐ์—๋Š” COUNT ํ•จ์ˆ˜์— ํ•ญ์ƒ NULL์ด ๋“ค์–ด์™€ ์นด์šดํŠธ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋”ฐ๋ผ์„œ, ๊ฒฐ๊ณผ๋Š” 0์ด๋‹ค.


SELECT COUNT(value) 
FROM prod.count_test;

์ด ์งˆ์˜์—์„œ๋Š” ์ปฌ๋Ÿผ ๊ฐ’์„ COUNT์˜ ์ธ์ž๋กœ ์คฌ๋‹ค.
์ด ๊ฒฝ์šฐ ์ปฌ๋Ÿผ์˜ ๋ ˆ์ฝ”๋“œ์—์„œ NULL์ด ์•„๋‹Œ ๊ฒฝ์šฐ๋งŒ ์นด์šดํŠธํ•œ๋‹ค.

๋”ฐ๋ผ์„œ, ๊ฒฐ๊ณผ๋Š” 6์ด๋‹ค.


SELECT COUNT(DISTINCT value) 
FROM prod.count_test;

์ด ์งˆ์˜์—์„œ๋Š” DISTSINCT๋ฅผ ๋จผ์ € ํ•˜๊ณ  ๊ทธ ๋‹ค์Œ COUNT๋ฅผ ํ–ˆ๋‹ค.
DISTSINCT ๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๋Š” NULL, 1, 0, 4, 3 ์ด ๋œ๋‹ค.
๊ทธ ๋‹ค์Œ COUNT๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด NULL๊ฐ’์ด ๋น ์ง„ 1, 0, 4, 3 ๋ ˆ์ฝ”๋“œ๋งŒ ์นด์šดํŠธํ•œ๋‹ค.

๋”ฐ๋ผ์„œ, ๊ฒฐ๊ณผ๋Š” 4์ด๋‹ค.



๐Ÿ’ฟ CASE WHEN

CASE
WHEN ์กฐ๊ฑด1 THEN ๊ฐ’1
WHEN ์กฐ๊ฑด2 THEN ๊ฐ’2
ELSE ๊ฐ’3
END ํ•„๋“œ์ด๋ฆ„

  • CASE WHEN์€ ํ•„๋“œ๋“ค์„ ๊ฐ€์ง€๊ณ  ์ ๋‹นํ•œ ๊ธฐ์ค€์œผ๋กœ ์ƒˆ ํ•„๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค.

  • WHEN์€ ์ ์–ด๋„ ํ•œ ๊ฐœ ์ด์ƒ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • CASE๋กœ ์—ด๊ณ  END๋กœ ๋‹ซ๋Š”๋‹ค.



์˜ˆ์ œ

SELECT channel_id, CASE
WHEN channel_id in (1, 5, 6) THEN 'Social-Media'
WHEN channel_id in (2, 4) THEN 'Search-Engine'
ELSE 'Something-Else'
END channel_type
FROM prod.session;

์œ„ ์˜ˆ์ œ๋Š”
prod ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ session์ด๋ผ๋Š” ํ…Œ์ด๋ธ”์˜ channel_id๋ผ๋Š” ํ•„๋“œ์—์„œ
channel_id๊ฐ€ 1, 5, 6์ด๋ฉด 'Social-Media'๋ผ๋Š” ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ณ 
channel_id๊ฐ€ 2, 4์ด๋ฉด 'Search-Engine'๋ผ๋Š” ๊ฐ’์„ ์‚ฌ์šฉํ•˜๊ณ 
์•„๋‹ˆ๋ฉด 'Something-Else'๋ผ๋Š” ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ผ๋Š” ์งˆ์˜๊ฐ€ ๋œ๋‹ค.


์œ„ ์งˆ์˜์˜ ๊ฒฐ๊ณผ๋กœ ๋‘ ๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.
int ํƒ€์ž…์˜ channel_id์™€ Varcharํƒ€์ž…์˜ channel_type์ด๋‹ค.


๊ฒฐ๊ณผ




3. WHERE

WHERE๋Š” ์กฐ๊ฑด๋ฌธ์ด๋‹ค.
WHERE์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์˜คํผ๋ ˆ์ดํ„ฐ๋“ค์ด ์žˆ๋‹ค.
๋‹ค์Œ ์˜คํผ๋ ˆ์ดํ„ฐ๋“ค์€ CASE WHEN ์‚ฌ์ด์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.



๐Ÿ’ฟ IN

WHERE channel_id IN (1, 2)
-- ์œ„ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๋™์ผํ•˜๋‹ค.
WHERE channel_id = 1 OR channel_id = 2

  • IN์€ OR์™€ ๋น„์Šทํ•œ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค.
    ์œ„ ์—ฐ์‚ฐ์€ channel_id๊ฐ€ 1์ด๋‚˜ 2์ธ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

WHERE channel_id NOT IN (1, 2)

  • ์œ„ ์—ฐ์‚ฐ์€ channel_id๊ฐ€ 1์ด๋‚˜ 2์ด ์•„๋‹Œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๐Ÿ’ฟ LIKE

WHERE channel LIKE 'A%'  --'A*' channel์ด๋ฆ„์ด A๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ 

WHERE channel LIKE '%์ˆ˜%'  --'*์ˆ˜*' channel์ด๋ฆ„์— ์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ

WHERE channel NOT LIKE '%C%'  --'*C*' channel์ด๋ฆ„์— C๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š์€ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ 

  • LIKE๋Š” ๋ฌธ์ž์—ด ๋งค์นญ์„ ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์—ฐ์‚ฐ์ด๋‹ค.

  • ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.



๐Ÿ’ฟ BETWEEN

SELECT DISTINCT ํ•„๋“œ์ด๋ฆ„
FROM ํ…Œ์ด๋ธ”๋ช…

  • BETWEEN ์€ ๋‚ ์งœ ๋ฒ”์œ„์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ’ฟ STRING Functions

LEFT(str, N)
REPLACE(str, exp1, exp2)
UPPER(str)
LOWER(str)
LENGTH(str)
LPAD
RPAD
SUBSTRING --ํ•„๋“œ๋ฅผ ๋ถ™์—ฌ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.
CONCAT
  • SQL์—์„œ๋Š” ๋‹ค์–‘ํ•œ String ํ•จ์ˆ˜๊ฐ€ ์ง€์›๋œ๋‹ค.


์˜ˆ์‹œ

SELECT
	LENGTH(channel),
    UPPER(channel),
    LOWER(channel),
    LEFT(channel, 4), -- ์•ž์—์„œ๋ถ€ํ„ฐ 4๊ธ€์ž๋งŒ ์ถœ๋ ฅ
    RPAD(channel, 15, '-') -- ์˜ค๋ฅธ์ชฝ์— ์ตœ๋Œ€ 15๊ธ€์ž๊นŒ์ง€ '-'๋ฅผ ํŒจ๋”ฉํ•œ๋‹ค
FROM channel;

์ถœ๋ ฅ๊ฒฐ๊ณผ




4. ORDER BY

ORDER BY๋Š” SELECT ๋กœ ๊ฐ€์ ธ์˜จ ๊ฐ’์— ์ˆœ์„œ๋ฅผ ์ค€๋‹ค.
ORDER BY ์˜ ๋””ํดํŠธ ์ˆœ์„œ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ(ASC)์ด๋‹ค.
์˜ค๋ฆ„์ฐจ์ˆœ์€ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์ด ๋จผ์ € ๋‚˜์˜จ๋‹ค.

๋งŒ์•ฝ TIMESTAMP ํƒ€์ž…์˜ ํ•„๋“œ๋ฅผ ์˜ค๋ž˜๋œ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด
์˜ค๋ฆ„์ฐจ์ˆœ(ASC)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋ฐ˜๋Œ€๋กœ ์ตœ์‹ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ ์‹ถ๋‹ค๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ(DESC)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.


๐Ÿ’ฟ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ •๋ ฌ

ORDER BY 1 DESC, 2, 3;

  • ์œ„ ์งˆ์˜์˜ ๋œป์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    SELECT๋ฌธ์—์„œ ์‚ฌ์šฉ๋œ ์ฒซ ๋ฒˆ์งธ ํ•„๋“œ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ ,

    ์ฒซ ๋ฒˆ์งธ ํ•„๋“œ๊ฐ’์—์„œ ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜ค๋ฉด
    ๋‘ ๋ฒˆ์งธ ํ•„๋“œ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.

    ๋‘ ๋ฒˆ์งธ ํ•„๋“œ๊ฐ’์—์„œ๋„ ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜ค๋ฉด
    ์„ธ ๋ฒˆ์งธ ํ•„๋“œ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.

์˜ˆ์ œ1

SELECT id, channel_id
FROM session
ORDER BY 1 DESC;

๊ฒฐ๊ณผ


์˜ˆ์ œ2

SELECT id, channel_id
FROM session
ORDER BY 2 DESC;

๊ฒฐ๊ณผ


์˜ˆ์ œ3

SELECT id, channel_id
FROM session
ORDER BY 2 DESC, 1;

๊ฒฐ๊ณผ


์˜ˆ์ œ4

SELECT id, channel_id
FROM session
ORDER BY 2 DESC, 1 DESC;

๊ฒฐ๊ณผ



๐Ÿ’ฟ NULL ๊ฐ’์˜ ์ •๋ ฌ

NULL

  • NULL๊ฐ’์˜ ์ •๋ ฌ์€ RDBMS๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค.

  • MySQL์˜ ๊ฒฝ์šฐ์—๋Š”
    ์˜ค๋ฆ„์ฐจ์ˆœ์ผ ๋•Œ NULL๊ฐ’์ด ์ฒ˜์Œ์— ์œ„์น˜ํ•˜๊ณ 
    ๋‚ด๋ฆผ์ฐจ์ˆœ์ผ ๋•Œ NULL๊ฐ’์ด ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•œ๋‹ค.



5. GROUP BY

GROUP BY๋Š” SELECT๋œ ๋ ˆ์ฝ”๋“œ๋ฅผ ํŠน์ • ํ•„๋“œ์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘ํ•˜๊ณ ,
์—ฌ๋Ÿฌ ์ง‘๊ณ„ํ•จ์ˆ˜ ์—ฐ์‚ฐ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.
ORDER BY์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•„๋“œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜,
ํ•„๋“œ ์ผ๋ จ๋ฒˆํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.


์‹ค์Šต์— ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

2๋งŒ๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด์žˆ๋Š” sessionํ…Œ์ด๋ธ”๊ณผ channelํ…Œ์ด๋ธ”์ด๋‹ค.






๐Ÿ’ฟ ์›”๋ณ„ ์„ธ์…˜ ์ˆ˜

SELECT
	LEFT(created, 7) AS mon,
    COUNT(1) AS session_count
FROM session
GROUP BY 1 -- GROUP BY mon, GROUP BY LEFT (created, 7)
ORDER BY 1;
  • ์œ„ ์งˆ์˜๋Š” LEFT()ํ•จ์ˆ˜๋กœ createdํ•„๋“œ์—์„œ ์ฒ˜์Œ 7๊ธ€์ž๋งŒ ๋ฝ‘์•„ mon์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ํ•„๋“œ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • GROUP BY๋กœ ์ธํ•ด mon ํ•„๋“œ์˜ ๊ฐ’์ด ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๋“ค์ด ๊ฐ™์€ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์ธ๋‹ค.
  • ORDER BY๋กœ SELECT ์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
    ์ด๋•Œ, ๋””ํดํŠธ ๊ฐ’์ธ ์˜ค๋ฆ„์ฐจ์ˆœ(ASE)์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค. ์‹œ๊ฐ„์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์€ ์˜ค๋ž˜๋œ ๋‚ ์งœ -> ์ตœ์‹ ์ˆœ์ด๋‹ค.

๊ฒฐ๊ณผ



๐Ÿ’ฟ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์ฑ„๋„์€?

์šฐ์„  '๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜์—ˆ๋‹ค'์˜ ๊ธฐ์ค€์„ ์ •ํ•ด์•ผํ•œ๋‹ค.
๋‘ ๊ฐ€์ง€ ๊ธฐ์ค€์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

  • ์„ธ์…˜ ์ˆ˜
  • ์‚ฌ์šฉ์ž์˜ ์ˆ˜

SELECT
	channel_id,
    COUNT(1) AS session_count,
    COUNT(DISTINCT user_id) AS user_count
FROM session
GROUP BY 1		-- GROUP BY channel_id
ORDER BY 2 DESC; -- ORDER BY session_count DESC

  • SELECT๋ฌธ์—์„œ COUNT()๋ฅผ ๋‘ ๋ฒˆ ํ–ˆ๋‹ค.
    ์ด ์ฑ„๋„์—์„œ ๋ฐœ์ƒํ•œ ์„ธ์„ ์˜ ์ˆ˜ ์นด์šดํŠธ
    ์ด ์ฑ„๋„์„ ์‚ฌ์šฉํ•œ ์œ ๋‹ˆํฌํ•œ user_id ์นด์šดํŠธ


๊ฒฐ๊ณผ

๋งŒ์ผ DISTICT๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.



๐Ÿ’ฟ ๊ฐ€์žฅ ๋งŽ์€ ์„ธ์…˜์„ ๋งŒ๋“ค์–ด๋‚ธ ์‚ฌ์šฉ์ž ID

SELECT
	user_id,
    COUNT(1) AS session_count	
FROM session
GROUP BY 1 			-- GROUP BY user_id
ORDER BY 2 DESC		-- ORDER BY count DESC
LIMIT 1;

๊ฒฐ๊ณผ



๐Ÿ’ฟ ์›”๋ณ„, ์ฑ„๋„๋ณ„ ์œ ๋‹ˆํฌํ•œ ์‚ฌ์šฉ์ž ์ˆ˜

MAU(Monthly Active User)๋Š” ํŠธ๋ž˜ํ”ฝ ์ง€ํ‘œ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค.
MAU์—๋Š” ์‹œ๊ฐ„์ •๋ณด, ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

์›”๋ณ„ ์„ธ์…˜ ์ˆ˜ ์‹ค์Šต๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ
์ด๋ฒˆ์—๋Š” channel_id ๊ฐ€ ์•„๋‹Œ channel์˜ ์ด๋ฆ„์œผ๋กœ ๊ณ„์‚ฐํ•ด๋ณด๋Š” ์‹ค์Šต์„ ํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ JOIN ํ•ด์•ผํ•œ๋‹ค.


SELECT 
	s.id, s.user_id, s.created, s.channel_id, c.channel
FROM session s
JOIN channel c ON c.id = s.channel_id;
  • ์œ„ ์งˆ์˜๋Š” INNER JOIN์˜ ์˜ˆ์‹œ๋‹ค.
    ์–‘ ์ชฝ ํ…Œ์ด๋ธ”์—์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋“ค์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ SELECT๊ฐ€ ๋œ๋‹ค(๊ต์ง‘ํ•ฉ).

  • JOIN์ด๋ž€ ์„œ๋กœ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์— ์กด์žฌํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋“ค์„ ํŠน์ • ์กฐ๊ฑด์„ ๋ฐ”ํƒ•์œผ๋กœ ๋ณ‘ํ•ฉํ•˜์—ฌ
    ๋” ์™„์ „ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์—ฐ์‚ฐ์ด๋‹ค.

  • ON ๋’ค์—๋Š” ๋ณ‘ํ•ฉ ์กฐ๊ฑด์„ ์„ค์ •ํ•ด์ค€๋‹ค.



์ด์ œ ์›”๋ณ„ ์ฑ„๋„๋ณ„ ์œ ๋‹ˆํฌํ•œ ์‚ฌ์šฉ์ž ์ˆ˜๋ฅผ ์•Œ์•„๋ณด์ž.


์˜ˆ์‹œ

SELECT 
	LEFT(s.created, 7) AS mon,
    c.channel,
    COUNT(DISTINCT user_id) AS MAU
FROM session s JOIN channel c ON s.channel_id = c.id 
GROUP BY 1, 2
ORDER BY 1 DESC, 2;

  • ์›”๋ณ„, ์ฑ„๋„๋ณ„์ด๊ธฐ ๋•Œ๋ฌธ์— GROUP BY๋ฅผ ๋‘ ๊ฐœ์˜ ํ•„๋“œ์— ์ ์šฉํ•œ๋‹ค.
    LEFT(s.created, 7)์™€ c.channel์ด ๊ฐ™์€ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ๊ณ 
    ๊ทธ ์•ˆ์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” user_id๋งŒ ์นด์šดํŠธํ•ด์„œ MAU๋ผ๋Š” ํ•„๋“œ๋กœ ๋งŒ๋“ ๋‹ค.

  • ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ mon๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœํ•˜๊ณ ,
    ์ค‘๋ณต๋˜๋Š” ๊ฒฝ์šฐ c.channel์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
    ์ฆ‰, ์ตœ์‹  ์ˆœ๋ถ€ํ„ฐ ์ •๋ ฌ๋˜๋ฉด์„œ ์ฑ„๋„์ด๋ฆ„์€ ABC์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ฒŒ ๋œ๋‹ค.

  • (+)์ฃผ์˜ํ•  ์ 
    COUNT์˜ ๋™์ž‘์„ ์ž˜ ์ดํ•ดํ•˜์ž. ๋งŒ์ผ DISTINCT๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ user_id๋งŒ ์นด์šดํŠธํ•œ๋‹ค๋ฉด ๊ฒฐ๊ณผ๋Š” ์‚ฌ์šฉ์ž ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ session์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธํ•œ ๊ฒƒ์ด ๋œ๋‹ค.


    ๊ฒฐ๊ณผ

๊ฒฐ๊ณผ2 (DINSTINCT๋ฅผ ํ•˜์ง€์•Š์•˜์„ ๋•Œ)

profile
๊ฐœ๋ฐœ ๊ณต๋ถ€๋ฅผ ํ•ด๋ผ

0๊ฐœ์˜ ๋Œ“๊ธ€