0% found this document useful (0 votes)
68 views2 pages

Biggest Single Number - SQL

This document contains the SQL code to solve a Leetcode challenge of finding the biggest number that appears only once in a table of numbers that may include duplicates. The code uses a subquery to first group the numbers by count and filter for any that have a count of 1, i.e. appear only once. It then takes the maximum of this result to return the single, biggest number.

Uploaded by

abhishek pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Biggest Single Number - SQL

This document contains the SQL code to solve a Leetcode challenge of finding the biggest number that appears only once in a table of numbers that may include duplicates. The code uses a subquery to first group the numbers by count and filter for any that have a count of 1, i.e. appear only once. It then takes the maximum of this result to return the single, biggest number.

Uploaded by

abhishek pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL-Leetcode-Challenge/Biggest Single number.sql at master · naiborh... https://github.com/naiborhujosua/SQL-Leetcode-Challenge/blob/master...

naiborhujosua / !"#
$#%
%&'
()%$
*+,-
-
%./% !"#$
%
&

forked from mrinal1704/SQL-Leetcode-Challenge

Pull requests Actions Projects Security Insights

'(
)*+
,

/ Easy /

01
2.,-
3456Attempted 5 more today 72
8&(1
9

3contributor

32 lines (30 sloc) 586 Bytes

1 -- Question 24
2 -- Table my_numbers contains many numbers in column num including duplicated ones.
3 -- Can you write a SQL query to find the biggest number, which only appears once.
4
5 -- +---+
6 -- |num|
7 -- +---+
8 -- | 8 |
9 -- | 8 |
10 -- | 3 |
11 -- | 3 |
12 -- | 1 |
13 -- | 4 |
14 -- | 5 |
15 -- | 6 |
16 -- For the sample data above, your query should return the following result:
17 -- +---+
18 -- |num|
19 -- +---+
20 -- | 6 |
21 -- Note:
22 -- If there is no such number, just output null.
23
24 -- Solution
25 Select max(a.num) as num
26 from
27 (

1 of 2 16/09/22, 12:10 am
SQL-Leetcode-Challenge/Biggest Single number.sql at master · naiborh... https://github.com/naiborhujosua/SQL-Leetcode-Challenge/blob/master...

28 select num, count(*)


29 from my_numbers
30 group by num
31 having count(*)=1
32 ) a

2 of 2 16/09/22, 12:10 am

You might also like