-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExpert-SQL25.sql
More file actions
33 lines (29 loc) · 872 Bytes
/
DataExpert-SQL25.sql
File metadata and controls
33 lines (29 loc) · 872 Bytes
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
-- Question: Which states have high risk of fatal collisions involving Alcohol
-- https://www.dataexpert.io/question/high-risk-state-analysis
-- Approach 1: Using CTE to calculate national average first
WITH alcohol_impaired AS (
SELECT
state,
percent_alcohol_impaired,
AVG(percent_alcohol_impaired) OVER () AS national_avg
FROM playground.bad_drivers
)
SELECT
state,
percent_alcohol_impaired
FROM alcohol_impaired
WHERE percent_alcohol_impaired > national_avg * 1.20
ORDER BY percent_alcohol_impaired DESC, state ASC;
-- Approach 2: Using subquery in WHERE clause
SELECT
state,
percent_alcohol_impaired
FROM
playground.bad_drivers
WHERE
percent_alcohol_impaired >= 1.2 * (
SELECT AVG(percent_alcohol_impaired)
FROM playground.bad_drivers
)
ORDER BY
percent_alcohol_impaired DESC;