database - query from multiple tables? -
hi started self-studying oracle sql, , stuck in following problem. glad if can guided on how approach solution these type of problem.
we have database. database consists of following tables:
table route_info contains descriptions of routes on monorails used travel.
route_info(route_no integer, source string, destination string, length integer, departs time, arrives time, cost integer); +----------+----------+-------------+--------+----------+----------+------+ | route_no | source | destination | length | departs | arrives | cost | +----------+----------+-------------+--------+----------+----------+------+ | 1462 | banglore | delhi | 1516 | 13:00:00 | 15:00:00 | 4500 | | 4456 | gwalior | delhi | 543 | 05:05:06 | 04:55:24 | 546 | | 4524 | banglore | delhi | 1516 | 04:44:00 | 13:00:45 | 1200 | | 7490 | banglore | gwalior | 1343 | 01:16:17 | 17:07:08 | 1400 | | 7890 | agra | gwalior | 343 | 01:15:41 | 07:07:08 | 400 | +----------+----------+-------------+--------+----------+----------+------+
table monorail contains information each monorail.
monorail(rid integer, rname string, runningrange integer); +------+---------------+--------------+ | rid | rname | runningrange | +------+---------------+--------------+ | 4454 | mark jung exp | 300 | | 1234 | speedester | 454 | | 2134 | monstor | 4054 | | 7334 | father | 1051 | | 4444 | supreme | 3023 | | 1444 | grain | 3023 | +------+---------------+--------------+
table employee contains information of employees in organization (including drivers of monorails).
employee(eid integer, ename string, salary integer); +------+--------+--------+ | eid | ename | salary | +------+--------+--------+ | 101 | nusrat | 100 | | 234 | emp1 | 5050 | | 323 | emp2 | 7050 | | 314 | emp3 | 10050 | | 541 | emp4 | 9010 | | 432 | emp5 | 12345 | | 123 | kabir | 400 | | 555 | raheem | 235 | +------+--------+--------+
table assigned contains data monorail driven employee. can many-to-many relation.
assigned(eid integer, rid integer); +------+------+ | eid | rid | +------+------+ | 101 | 4454 | | 234 | 1234 | | 323 | 1234 | | 314 | 1234 | | 314 | 2134 | | 541 | 2134 | | 123 | 7334 | | 101 | 4444 | | 101 | 1444 | +------+------+
we given following guidelines
- make necessary assumptions.
- string comparison case-insensitive.
- always return result in increasing order of output.
now need write oracle sql query this: monorails "runningrange" greater 500, find id of rail ("rid") , sum of salary of drivers assigned rail.
break down smaller pieces make easier understand:
start getting monorails "runningrange" greater 500:
select rid monorail runningrange > 500
then add inner join
assigned
table:
select m.rid monorail m inner join assigned on(m.rid = a.rid) runningrange > 500
then add inner join
employee
table:
select m.rid monorail m inner join assigned on(m.rid = a.rid) inner join employee e on(a.eid = e.eid) runningrange > 500
and add group by
, sum
.
select m.rid, sum(salary) monorail m inner join assigned on(m.rid = a.rid) inner join employee e on(a.eid = e.eid) runningrange > 500 group m.rid order sum(salary)
Comments
Post a Comment