-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path619-BiggestSingleNumber.sql
More file actions
104 lines (99 loc) · 2.13 KB
/
619-BiggestSingleNumber.sql
File metadata and controls
104 lines (99 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
-- 619. Biggest Single Number
-- Table: MyNumbers
-- +-------------+------+
-- | Column Name | Type |
-- +-------------+------+
-- | num | int |
-- +-------------+------+
-- There is no primary key for this table. It may contain duplicates.
-- Each row of this table contains an integer.
-- A single number is a number that appeared only once in the MyNumbers table.
-- Write an SQL query to report the largest single number. If there is no single number, report null.
-- The query result format is in the following example.
-- Example 1:
-- Input:
-- MyNumbers table:
-- +-----+
-- | num |
-- +-----+
-- | 8 |
-- | 8 |
-- | 3 |
-- | 3 |
-- | 1 |
-- | 4 |
-- | 5 |
-- | 6 |
-- +-----+
-- Output:
-- +-----+
-- | num |
-- +-----+
-- | 6 |
-- +-----+
-- Explanation: The single numbers are 1, 4, 5, and 6.
-- Since 6 is the largest single number, we return it.
-- Example 2:
-- Input:
-- MyNumbers table:
-- +-----+
-- | num |
-- +-----+
-- | 8 |
-- | 8 |
-- | 7 |
-- | 7 |
-- | 3 |
-- | 3 |
-- | 3 |
-- +-----+
-- Output:
-- +------+
-- | num |
-- +------+
-- | null |
-- +------+
-- Explanation: There are no single numbers in the input table so we return null.
-- Create table If Not Exists MyNumbers (num int)
-- Truncate table MyNumbers
-- insert into MyNumbers (num) values ('8')
-- insert into MyNumbers (num) values ('8')
-- insert into MyNumbers (num) values ('3')
-- insert into MyNumbers (num) values ('3')
-- insert into MyNumbers (num) values ('1')
-- insert into MyNumbers (num) values ('4')
-- insert into MyNumbers (num) values ('5')
-- insert into MyNumbers (num) values ('6')
-- Write your MySQL query statement below
-- max
SELECT
MAX(num) AS num
FROM
( -- 求出一出现一次的所有数字
SELECT
num
FROM
MyNumbers
GROUP By
num
HAVING
COUNT(*) = 1
) AS a
-- limit
SELECT
num
FROM
( -- 求出一出现一次的所有数字
SELECT
num
FROM
MyNumbers
GROUP By
num
HAVING
COUNT(*) = 1
) AS a
ORDER BY
num DESC
LIMIT
1