Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions 11_extraweek/BigQuery_lab.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

-- Using the NYC Taxi public dataset (Yellow Trips) from Google BigQuery, complete the following exercises:



-- Exercise 1: Count the number of trips in January 2021
SELECT COUNT(*)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`;

-- Exercise 2: Calculate the total revenue generated by taxi trips in 2021
SELECT SUM(total_amount)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`

-- Exercise 3: Find the most popular pickup location
SELECT pickup_location_id,
COUNT(*) AS trip_count
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
GROUP BY pickup_location_id
ORDER BY trip_count DESC;

-- Exercise 4: Analyze the number of trips per hour of the day
SELECT pickup_location_id,
COUNT(*) AS trip_count,
EXTRACT(DAY FROM pickup_datetime) AS day,
EXTRACT(HOUR FROM pickup_datetime) AS hour
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
GROUP BY pickup_location_id, day, hour
ORDER BY pickup_location_id, day, hour;

-- Exercise 5: Calculate the average trip distance
SELECT AVG(trip_distance)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`;

-- Exercise 6: Find the longest trip by distance
SELECT pickup_location_id, MAX(trip_distance) AS longest_trip
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
GROUP BY pickup_location_id
ORDER BY longest_trip DESC;


SELECT pickup_location_id, trip_distance
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
ORDER BY trip_distance DESC;


-- Exercise 7: Calculate the total number of passengers by payment type
SELECT payment_type, SUM(passenger_count) AS passenger_count
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
GROUP BY payment_type
ORDER BY payment_type;

-- Exercise 8: Find the most common drop-off location for trips paid by credit card
SELECT dropoff_location_id,
COUNT(*) AS trip_count,
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
WHERE payment_type = '1'
GROUP BY dropoff_location_id
ORDER BY trip_count DESC;

-- Exercise 9: Calculate the total number of trips that had more than 4 passengers
SELECT
COUNT(*) AS trip_count,
SUM(passenger_count) AS passenger_count
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
WHERE passenger_count > 4;

-- Exercise 10: Subquery - Find the average fare for trips longer than the average trip distance

SELECT AVG(fare_amount) AS avg_fare
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`
WHERE trip_distance > (
SELECT AVG(trip_distance)
FROM `bigquery-public-data.new_york_taxi_trips.tlc_yellow_trips_2021`);
Loading