File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed
Solutions/0619-biggest-single-number Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ <p >Table: <code >MyNumbers</code ></p >
2+
3+ <pre >
4+ +-------------+------+
5+ | Column Name | Type |
6+ +-------------+------+
7+ | num | int |
8+ +-------------+------+
9+ This table may contain duplicates (In other words, there is no primary key for this table in SQL).
10+ Each row of this table contains an integer.
11+ </pre >
12+
13+ <p >  ; </p >
14+
15+ <p >A <strong >single number</strong > is a number that appeared only once in the <code >MyNumbers</code > table.</p >
16+
17+ <p >Find the largest <strong >single number</strong >. If there is no <strong >single number</strong >, report <code >null</code >.</p >
18+
19+ <p >The result format is in the following example.</p >
20+ <ptable > </ptable >
21+ <p >  ; </p >
22+ <p ><strong class =" example " >Example 1:</strong ></p >
23+
24+ <pre >
25+ <strong >Input:</strong >
26+ MyNumbers table:
27+ +-----+
28+ | num |
29+ +-----+
30+ | 8 |
31+ | 8 |
32+ | 3 |
33+ | 3 |
34+ | 1 |
35+ | 4 |
36+ | 5 |
37+ | 6 |
38+ +-----+
39+ <strong >Output:</strong >
40+ +-----+
41+ | num |
42+ +-----+
43+ | 6 |
44+ +-----+
45+ <strong >Explanation:</strong > The single numbers are 1, 4, 5, and 6.
46+ Since 6 is the largest single number, we return it.
47+ </pre >
48+
49+ <p ><strong class =" example " >Example 2:</strong ></p >
50+
51+ <pre >
52+ <strong >Input:</strong >
53+ MyNumbers table:
54+ +-----+
55+ | num |
56+ +-----+
57+ | 8 |
58+ | 8 |
59+ | 7 |
60+ | 7 |
61+ | 3 |
62+ | 3 |
63+ | 3 |
64+ +-----+
65+ <strong >Output:</strong >
66+ +------+
67+ | num |
68+ +------+
69+ | null |
70+ +------+
71+ <strong >Explanation:</strong > There are no single numbers in the input table so we return null.
72+ </pre >
Original file line number Diff line number Diff line change 1+ SELECT DISTINCT MAX (num) AS num
2+ FROM (
3+ SELECT num
4+ FROM MyNumbers
5+ GROUP BY num
6+ HAVING COUNT (num) = 1
7+ ) AS T
You can’t perform that action at this time.
0 commit comments