-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path618-StudentsReportByGeography.sql
More file actions
80 lines (80 loc) · 2.14 KB
/
618-StudentsReportByGeography.sql
File metadata and controls
80 lines (80 loc) · 2.14 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
-- 618. Students Report By Geography
-- Table: Student
--
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | name | varchar |
-- | continent | varchar |
-- +-------------+---------+
-- There is no primary key for this table. It may contain duplicate rows.
-- Each row of this table indicates the name of a student and the continent they came from.
--
-- A school has students from Asia, Europe, and America.
--
-- Write an SQL query to pivot the continent column in the Student table so that each name is sorted alphabetically and displayed underneath its corresponding continent.
-- The output headers should be America, Asia, and Europe, respectively.
-- The test cases are generated so that the student number from America is not less than either Asia or Europe.
-- The query result format is in the following example.
--
-- Example 1:
--
-- Input:
-- Student table:
-- +--------+-----------+
-- | name | continent |
-- +--------+-----------+
-- | Jane | America |
-- | Pascal | Europe |
-- | Xi | Asia |
-- | Jack | America |
-- +--------+-----------+
-- Output:
-- +---------+------+--------+
-- | America | Asia | Europe |
-- +---------+------+--------+
-- | Jack | Xi | Pascal |
-- | Jane | null | null |
-- +---------+------+--------+
--
-- Follow up: If it is unknown which continent has the most students, could you write a query to generate the student report?
--
-- Write your MySQL query statement below
SELECT
a.America,
b.Asia,
c.Europe
FROM
(
SELECT
name AS America,
ROW_NUMBER() OVER(ORDER BY name ASC) AS r
FROM
Student
WHERE
continent = 'America'
) AS a
LEFT JOIN
(
SELECT
name AS Asia,
ROW_NUMBER() OVER(ORDER BY name ASC) AS r
FROM
Student
WHERE
continent = 'Asia'
) AS b
ON
a.r = b.r
LEFT JOIN
(
SELECT
name AS Europe,
ROW_NUMBER() OVER(ORDER BY name ASC) AS r
FROM
Student
WHERE
continent = 'Europe'
) AS c
ON
a.r = c.r