-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex5_2.sql
More file actions
48 lines (43 loc) · 2.2 KB
/
Copy pathex5_2.sql
File metadata and controls
48 lines (43 loc) · 2.2 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
--1
SELECT `users`.`id`, `name`, `train`, `seat_number`
FROM `users` INNER JOIN `tickets` ON `users`.`id`=`tickets`.`user`
WHERE `tickets`.`train`=11 ORDER BY `seat_number` ASC;
--2
SELECT `users`.`id`, `users`.`name`, Count(*) as `trains_count`,
Round(Sum(`distance`)*0.1, 4) AS `total_distance`
FROM `tickets` INNER JOIN `users` ON `tickets`.`user`=`users`.`id`
INNER JOIN `trains` ON `tickets`.`train`=`trains`.`id`
GROUP BY `user` ORDER BY `total_distance` DESC LIMIT 0, 6;
--3
SELECT `trains`.`id`, `types`.`name` AS `type`, `source`.`name` AS `src_stn`,
`dest`.`name` AS `dst_stn`, Timediff(`arrival`, `departure`) AS `travel_time`
FROM `trains` INNER JOIN `types` ON `trains`.`type`=`types`.`id`
INNER JOIN `stations` AS `source` ON `trains`.`source`=`source`.`id`
INNER JOIN `stations` AS `dest` ON `trains`.`destination`=`dest`.`id`
ORDER BY `travel_time` DESC LIMIT 0, 6;
--4
SELECT `types`.`name` AS `type`, `source`.`name` AS `src_stn`,
`dest`.`name` AS `dst_stn`, `departure`, `arrival`,
Round(`fare_rate`*`distance`*0.001, -2) AS `fare`
FROM `trains` INNER JOIN `types` ON `trains`.`type`=`types`.`id`
INNER JOIN `stations` AS `source` ON `trains`.`source`=`source`.`id`
INNER JOIN `stations` AS `dest` ON `trains`.`destination`=`dest`.`id`
ORDER BY `departure` ASC;
--5
SELECT `trains`.`id`, `types`.`name` AS `type`,
`source`.`name` AS `src_stn`, `dest`.`name` AS `dst_stn`,
Count(*) AS `occupied`, `max_seats` AS `maximum`
FROM `tickets` INNER JOIN `trains` ON `tickets`.`train`=`trains`.`id`
INNER JOIN `types` ON `trains`.`type`=`types`.`id`
INNER JOIN `stations` AS `source` ON `trains`.`source`=`source`.`id`
INNER JOIN `stations` AS `dest` ON `trains`.`destination`=`dest`.`id`
GROUP BY `trains`.`id` ORDER BY `trains`.`id` ASC;
--6
SELECT `trains`.`id`, `types`.`name` AS `type`,
`source`.`name` AS `src_stn`, `dest`.`name` AS `dst_stn`,
Count(`tickets`.`id`) AS `occupied`, `max_seats` AS `maximum`
FROM `tickets` RIGHT OUTER JOIN `trains` ON `tickets`.`train`=`trains`.`id`
INNER JOIN `types` ON `trains`.`type`=`types`.`id`
INNER JOIN `stations` AS `source` ON `trains`.`source`=`source`.`id`
INNER JOIN `stations` AS `dest` ON `trains`.`destination`=`dest`.`id`
GROUP BY `trains`.`id` ORDER BY `trains`.`id` ASC LIMIT 0, 20;