--- id: sql-case title: "SQL Case" category: "Database" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["SQL CASE Expression", "CASE expression", "SQL 쑰건식"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["sql", "database", "w3schools", "case", "conditional"] raw_sources: ["https://www.w3schools.com/sql/sql_case.asp"] applied_in: [] github_commit: "" --- # [[SQL Case]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) CASE works like an if-then-else chain inside a query β€” it stops at the first true condition, falls back to ELSE, and returns NULL if nothing matches and there's no ELSE. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **CASE expression** β€” defines different results based on specified conditions within an SQL statement. [S1] - **Short-circuit evaluation** β€” stops at the first true condition and returns its result, like an if-then-else statement. [S1] - **ELSE fallback** β€” if no condition is true, returns the ELSE value; without ELSE and no match, returns NULL. [S1] - **Syntax** β€” `CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END;`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Categorize products by price into a new derived column: [S1] ```sql SELECT ProductName, Price, CASE WHEN Price < 20 THEN 'Low Cost' WHEN Price BETWEEN 20 AND 50 THEN 'Medium Cost' ELSE 'High Cost' END AS PriceCategory FROM Products; ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 가격 κ΅¬κ°„μ²˜λŸΌ 값을 λ²”μ£Όν™”ν•˜λŠ” νŒŒμƒ μ»¬λŸΌμ„ λ§Œλ“€ λ•Œ μ „ν˜•μ μœΌλ‘œ 쓰인닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) CASE for value categorization (SQL): ```sql SELECT ProductName, Price, CASE WHEN Price < 20 THEN 'Low Cost' WHEN Price BETWEEN 20 AND 50 THEN 'Medium Cost' ELSE 'High Cost' END AS PriceCategory FROM Products; ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[SQL Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[SQL Between]], [[SQL Aliases]], [[SQL Null Functions]] - **μ°Έμ‘° λ§₯락:** κ°’ 기반 λΆ„κΈ°/λ²”μ£Όν™” νŒŒμƒ 컬럼이 ν•„μš”ν•œ λͺ¨λ“  쿼리의 κΈ°λ³Έ 쑰건식. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” SQL CASE Expression β€” https://www.w3schools.com/sql/sql_case.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "SQL CASE Expression" page (Astra wiki-curation, P-Reinforce v3.1 format).