-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-aggregation.sql
More file actions
179 lines (144 loc) · 5.34 KB
/
sql-aggregation.sql
File metadata and controls
179 lines (144 loc) · 5.34 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
-- Revising Aggregations - The Count Function
-- Query a count of the number of cities in CITY having a Population larger than .
SELECT COUNT(NAME)
FROM CITY
WHERE POPULATION > 100000;
-- Revising Aggregations - The Sum Function
-- Query the total population of all cities in CITY where District is California.
SELECT SUM(POPULATION)
FROM CITY
WHERE DISTRICT = 'California';
-- Revising Aggregations - Averages
-- Query the average population of all cities in CITY where District is California.
SELECT AVG(POPULATION)
FROM CITY
WHERE DISTRICT = 'California';
-- Average Population
-- Query the average population for all cities in CITY, rounded down to the nearest integer.
SELECT ROUND(AVG(POPULATION))
FROM CITY;
-- Japan Population
-- Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.
SELECT SUM(POPULATION)
FROM CITY
WHERE COUNTRYCODE = 'JPN';
-- Population Density Difference
-- Query the difference between the maximum and minimum populations in CITY.
SELECT MAX(POPULATION) - MIN(POPULATION)
FROM CITY;
-- The Blunder
/* Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard's key was broken until after completing the calculation. She wants your help finding the difference between her miscalculation (using salaries with any zeroes removed), and the actual average salary.
Write a query calculating the amount of error (i.e.: average monthly salaries), and round it up to the next integer.
*/
SELECT CEILING(AVG(Salary) - AVG(REPLACE(Salary,'0','')))
FROM EMPLOYEES;
-- Top Earners
/* We define an employee's total earnings to be their monthly salary x months worked,
and the maximum total earnings to be the maximum total earnings for any employee in the Employee table.
Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings.
Then print these values as space-separated integers.
*/
SELECT (salary * months) as earnings, count(*)
FROM employee
GROUP BY earnings
ORDER BY earnings DESC
LIMIT 1;
-- Weather Observation Station 2
/*
Query the following two values from the STATION table:
1. The sum of all values in LAT_N rounded to a scale of decimal places.
2. The sum of all values in LONG_W rounded to a scale of decimal places.
*/
SELECT ROUND(SUM(LAT_N), 2), ROUND(SUM(LONG_W), 2)
FROM STATION;
-- Weather Observation Station 13
/*
Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than 38.7880
and less than 137.2345.
Truncate your answer to 4 decimal places.
*/
SELECT ROUND(SUM(LAT_N),4) as lat_sum
FROM STATION
WHERE LAT_N > 38.7880
AND LAT_N < 137.2345;
-- Weather Observation Station 14
/*
Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345.
Truncate your answer to 4 decimal places.
*/
SELECT ROUND(MAX(LAT_N), 4)
FROM STATION
WHERE LAT_N < 137.2345;
-- Weather Observation Station 15
/*
Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is
less than 137.2345. Round your answer to 4 decimal places.
*/
SELECT ROUND(LONG_W, 4)
FROM STATION
WHERE LAT_N < 137.2345
ORDER BY LAT_N DESC
LIMIT 1;
-- Weather Observation Station 16
/*
Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7780.
Round your answer to decimal places.
*/
SELECT ROUND(LAT_N, 4)
FROM STATION
WHERE LAT_N > 38.7780
ORDER BY LAT_N
LIMIT 1;
-- Weather Observation Station 17
/*
Query the Western Longitude (LONG_W) where the smallest Northern Latitude (LAT_N) in STATION
is greater than 38.7780.
Round your answer to decimal places.
*/
SELECT ROUND(LONG_W, 4)
FROM STATION
WHERE LAT_N > 38.7780
ORDER BY LAT_N
LIMIT 1;
-- Weather Observation Station 18
/*
Consider P_1(a,b) and P_2(a,b) to be two points on a 2D plane.
happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
happens to equal the minimum value in Western Longitude (LONG_W in STATION).
happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
happens to equal the maximum value in Western Longitude (LONG_W in STATION).
Query the Manhattan Distance between points P_1 and P_2 and round it to a scale of 4 decimal places.
*/
SELECT ROUND(
ABS(MIN(LAT_N) - MAX(LAT_N)) +
ABS(MIN(LONG_W) - MAX(LONG_W)), 4
)
FROM STATION;
-- Weather Observation Station 19
/*
Consider P_1(a,c) and P_2(b,d) to be two points on a 2D plane.
where (a,b) are the respective minimum and maximum values of Northern Latitude (LAT_N) and
(c,d) are the respective minimum and maximum values of Western Longitude (LONG_W) in STATION.
Query the Euclidean Distance between points P_1 and P_2 and format your answer to display 4 decimal digits.
*/
SELECT ROUND(
SQRT(
POWER(MAX(LAT_N) - MIN(LAT_N), 2) +
POWER(MAX(LONG_W) - MIN(LONG_W), 2)
), 4
)
FROM STATION;
-- Weather Observation Station 20
/*
A median is defined as a number separating the higher half of a data set from the lower half.
Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to 4 decimal places.
*/
SELECT ROUND(S.LAT_N,4)
FROM STATION as S
WHERE (
SELECT COUNT(*)
FROM STATION
WHERE LAT_N < S.LAT_N ) = (
SELECT COUNT(*)
FROM STATION
WHERE LAT_N > S.LAT_N)