JOINS and SET Operations — Question 27
Back to all questionsWith reference to the below given tables, write commands in SQL for (i) to (iii) :
Table : TRANSPORTER
| ORDERID | ITEM | TRANSPORTDATE | DESTINATION | DRIVERID |
|---|---|---|---|---|
| 1120 | TELEVISION | 2019-03-19 | MUMBAI | D103 |
| 1121 | REFRIGERATOR | 2019-04-12 | UDAIPUR | D101 |
| 1122 | TELEVISION | 2019-05-06 | MUMBAI | D101 |
| 1123 | MICROWAVE | 2019-05-07 | KANPUR | D103 |
| 1124 | FURNITURE | 2019-06-15 | KANPUR | D102 |
Table : DRIVER
| DRIVERID | DRIVERNAME | DRIVERGRADE | PHONE |
|---|---|---|---|
| D101 | Radhey Shyam | A | 981234567 |
| D102 | Jagat Singh | 981017897 | |
| D103 | Timsy Yadav | B | |
| D104 | Zoravar Singh | A | 981107887 |
(i) To display OrderId, Item being transported, DriverId and names of Drivers for all the orders that are being transported by drivers with 'A' grade.
(ii) To display DriverId, Names of drivers and Travel dates for drivers who are travelling after 1st March, 2019.
Or
How many rows will be there in Cartesian product of the two tables in consideration here ?
(iii) To display Orderld, Driverld and names of Drivers for all the orders that are transporting TELEVISION.
(i)
SELECT T.ORDERID, T.ITEM, D.DRIVERID, D.DRIVERNAME
FROM TRANSPORTER T, DRIVER D
WHERE T.DRIVERID = D.DRIVERID AND D.DRIVERGRADE = 'A';+---------+--------------+----------+--------------+
| ORDERID | ITEM | DRIVERID | DRIVERNAME |
+---------+--------------+----------+--------------+
| 1121 | REFRIGERATOR | D101 | RADHEY SHYAM |
| 1122 | TELEVISION | D101 | RADHEY SHYAM |
+---------+--------------+----------+--------------+
(ii)
SELECT D.DRIVERID, D.DRIVERNAME, T.TRANSPORTDATE
FROM TRANSPORTER T, DRIVER D
WHERE T.DRIVERID = D.DRIVERID AND T.TRANSPORTDATE > '2019-03-01';+----------+--------------+---------------+
| DRIVERID | DRIVERNAME | TRANSPORTDATE |
+----------+--------------+---------------+
| D103 | TIMSY YADAV | 2019-03-19 |
| D101 | RADHEY SHYAM | 2019-04-12 |
| D101 | RADHEY SHYAM | 2019-05-06 |
| D103 | TIMSY YADAV | 2019-05-07 |
| D102 | JAGAT SINGH | 2019-06-15 |
+----------+--------------+---------------+
Or
The number of rows in the Cartesian product of the two tables "TRANSPORTER" and "DRIVER" would be the product of the number of rows in each table. In this case, since the "TRANSPORTER" table has 5 rows and the "DRIVER" table has 4 rows, the Cartesian product would have 5 * 4 = 20 rows.
(iii)
SELECT T.ORDERED, D.DRIVERID, D.DRIVERNAME
FROM TRANSPORTER T, DRIVER D
WHERE T.DRIVERID = D.DRIVERID AND T.ITEM = 'TELEVISION';+---------+----------+--------------+
| ORDERED | DRIVERID | DRIVERNAME |
+---------+----------+--------------+
| 1120 | D103 | TIMSY YADAV |
| 1122 | D101 | RADHEY SHYAM |
+---------+----------+--------------+