Biggest Single Number Problem


Description

LeetCode Problem 619.

Table my_numbers contains many numbers in column num including duplicated ones. Can you write a SQL query to find the biggest number, which only appears once.

1
2
3
4
5
6
7
8
9
10
11
12
+---+
|num|
+---+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 |
+---+

For the sample data above, your query should return the following result:

1
2
3
4
5
+---+
|num|
+---+
| 6 |
+---+

Note: If there is no such number, just output null.


MySQL Solution

1
2
3
4
5
6
select max(num) as num
from
    (select num
    from my_numbers
    group by num
    having count(num) = 1) n




Related Posts

Exchange Seats Problem

LeetCode 626. Mary is a teacher in a middle school...

Customers Who Bought All Products Problem

LeetCode 1045. Write an SQL query for a report that...

Biggest Single Number Problem

LeetCode 619. Table my_numbers contains many numbers in column num...

Not Boring Movies Problem

LeetCode 620. X city opened a new cinema, many people...

Swap Salary Problem

LeetCode 627. Given a table salary, such as the one...

Actors And Directors Who Cooperated At Least Three Times Problem

LeetCode 1050. Write a SQL query for a report that...