[Leet code] 619. Biggest Single Number
๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ Problems, https://leetcode.com/problemset/all/์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://leetcode.com/problems/biggest-single-number/
Biggest Single Number - LeetCode
Can you solve this real interview question? Biggest Single Number - Table: MyNumbers +-------------+------+ | Column Name | Type | +-------------+------+ | num | int | +-------------+------+ This table may contain duplicates (In other words, there is no pr
leetcode.com
Table : MyNumbers
A single number is a number that appeared only once in the MyNumbers table.
Find the largest single number. If there is no single number, report null.
The result format is in the following example.
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
๋จ์ผ๊ฐ์ธ ์ซ์์ค ๊ฐ์ฅ ํฐ ์ซ์๋ฅผ ์ถ๋ ฅํด์ผํ๋๋ฐ ๋ง์ฝ ๋จ์ผ๊ฐ์ธ ์ซ์๊ฐ ์๋ค๋ฉด null์ ์ถ๋ ฅํด์ผ ํ๋ค.
โ ์ซ์๋ณ ๊ฐ์(cnt)๋ฅผ ๊ตฌํด์ > group by์
โก ์ซ์๋ณ ๊ฐ์๊ฐ 1์ธ ๊ฒ์ ์ฐพ๊ณ > having์
โข < โก > num ์ค์ ์ต๋ num์ ์ฐพ๋๋ค.
SELECT MAX(num) AS num
FROM (
SELECT num, COUNT(*) AS cnt
FROM MyNumbers
GROUP BY 1
HAVING COUNT(*) = 1
) T1
;
FROM์ ์ ์๋ธ์ฟผ๋ฆฌ์์ HAVING์ ์ ์ฌ์ฉํ์ง ์๊ณ ๋ฉ์ธ SELECT์ ์์ WHERE์ ๋ก ๋จ์ผ๊ฐ์ ์กฐ๊ฑด์ ์ค์ ํด์ค๋ ๊ฒฐ๊ณผ๊ฐ์ ๋์ผํ๊ฒ ์ถ๋ ฅํ ์ ์๋ค.
SELECT MAX(num) AS num
FROM (
SELECT num, COUNT(*) AS cnt
FROM MyNumbers
GROUP BY 1
) T1
WHERE cnt = 1
;