CS 383
Lab 2
SQL Queries and Postgres
Work on the following for no more and 80 minutes. I do not expect you complete all of queries below; the goal is just to get practice.
Answers, or at least partial answers, are included for the first few queries. Also, correct queries are in a separate link from the class web site. I encourage you to not look at the correct queries except to check those you wrote yourself.
PostgreSQL
- Connect to the flight database
- What tables are in this database?
- What is the structure of each table. Hereis my UML-ish sketch for the structure
For the queries below, create a file (xxx.sql) that holds them all.
- How many rows are in each table? (correct answers are: flights=196431, carriers=5621, airports=1948)
- List all departure flights, sorted by departure delays. The first 3 rows should be
date | departuretime | arrivaltime | carrier | flightnum | arrivaldelay | departuredelay | origin | destination | distance | cancelled
------------+---------------+-------------+---------+-----------+--------------+----------------+--------+-------------+----------+-----------
2008-10-16 | 08:03:00 | 10:20:00 | NW | 1765 | 1392 | 1369 | PHL | DTW | 453 | f
2008-07-20 | 02:36:00 | 04:15:00 | FL | 927 | 1175 | 1206 | PHL | ATL | 665 | f
2008-06-13 | 07:25:00 | 09:11:00 | NW | 689 | 1009 | 1020 | PHL | MSP | 980 | f
- List all flights on Jan 3, 2008 The first three rows should be:
date | departuretime | arrivaltime | carrier | flightnum | arrivaldelay | departuredelay | origin | destination | distance | cancelled
------------+---------------+-------------+---------+-----------+--------------+----------------+--------+-------------+----------+-----------
2008-01-03 | 17:34:00 | 19:41:00 | WN | 23 | 36 | 44 | JAX | PHL | 742 | f
2008-01-03 | 07:12:00 | 09:26:00 | WN | 1232 | 11 | 12 | JAX | PHL | 742 | f
2008-01-03 | 11:27:00 | 18:56:00 | WN | 1285 | 21 | 42 | LAS | PHL | 2176 | f
- The number of arrivals into PHL in March. (You will need to look up how Postgres does dates) (8457)
- The number of departures from PHL on the 5 day of a month. (You will need to look up how Postgres does dates) (3199)
- The number of departures from PHL on the 5 day of a month between 10am and 10:59am (inclusive). (You will need to look up how Postgres does times.) (186)
- List all different departure delays (without duplication)
- How many different departure delays are there? (486).
- What was the largest departure delay? The query should have a single response. Write this query in two ways
- What flight number(s) had the largest delay? Write this query by just putting the largest delay into the query.
- What is the smallest arrival delay? (actual delay, not early or on-time)
- How main times did the smallest delay happen?
- On which flight numbers did this arrival delay happen? for each of those flight numbers, how many times did this arrival delay happen?
-
Which is greater, the number of destinations or the number of origins?
Use two queries
- How many carriers are in this database. How many actually fly into PHL? (Out of PHL?)
- How many carriers fly from each location from which you can fly directly to PHL?
- How many flights go past midnight?
- How many flights from each origin landed in PHL on the day after they took off?
What to turn in.
Send email to gtowell@brynmawr.edu with the last query you got working.