110 likes | 183 Views
SQL Examples. Person. Car. Owns. Accident. Participated. List all drivers who live on Main Street. select driver_id , name from person where address like '%Main Street%';. List all drivers who own more than one car. select p.driver_id , p.name from person p, owns o
E N D
Person Car Owns Accident Participated
List all drivers who live on Main Street. select driver_id, name from person where address like '%Main Street%';
List all drivers who own more than one car. select p.driver_id, p.name from person p, owns o where p.driver_id = o.driver_id group by p.driver_id, p.name having count(*)>1;
List all drivers who own more than one car. select p.driver_id, p.name from person p where p.driver_id in (select o.driver_id from owns o group by o.driver_id having count(*)>1 );
List all drivers who were involved in more than one accident. select p.driver_id, p.name from person p where p.driver_id in (select pa.driver_id from participated pa group by pa.driver_id having count(car) > 1 );
Find the driver with the highest payout. The insurance payout for a driver is the total damage amount in all the accidents in which the driver participated. select pa.driver_id, sum(damage_amount) as totalamount from participated pa group by pa.driver_id;
Find the driver with the highest payout. select pa.driver_id, sum(damage_amount) as totalamount from participated pa group by pa.driver_id having sum(damage_amount) >= ALL (select sum(damage_amount) as total from participated p group by p.driver_id );
List all the cars that were involved in more than one accident. select car from participated group by car having count(car)>1;
List all the pairs of drivers and cars that were involved in more than one accident. select driver_id, car from participated group by driver_id, car having count(*)>1;
What was the date of the latest accident involving driver whose driver_id is 10? select p.driver_id, max(a.adate) from participated p, accident a where p.report= a.report_number and p.driver_id=10 group by driver_id;