๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/LeetCode

[SQL50] #28. Biggest Single Number

by ์ด๋ฎด 2024. 1. 7.
728x90

๐Ÿ’ป ๋ฌธ์ œ ์ฃผ์†Œ : https://leetcode.com/problems/biggest-single-number/description/?envType=study-plan-v2&envId=top-sql-50

 

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

 

๐Ÿ’ป ๋ฌธ์ œ

A single number is a number that appeared only once in the MyNumbers table. Find the largest single number. If there's no single number, report null.

Input๊ณผ Output ์˜ˆ์‹œ

 

๐Ÿ’ป ์ฝ”๋“œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ

๋”๋ณด๊ธฐ
select max(num) as num
from 
	(select num from MyNumbers group by num having count(num) = 1) as a

 

๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

์ด๋ฒˆ ๋ฌธ์ œ๋Š” Easy ๋‚œ์ด๋„์˜€์Œ์—๋„ ๊ฝค๋‚˜ ์˜ค๋ฅ˜๋ฅผ ๋งŽ์ด ๊ฒช์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. ์ด๋Š” ๋ฌธ์ œ ์กฐ๊ฑด์ธ max ๊ฐ’ ๋˜๋Š” null ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฐœ์ƒํ–ˆ๋˜ ์ด์Šˆ๋กœ, ์ฐจ๊ทผ์ฐจ๊ทผ ํ’€์ด ๊ณผ์ •์„ ์˜ฎ๊น€์œผ๋กœ์จ ๋ฌธ์ œ ํ’€์ด๋ฅผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๋˜์ƒˆ๊ธฐ๊ณ ์ž ํ•œ๋‹ค.

1๏ธโƒฃ Single number : appeared only 1 time in the MyNumbers table
2๏ธโƒฃ Find the largest single number. If there's no single number, then report 'null'

 

1๏ธโƒฃ Single number ์ฐพ๊ธฐ

๋ฌธ์ œ์—์„œ Single number๋ž€, MyNumbers๋ผ๋Š” ํ…Œ์ด๋ธ”์— ์˜ค์ง ํ•œ ๋ฒˆ๋งŒ ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ์ˆซ์ž๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋Š” ๊ฒƒ์€ ๊ฐ ์ˆซ์ž๋ณ„๋กœ ๋ช‡ ๋ฒˆ ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š”์ง€ ๊ทธ ์ˆ˜๋ฅผ count()ํ•˜๋ฉด ๋œ๋‹ค๋Š” ๋œป์ด๊ธฐ ๋•Œ๋ฌธ์— group by๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

select num from MyNumbers group by num having count(num) = 1

 

2๏ธโƒฃ Find the largest single number or 'null'

1๏ธโƒฃ๊นŒ์ง€ ํ•˜๋ฉด Single number์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋“ค์ด ํ•„ํ„ฐ๋ง๋˜์–ด select์— ์˜ํ•ด ์ถ”์ถœ๋  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ์ด์ค‘์—์„œ๋„ '๊ฐ€์žฅ ํฐ' single number์„ ์ถ”์ถœํ•ด์•ผ ํ•˜๋Š”๋ฐ ๋งŒ์•ฝ single number๊ฐ€ ์—†๋‹ค๋ฉด null์„ ๋Œ€์‹  ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ์œ„ 1๏ธโƒฃ๋‹จ๊ณ„์—์„œ select์— ifnull ๋“ฑ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์ด๋ฏธ group by๋ฅผ ํ†ตํ•ด ์ง‘๊ณ„๋œ ๊ฒฐ๊ณผ๋ฌผ์—์„œ having์ ˆ์„ ํ†ตํ•ด ํ•„ํ„ฐ๋ง์ด ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— null์ธ ๊ฐ’์ด ๋‚˜์˜ฌ ์ˆ˜๊ฐ€ ์—†๋‹ค (* case2).

 

๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” 1๏ธโƒฃ์—์„œ ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ๋ฅผ from ์ ˆ์— ๋“ค์–ด๊ฐˆ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์‚ฌ์šฉํ•˜์—ฌ, ์ด ์„œ๋ธŒ์ฟผ๋ฆฌ ๋‚ด์—์„œ single number๊ฐ€ ์žˆ์œผ๋ฉด ๊ทธ๊ฒƒ์„ ์ถœ๋ ฅํ•˜๊ณ , ์—†์œผ๋ฉด ์ž๋™์œผ๋กœ null์ด ์ถœ๋ ฅ๋˜๊ฒŒ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

select max(num)
from
	(select num from MyNumbers group by num having count(num) = 1)

 

728x90
๋ฐ˜์‘ํ˜•

'SQL > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SQL50] #29. Customers Who Bought All Products  (1) 2024.01.09
[SQL50] #27. Find Followers Count  (2) 2024.01.06
[SQL50] #26. Classes More Than 5 Students  (1) 2024.01.05
[LeetCode][SQL50] #25. Product Sales Analysis III  (1) 2024.01.04
[LeetCode][SQL50] #24. User Activity for the Past 30 Days I  (1) 2024.01.04