-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
45 lines (41 loc) · 1.44 KB
/
queries.sql
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
-- IMPLICIT JOIN
SELECT tpep_pickup_datetime,
tpep_dropoff_datetime,
total_amount,
CONCAT(zpu."Borough", ' / ', zpu."Zone") AS "pickup_location",
CONCAT(zdo."Borough", ' / ', zdo."Zone") AS "dropoff_location"
FROM yellow_taxi_data t,
zones zpu,
zones zdo
WHERE t."PULocationID" = zpu."LocationID"
AND t."DOLocationID" = zdo."LocationID"
LIMIT 100;
-- INNER JOIN
SELECT tpep_pickup_datetime,
tpep_dropoff_datetime,
total_amount,
CONCAT(zpu."Borough", ' / ', zpu."Zone") AS "pickup_location",
CONCAT(zdo."Borough", ' / ', zdo."Zone") AS "dropoff_location"
FROM yellow_taxi_data t
JOIN zones zpu ON t."PULocationID" = zpu."LocationID"
JOIN zones zdo ON t."DOLocationID" = zdo."LocationID"
LIMIT 100;
-- NESTED QUERIES
-- Checking whether there is a PICKUP location in yellow_taxi_data not included in the zones lookup table
SELECT *
FROM yellow_taxi_data t
WHERE "PULocationID" NOT IN (SELECT "LocationID" FROM zones)
LIMIT 100;
-- NESTED QUERIES
-- Checking whether there is a DROPOFF location in yellow_taxi_data not included in the zones lookup table
SELECT *
FROM yellow_taxi_data t
WHERE "DOLocationID" NOT IN (SELECT "LocationID" FROM zones)
LIMIT 100;
-- GROUP BY: Number of trips per day
SELECT CAST(tpep_dropoff_datetime AS DATE) AS "day",
COUNT(1) AS "trips_count"
FROM yellow_taxi_data t
GROUP BY CAST(tpep_dropoff_datetime AS DATE)
ORDER BY trips_count DESC
LIMIT 10;