Skip to content
Snippets Groups Projects
Commit a762c8b4 authored by keleher's avatar keleher
Browse files

auto

parent e04734f2
Branches master
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
...@@ -12,10 +12,7 @@ order by city; ...@@ -12,10 +12,7 @@ order by city;
### 1. Write a query to find the names of the customers whose names are at least 15 characters long, and the second letter in the name is "l". ### 1. Write a query to find the names of the customers whose names are at least 15 characters long, and the second letter in the name is "l".
### Order by name. ### Order by name.
queries[1] = """ queries[1] = """
select name select 0;
from customers
where name like '_l%' and char_length(name) >= 15
order by name;
""" """
...@@ -23,68 +20,27 @@ order by name; ...@@ -23,68 +20,27 @@ order by name;
### Order output by Customer Name. ### Order output by Customer Name.
### Output columns: all columns from customers ### Output columns: all columns from customers
queries[2] = """ queries[2] = """
select distinct c.customerid, name, birthdate,frequentflieron select 0;
from customers c, flewon
where extract(month FROM birthdate) = extract(month FROM flightdate) and extract(day FROM birthdate) = extract(day FROM flightdate)
order by name;
""" """
### 3. Write a query to generate a list: (source_city, source_airport_code, dest_city, dest_airport_code, number_of_flights) for all source-dest pairs with at least 3 flights. ### 3. Write a query to generate a list: (source_city, source_airport_code, dest_city, dest_airport_code, number_of_flights) for all source-dest pairs with at least 3 flights.
### Order first by number_of_flights in decreasing order, then source_city in the increasing order, and then dest_city in the increasing order. ### Order first by number_of_flights in decreasing order, then source_city in the increasing order, and then dest_city in the increasing order.
### Note: You must generate the source and destination cities along with the airport codes. ### Note: You must generate the source and destination cities along with the airport codes.
queries[3] = """ queries[3] = """
with common_flights (flightid, source, dest, airlineid) as (select distinct a.flightid, a.source, a.dest, a.airlineid select 0;
from flights a, flights b
where (a.source = b.source) and (a.dest = b.dest) and (a.flightid != b.flightid)
order by a.source, a.dest),
common_count (source, dest, count) as
(select source, dest, count(*)
from common_flights
group by source, dest
having count(*) >= 3),
source_city (source_city, source_airport_code, dest_airport_code, number_of_flights) as
(select a.city as source_city, c.*
from common_count c left join airports a
on c.source = a.airportid)
select s.source_city, s.source_airport_code, a.city as dest_city, s.dest_airport_code, s.number_of_flights
from source_city s left join airports a
on s.dest_airport_code = a.airportid
order by number_of_flights desc, source_city, dest_city;
""" """
### 4. Find the name of the airline with the maximum number of customers registered as frequent fliers. ### 4. Find the name of the airline with the maximum number of customers registered as frequent fliers.
### Output only the name of the airline. If multiple answers, order by name. ### Output only the name of the airline. If multiple answers, order by name.
queries[4] = """ queries[4] = """
with counts (airlineid, ff_count) as (select frequentflieron, count(frequentflieron) as ff_count select 0;
from customers
group by frequentflieron
order by frequentflieron),
highest_ff (airlineid, ff_count) as (select airlineid, ff_count
from counts
where ff_count = (select max(ff_count) from counts))
select name
from highest_ff h join airlines a
on h.airlineid = a.airlineid
order by name;
""" """
### 5. For all flights from OAK to IAD, list the flight id, airline name, and the ### 5. For all flights from OAK to IAD, list the flight id, airline name, and the
### duration in hours and minutes. So the output will have 4 fields: flightid, airline name, ### duration in hours and minutes. So the output will have 4 fields: flightid, airline name,
### hours, minutes. Order by flightid. ### hours, minutes. Order by flightid.
queries[5] = """ queries[5] = """
with oak_flights (flightid, airlineid, duration) as (select flightid, airlineid, (local_arrival_time - local_departing_time) as duration select 0;
from flights
where source = 'OAK' and dest = 'IAD')
select flightid, name, extract(hour FROM duration) as hours, extract(minute FROM duration) as minutes
from oak_flights o join airlines a
on o.airlineid = a.airlineid
order by flightid;
""" """
### 6. Write a query to find empty flights (flight, flight date) on any date ### 6. Write a query to find empty flights (flight, flight date) on any date
...@@ -92,20 +48,7 @@ order by flightid; ...@@ -92,20 +48,7 @@ order by flightid;
### flights took off as scheduled, with or without passengers. Order by flight ### flights took off as scheduled, with or without passengers. Order by flight
### id in increasing order, and then by date in increasing order. ### id in increasing order, and then by date in increasing order.
queries[6] = """ queries[6] = """
with days_of_flights (flight_date) as (select distinct flightdate select 0;
from flewon),
cart_product (flightid, flight_date) as (select a.flightid, b.flight_date
from flights a, days_of_flights b
order by a.flightid, b.flight_date)
select *
from cart_product
except (select distinct b.flightid, flightdate
from flights a, flewon b
where a.flightid = b.flightid
order by b.flightid, flightdate)
order by flightid, flight_date;
""" """
### 7. Write a query to generate a list of customers who don't list Southwest as their frequent flier airline, but ### 7. Write a query to generate a list of customers who don't list Southwest as their frequent flier airline, but
...@@ -113,27 +56,7 @@ order by flightid, flight_date; ...@@ -113,27 +56,7 @@ order by flightid, flight_date;
### Output columns: customerid, customer_name ### Output columns: customerid, customer_name
### Order by: customerid ### Order by: customerid
queries[7] = """ queries[7] = """
with counts (customerid, airlineid, frequency) as (select a.customerid, b.airlineid, count(b.airlineid) as frequency select 0;
from flewon a left join flights b
on a.flightid = b.flightid
group by a.customerid, b.airlineid
order by a.customerid),
maximums (customerid, max) as (select counts.customerid, max(counts.frequency)
from counts
group by counts.customerid
order by counts.customerid),
max_airlines (customerid, airlineid) as (select a.customerid, a.airlineid, b.max
from counts a join maximums b
on a.customerid = b.customerid
where a.frequency = b.max)
select a.customerid, b.name as customer_name
from max_airlines a left join customers b
on a.customerid = b.customerid
where b.frequentflieron != 'SW' and a.airlineid = 'SW'
order by customerid;
""" """
### 8. Write a query to generate a list of customers where the interval between first and last flight is 5 days. ### 8. Write a query to generate a list of customers where the interval between first and last flight is 5 days.
...@@ -141,17 +64,7 @@ order by customerid; ...@@ -141,17 +64,7 @@ order by customerid;
### Output columns: name ### Output columns: name
### Order by: name ### Order by: name
queries[8] = """ queries[8] = """
with first_last (customerid, interval) as (select customerid, (extract(day FROM max(flightdate)) - extract(day FROM min(flightdate))) as interval select 0;
from flewon
group by customerid
having extract(month FROM max(flightdate)) = extract(month FROM min(flightdate))
and extract(year FROM max(flightdate)) = extract(year FROM min(flightdate)))
select b.name
from first_last a left join customers b
on a.customerid = b.customerid
where interval = 5
order by b.name;
""" """
...@@ -160,22 +73,7 @@ order by b.name; ...@@ -160,22 +73,7 @@ order by b.name;
### Output columns: customerid, airlinename ### Output columns: customerid, airlinename
### Order by: customerid, airlinename ### Order by: customerid, airlinename
queries[9] = """ queries[9] = """
with never_flown as ((select customerid, a.airlineid select 0;
from customers c, airlines a
order by c.customerid)
except
(select a.customerid, b.airlineid
from flewon a left join flights b
on a.flightid = b.flightid
group by a.customerid, b.airlineid
order by a.customerid))
select a.customerid, b.name as airlinename
from never_flown a left join airlines b
on a.airlineid = b.airlineid
order by a.customerid, b.name;
""" """
...@@ -185,13 +83,7 @@ order by a.customerid, b.name; ...@@ -185,13 +83,7 @@ order by a.customerid, b.name;
### Output columns: name,coupon ### Output columns: name,coupon
### Order by: name,coupon ### Order by: name,coupon
queries[10] = """ queries[10] = """
select name, concat(substring(name, 1, 3),extract(day FROM birthdate)) as coupon select 0;
from customers a left join flewon b
on a.customerid = b.customerid
where
(extract(month FROM birthdate) = extract(month FROM flightdate)) and
(extract(day FROM birthdate) = extract(day FROM flightdate))
order by name, coupon;
""" """
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment