๐ป ๋ฌธ์ ๋งํฌ
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);
'Basic > [SQL] Leetcode ํ์ด๋ ธํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode | Easy] 196. Delete Duplicate Emails (0) | 2023.03.19 |
---|---|
[LeetCode | Easy] 627. Swap Salary (0) | 2023.03.19 |
[LeetCode | Easy] 183. Customers Who Never Order (0) | 2023.03.17 |
[LeetCode | Easy] 595. Big Countries (0) | 2023.03.17 |
[LeetCode | Easy] 1757. Recyclable and Low Fat Products (0) | 2023.03.17 |