Informatics Practices
With 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.
SQL Joins & Grouping
3 Likes
Answer
(i)
SELECT T.ORDERID, T.ITEM, D.DRIVERID, D.DRIVERNAME
FROM TRANSPORTER T, DRIVER D
WHERE T.DRIVERID = D.DRIVERID AND D.DRIVERGRADE = 'A';
Output
+---------+--------------+----------+--------------+
| 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';
Output
+----------+--------------+---------------+
| 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';
Output
+---------+----------+--------------+
| ORDERED | DRIVERID | DRIVERNAME |
+---------+----------+--------------+
| 1120 | D103 | TIMSY YADAV |
| 1122 | D101 | RADHEY SHYAM |
+---------+----------+--------------+
Answered By
3 Likes
Related Questions
Write a SQL query to display all the divnos in Member table but not in the Division table.
Table : Member
EmpId Name Pay Divno 1001 Shankhya 34000 10 1003 Ridhima 32000 50 1002 Sunish 45000 20 Table : Division
Divno Divname Location 10 Media TF02 20 Dance FF02 30 Production SF01 Write a SQL query to display all the divnos in Division table but not in the Member table.
Table : Member
EmpId Name Pay Divno 1001 Shankhya 34000 10 1003 Ridhima 32000 50 1002 Sunish 45000 20 Table : Division
Divno Divname Location 10 Media TF02 20 Dance FF02 30 Production SF01 Write a SQL query to display common divnos from both the tables.
Table : Member
EmpId Name Pay Divno 1001 Shankhya 34000 10 1003 Ridhima 32000 50 1002 Sunish 45000 20 Table : Division
Divno Divname Location 10 Media TF02 20 Dance FF02 30 Production SF01 Consider the following tables TRANSPORTER and DRIVER and answer the questions that follow :
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) Can the column 'PHONE' be set as the primary key of the table DRIVER ? Give reason(s).
Or
Identify the Primary key in the table 'DRIVER'. Give reason(s) for your choice.
(ii) In the PHONE column of "DRIVER" table, each phone number is taking same number of bytes (being padded with spaces to specified length) irrespective of the number of characters actually stored in that column. Which data type has been used for PHONE column ?
Or
Identify data type and size to be used for column DRIVERID.