The Fifth Lab
Query Statements in PostgreSQL
(I) Query on Stations
SELECT * FROM stations WHERE station LIKE '%Boston%';
(II) Query on Trips
SELECT COUNT(*) AS COUNT From trips WHERE trips.strt_statn != trips.end_statn;
(III) Query on both Stations and Trips
(IV) Solutions
FROM stations
WHERE station LIKE '%Boston%';
FROM stations
WHERE municipal = 'Brookline' AND status = 'Existing';
FROM stations
GROUP BY municipal;
FROM stations;
From trips
WHERE trips.strt_statn != trips.end_statn;
From trips;
FROM trips
WHERE bike_nr != ''
GROUP BY bike_nr
ORDER BY COUNT DESC
LIMIT 5;
INTO COUNT_TABLE
FROM trips
WHERE bike_nr != ''
GROUP BY bike_nr;
SELECT AVG(COUNT) FROM COUNT_TABLE;
FROM trips
WHERE birth_date > 0
GROUP BY birth_date
ORDER BY count DESC
LIMIT 5;
FROM trips
GROUP BY DATE_PART('hour', start_date)
ORDER BY DATE_PART('hour', start_date) ASC;
FROM trips
GROUP BY DATE_PART('hour', end_date)
ORDER BY DATE_PART('hour', end_date) ASC;
FROM stations, trips
WHERE stations.id = trips.strt_statn
AND trips.strt_statn != trips.end_statn
GROUP BY station
ORDER BY COUNT DESC
LIMIT 5;
FROM stations, trips
WHERE stations.id = trips.end_statn
AND trips.strt_statn != trips.end_statn
GROUP BY station
ORDER BY COUNT DESC
LIMIT 5; )
FROM stations AS S1, stations AS S2, trips
WHERE S1.id = trips.strt_statn
AND S2.id = trips.end_statn
AND trips.strt_statn != trips.end_statn
GROUP BY S1.station, S2.station
ORDER BY COUNT DESC
LIMIT 5;