C.W.K.
Stream
Lesson 05 of 10 · published

DISTINCT 와 집계 함수

~12 min · sql, distinct, aggregate

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Row 에서 요약으로

DISTINCT 가 row 값 중복 제거; 집계는 여러 row 를 한 숫자로 collapse.

  • COUNT(*) — row count. COUNT(col) 은 col 의 non-NULL count.
  • SUM(col), AVG(col), MIN(col), MAX(col) — 명백한 거.
  • GROUP_CONCAT(col, sep) — 값들을 string 으로 합침.
  • TOTAL(col) — SUM 같은데 빈 input 이면 0 반환 (SUM 은 NULL).
Tip: WHERE 없는 테이블의 SELECT count(*) 가 SQLite 에서 O(n) — 실제로 row 들 walk. 자주 호출하면 count 캐시하거나 counter row 유지. (Postgres 도 같은 속성.)

Code

집계 + DISTINCT·sql
SELECT count(*) FROM users;                              -- 총 row
SELECT count(DISTINCT role) FROM users;                  -- 고유 role count
SELECT role, count(*) FROM users GROUP BY role;          -- 그룹 count

SELECT min(created_at), max(created_at) FROM messages;

SELECT GROUP_CONCAT(username, ', ') FROM users;
-- alice, bob, carol, dave

External links

Exercise

messages 테이블에서 계산: (1) 총 row, (2) 고유 conversation_id, (3) per-conversation message 개수, (4) per-conversation 가장 빠른/늦은 created_at. 각각 single SELECT 로, 수동 sample 로 확인. DISTINCT 와 GROUP BY 어디서 어떻게 썼는지 기록.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.