113 lines
3.4 KiB
Markdown

## Lab 4 - OracleDB
### Verifying the existence of the previous labs' DB.
```sql
SQL> select table_name from user_tables;
TABLE_NAME
--------------------------------------------------------------------------------
ACCIDENT
CAR
OWNS
PARTICIPATED
PERSON
```
### Q1. Find the total number of people who owned cars that were involved in accidents in 2008.
Ans.
Modifying the table to add required entry from 2008.
```sql
SQL> select * from accident;
REPORT_NUMBER ACCD_DATE LOCATION
------------- --------- --------------------------------------------------
1 01-JAN-24 Delhi India
3 29-FEB-24 India
5 31-MAR-00 Gujrat India
12 17-JUL-00 karnataka India
10 01-JAN-08 Manipal India
SQL> update accident set ACCD_DATE='01-Jan-2008' where REPORT_NUMBER=1
2 ;
1 row updated.
SQL> select * from accident;
REPORT_NUMBER ACCD_DATE LOCATION
------------- --------- --------------------------------------------------
1 01-JAN-08 Delhi India
3 29-FEB-24 India
5 31-MAR-00 Gujrat India
12 17-JUL-00 karnataka India
10 01-JAN-08 Manipal India
```
```sql
SQL> select count(distinct Driver_ID#) from participated natural join accident where extract(year from accd_date)=2008;
COUNT(DISTINCTDRIVER_ID#)
-------------------------
1
```
### Q2. Find the number of accidents in which cars belonging to a specific model were involved.
Ans.
```sql
SQL> select * from car;
REGNO MODEL YEAR
-------------------- ------------------------------ ----------
ABCD0001 Celtos 3
EFGH2001 Ferrari 32
DSDS0001 Urus 5
ABCD4001 Honda City 1
HFSP5601 mini cooper 7
SQL> select count(distinct driver_id#) from car natural join participated where model like 'Urus';
COUNT(DISTINCTDRIVER_ID#)
-------------------------
1
```
### Q3. Produce a listing with header as OWNER_NAME, No. of Accidents, and Total Damage Amount in a descending order on total damage.
Ans.
```sql
SQL> select name as OWNER_NAME,count(distinct DRIVER_ID#),sum(Damage_amount) as total_Damage from person natural join participated group by(name) order by(TOTAL_DAMAGE) DESC;
OWNER_NAME COUNT(DISTINCTDRIVER_ID#) TOTAL_DAMAGE
---------- ------------------------- ------------
Amogh 1 26500
Rohan 1 10000
Ramesh 1 150
```
### Q4. List the Owners who made more than 2 accidents in a year.
Ans.
```sql
SQL> insert into participated values(1236,'DSDS0001',99,100000000);
1 row created.
SQL> select name,extract(year from accd_date) as year,count(report_number) as total_accidents from person natural join participated join accident using (report_number) group by(name,extract(year from accd_date)) having count(report_number)>=2;
NAME YEAR TOTAL_ACCIDENTS
---------- ---------- ---------------
Ramesh 2024 2
```
### Q5. List the owners who are not involved in any accident.
Ans.
```sql
SQL> select name from person where(driver_id#) not in(select driver_id# from participated);
NAME
----------
Rohit
Saarthak
```