In SQL, running on an Oracle database, write a query for each problem that solve
ID: 3587770 • Letter: I
Question
In SQL, running on an Oracle database, write a query for each problem that solves the task. Please ensure that it
1) Find for each person how many cars manufactured by Honda or Toyota they own. If a person does not own any such cars, she should still appear in the result with the number of cars reported as 0. Write your query using outer join. Be very careful not to count also the BMW cars a person owns.
2) Write the same query as above, but using a scalar query for counting the Hondas and Toyotas a person owns, without outer join.
Note: The queries listed above should yield the following result shown below. If possible, try to show the output for your queries. Also,
NAME Jenson Button1 Rubens Barrichello 1 Sebastian Vettel 0 Mark Webber Lewis Hamilton 2 Felipe Massa 0 NUM_OF_CARS CDExplanation / Answer
1.
select MAX(p.Name), count(c.License) as NUM_OF_CARS
from
Person p, Car c, Owns o
where
p.Driver_id = o.Driver_id(+)
and o.License = c.License(+)
and ((c.Model(+) = 'Honda') OR (c.Model(+) = 'Toyota'))
group by p.Driver_id;
2.
select p.Name,
NVL((
select count(c.License) from Car c, Owns o where
c.License = o.License and o.Driver_id = p.Driver_id
and ((c.Model = 'Honda') or (c.Model = 'Toyota'))
group by o.Driver_id
), 0) as NUM_OF_CARS
from
Person p;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.