-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab5_subquery.sql
34 lines (31 loc) · 1.29 KB
/
lab5_subquery.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- Choose route codes with average time of moving between stations is more than average
SELECT route_code, delta_time
FROM routes r, link_routes_stations l
WHERE r.route_id=l.route_id
AND delta_time>(SELECT AVG(delta_time)
FROM routes r1, link_routes_stations l1
WHERE r1.route_id=l1.route_id);
-- Choose route codes with maximum time of moving between stations
SELECT route_code, delta_time
FROM routes r, link_routes_stations l
WHERE r.route_id=l.route_id
GROUP BY route_code, delta_time
HAVING MAX(delta_time)=(SELECT MAX(delta_time)
FROM link_routes_stations);
-- Choose route codes and name of stations on them with maximum time of moving between stations
SELECT route_code, station_name
FROM routes r INNER JOIN link_routes_stations l
ON r.route_id=l.route_id
INNER JOIN stations s
ON l.station_id=s.station_id
GROUP BY route_code, station_name
HAVING MAX(delta_time)=(SELECT MAX(delta_time)
FROM link_routes_stations);
-- Choose route code with the largest average time
SELECT route_code
FROM routes r, link_routes_stations l
WHERE r.route_id=l.route_id
AND delta_time>(SELECT AVG(delta_time)
FROM routes r1, link_routes_stations l1
WHERE r1.route_id=l1.route_id)
ORDER BY delta_time DESC LIMIT 1;