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
40 changes: 25 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,44 @@ Write a query to answer each of the questions below.

**Result:** 300,432

2. [What is the percent change in trips in Q3 2022 as compared to Q3 2021?](query02.sql)

2. [What is the percent change in trips in Q3 2022 as compared to Q3 2021?](query02.sql)
Result: 3.98%
3. [What is the average duration of a trip for 2021?](query03.sql)

18.86 minutes
4. [What is the average duration of a trip for 2022?](query04.sql)

17.88 minutes
5. [What is the longest duration trip across the two quarters?](query05.sql)
1440mins

_Why are there so many trips of this duration?_

**Answer:**
**1440 minutes equals exactly 24 hours. This likely indicates bikes that were not returned on time or were checked out but not properly closed in the system. The system may have a default maximum duration of 24 hours for billing purposes, causing all overdue rentals to be capped at this duration. This could also represent data anomalies or system errors where bike checkouts were not properly recorded as completed.**

6. [How many trips in each quarter were shorter than 10 minutes?](query06.sql)

Q3 2021: 27,121 trips
Q3 2022: 28,122 trips
7. [How many trips started on one day and ended on a different day?](query07.sql)

Q3 2021: 2,301 trips
Q3 2022: 2,060 trips
8. [Give the five most popular starting stations across all years between 7am and 9:59am.](query08.sql)

_Hint: Use the `EXTRACT` function to get the hour of the day from the timestamp._

Station 3032: 1,828 trips
Station 3102: 1,689 trips
Station 3012: 1,614 trips
Station 3066: 1,529 trips
Station 3007: 1,434 trips
9. [List all the passholder types and number of trips for each across all years.](query09.sql)

Indego30: 441,856 trips
Indego365: 109,251 trips
Day Pass: 61,659 trips
NULL: 43 trips
Walk-up: 2 trips
10. [Using the station status dataset, find the distance in meters of each station from Meyerson Hall.](query10.sql)

All 293 stations with their distances from Meyerson Hall (in meters, rounded to nearest 50m)
11. [What is the average distance (in meters) of all stations from Meyerson Hall?](query11.sql)

Result: 3 km (approximately 3,000 meters)
12. [How many stations are within 1km of Meyerson Hall?](query12.sql)

Result: 18 stations
13. [Which station is furthest from Meyerson Hall?](query13.sql)

Manayunk & Conarroe, Fairview Park (Station ID: 3432) - 9,000 meters (9 km away)
14. [Which station is closest to Meyerson Hall?](query14.sql)
34th & Spruce (Station ID: 3208)
14 changes: 13 additions & 1 deletion query02.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
-- Enter your SQL query here



/*
If you want to get fancier here, you can cast the result to a string and
concatenate a '%' to the end. For example:
Expand All @@ -22,3 +21,16 @@
This uses the type casting (number to string) and string concatenation
operator (`||`, double pipes) that's essentially a `+` for strings.
*/


SELECT
CAST(
ROUND(
CAST(
(((SELECT COUNT(*) FROM indego.trips_2022_q3) - (SELECT COUNT(*) FROM indego.trips_2021_q3)) / CAST((SELECT COUNT(*) FROM indego.trips_2021_q3) AS FLOAT)) * 100
AS NUMERIC
),
2
)
AS FLOAT
) AS perc_change;
2 changes: 2 additions & 0 deletions query03.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
*/

-- Enter your SQL query here
SELECT ROUND(AVG(duration), 2) AS avg_duration
FROM indego.trips_2021_q3;
3 changes: 3 additions & 0 deletions query04.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
*/

-- Enter your SQL query here

SELECT ROUND(AVG(duration), 2) AS avg_duration
FROM indego.trips_2022_q3;
6 changes: 6 additions & 0 deletions query05.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@
*/

-- Enter your SQL query here
SELECT MAX(duration) AS max_duration
FROM (
SELECT duration FROM indego.trips_2021_q3
UNION ALL
SELECT duration FROM indego.trips_2022_q3
) AS combined_trips;
15 changes: 15 additions & 0 deletions query06.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@
*/

-- Enter your SQL query here
SELECT
2021 AS trip_year,
'Q3' AS trip_quarter,
COUNT(*) AS num_trips
FROM indego.trips_2021_q3
WHERE duration < 10

UNION ALL

SELECT
2022 AS trip_year,
'Q3' AS trip_quarter,
COUNT(*) AS num_trips
FROM indego.trips_2022_q3
WHERE duration < 10;
16 changes: 15 additions & 1 deletion query07.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
-- Enter your SQL query here



/*

Hint 1: when you cast a TIMESTAMP to a DATE the time component of the value is simply stripped off
Expand All @@ -17,3 +16,18 @@
[EXTRACT](https://www.postgresql.org/docs/12/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT)
function.
*/
SELECT
2021 AS trip_year,
'Q3' AS trip_quarter,
COUNT(*) AS num_trips
FROM indego.trips_2021_q3
WHERE CAST(start_time AS DATE) != CAST(end_time AS DATE)

UNION ALL

SELECT
2022 AS trip_year,
'Q3' AS trip_quarter,
COUNT(*) AS num_trips
FROM indego.trips_2022_q3
WHERE CAST(start_time AS DATE) != CAST(end_time AS DATE);
17 changes: 17 additions & 0 deletions query08.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,20 @@
Hint: Use the `EXTRACT` function to get the hour of the day from the
timestamp.
*/
SELECT
s.id AS station_id,
s.geog AS station_geog,
COUNT(*) AS num_trips
FROM (
SELECT start_station FROM indego.trips_2021_q3
WHERE EXTRACT(HOUR FROM start_time) >= 7
AND EXTRACT(HOUR FROM start_time) < 10
UNION ALL
SELECT start_station FROM indego.trips_2022_q3
WHERE EXTRACT(HOUR FROM start_time) >= 7
AND EXTRACT(HOUR FROM start_time) < 10
) trips
JOIN indego.station_statuses s ON CAST(trips.start_station AS INTEGER) = s.id
GROUP BY s.id, s.geog
ORDER BY num_trips DESC
LIMIT 5;
10 changes: 10 additions & 0 deletions query09.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@
*/

-- Enter your SQL query here
SELECT
passholder_type,
COUNT(*) AS num_trips
FROM (
SELECT passholder_type FROM indego.trips_2021_q3
UNION ALL
SELECT passholder_type FROM indego.trips_2022_q3
) AS combined_trips
GROUP BY passholder_type
ORDER BY num_trips DESC;
10 changes: 10 additions & 0 deletions query10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@
*/

-- Enter your SQL query here

SELECT
id AS station_id,
geog AS station_geog,
ROUND(ST_DISTANCE(
geog,
ST_SETSRID(ST_MAKEPOINT(-75.192584, 39.952415), 4326)::GEOGRAPHY
) / 50) * 50 AS distance
FROM indego.station_statuses
ORDER BY distance ASC;
8 changes: 8 additions & 0 deletions query11.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
*/

-- Enter your SQL query here
SELECT
ROUND(AVG(
111.111 * SQRT(
POWER(lat - 39.952415, 2)
+ POWER((lon - (-75.192584)) * COS(RADIANS(lat)), 2)
)
)) AS avg_distance_km
FROM indego.station_statuses;
6 changes: 6 additions & 0 deletions query12.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
*/

-- Enter your SQL query here
SELECT COUNT(*) AS num_stations
FROM indego.station_statuses
WHERE ST_DISTANCE(
geog,
ST_SETSRID(ST_MAKEPOINT(-75.192584, 39.952415), 4326)::GEOGRAPHY
) < 1000;
11 changes: 11 additions & 0 deletions query13.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@
*/

-- Enter your SQL query here

SELECT
id AS station_id,
name AS station_name,
ROUND(111.111 * SQRT(
POWER(lat - 39.952415, 2)
+ POWER((lon - (-75.192584)) * COS(RADIANS(lat)), 2)
) * 1000 / 50) * 50 AS distance
FROM indego.station_statuses
ORDER BY distance DESC
LIMIT 1;
10 changes: 10 additions & 0 deletions query14.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@
*/

-- Enter your SQL query here
SELECT
id AS station_id,
name AS station_name,
ROUND(111.111 * SQRT(
POWER(lat - 39.952415, 2)
+ POWER((lon - (-75.192584)) * COS(RADIANS(lat)), 2)
) * 1000 / 50) * 50 AS distance
FROM indego.station_statuses
ORDER BY distance ASC
LIMIT 1;
Loading