Computer Science

A departmental store "ABC" is considering maintaining their inventory using SQL to store data and maintain basic transactions. As a database manager, Mehak has to create two tables as Customer & Transaction:

Table: CUSTOMER

CNoCNAMEADDRESS
101Richa JainDelhi
102Surbhi SinhaChennai
103Lisa ThomasBengaluru
104Imran AliDelhi
105Roshan SinghChennai

Table: TRANSACTION

DeptCNOAMOUNTTYPEDOT
T0011011500Credit2017-11-23
T0021032000Debit2017-05-12
T0031023000Credit2017-06-10
100410312000Credit2017-09-12
T0051011000Debit2017-09-05

(i) Identify the attribute best suited to be declared as a Primary key in customer table.

(ii) Help Mehak to display details of all transactions of TYPE Credit from Table TRANSACTION.

(iii) Mehak wants to display all CNO, CNAME and DOT (date of transaction) of those CUSTOMERS from tables CUSTOMER and TRANSACTION who have done transactions more than or equal to 2000 for the Balance Sheet.

(iv) Mehak wants to display the last date of transaction (DOT) from the table TRANSACTION for the customer having CNO as 103. Which command will she use?

SQL Joins & Grouping

3 Likes

Answer

(i) The attribute best suited to be declared as a Primary Key in the CUSTOMER table is CNo (Customer Number). It uniquely identifies each customer in the table.

(ii)

SELECT * FROM TRANSACTION
WHERE TYPE = "Credit";
Output
+------+-----+--------+--------+------------+
| DEPT | CNO | AMOUNT | TYPE   | DOT        |
+------+-----+--------+--------+------------+
| T001 | 101 |   1500 | Credit | 2017-11-23 |
| T003 | 102 |   3000 | Credit | 2017-06-10 |
| T004 | 103 |  12000 | Credit | 2017-09-12 |
+------+-----+--------+--------+------------+

(iii)

SELECT C.CNO, C.CNAME, T.DOT
FROM CUSTOMER C, TRANSACTION T
WHERE C.CNO = T.CNO AND T.AMOUNT >= 2000; 
Output
+-----+--------------+------------+
| CNO | CNAME        | DOT        |
+-----+--------------+------------+
| 103 | LISA THOMAS  | 2017-05-12 |
| 102 | SURBHI SINHA | 2017-06-10 |
| 103 | LISA THOMAS  | 2017-09-12 |
+-----+--------------+------------+

(iv) Mehak will use "MAX()" function to display the last date of transaction (DOT) from the table TRANSACTION for the customer having CNO as 103.

SELECT MAX(DOT) AS Last_Transaction_Date
FROM TRANSACTION
WHERE CNO = 103;
Output
+-----------------------+
| Last_Transaction_Date |
+-----------------------+
| 2017-09-12            |
+-----------------------+

Answered By

3 Likes


Related Questions