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

Basic/[SQL] Leetcode ํ’€์ด๋…ธํŠธ

[LeetCode | Easy] 584. Find Customer Referee

๐Ÿ”ป ๋ฌธ์ œ ๋งํฌ

https://leetcode.com/problems/find-customer-referee/description/?envType=study-plan&id=sql-i 

 

Find Customer Referee - LeetCode

Can you solve this real interview question? Find Customer Referee - Table: Customer +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | | referee_id | int | +-------------+---------+ id is the primary

leetcode.com

 

๐Ÿ”ป ๋ฌธ์ œ ํŒŒ์•…

[๊ณ ๊ฐ id]-[์ด๋ฆ„]-[์ถ”์ฒœ์ธ id]๊ฐ€ ๋‹ด๊ธด ๊ณ ๊ฐ ์ •๋ณด ํ…Œ์ด๋ธ”์ด๋‹ค.

์ถ”์ฒœ์ธ id๊ฐ€ 2๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ ํ•ด์•ผํ•œ๋‹ค. ์ฃผ์˜ํ•  ์ ์€ referee_id์— null๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ!

 

๐Ÿ”ป ๋ฌธ์ œ ํ•ด๊ฒฐ

  • SELECT~ FROM ์œผ๋กœ ์กฐํšŒํ•  ํ–‰์„ ์„ ํƒ
  • WHERE๋กœ ์กฐ๊ฑด ์ž…๋ ฅ
    • referee_id๊ฐ€ 2๊ฐ€ ์•„๋‹ˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— <> ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์ค€๋‹ค.
    • null๊ฐ’์€ '๊ฐ’์ด ์—†๋‹ค'๋Š” ์˜๋ฏธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์ฟผ๋ฆฌ์— null๊ฐ’๋„ ํฌํ•จ๋จ์„ ๋ช…์‹œํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
SELECT name
FROM customer
WHERE (referee_id<>2) or (referee_id is NULL);