how to use sub-sequence
Tablenumber
contains many numbers in columnnumincluding duplicated ones.
Can you write a SQL query to find the biggest number, which only appears once.
+---+
|num|
+---+
| 8 |
| 8 |
| 3 |
| 3 |
| 1 |
| 4 |
| 5 |
| 6 |
For the sample data above, your query should return the following result:
+---+
|num|
+---+
| 6 |
Note:
If there is no such number, just output
null
.
Solution
Approach: UsingsubqueryandMAX()
function [Accepted]
Algorithm
Use subquery to select all the numbers appearing just one time.
Then choose the biggest one usingMAX()
.
select num
from number
GROUP BY num
having count(num) = 1
select MAX(num) as num
from (
select num
from number
GROUP BY num
having count(num) = 1
)