Comprehension

Assume that you are the Manager of the Loans department of a Finance House. To keep track of the loans you have created two tables: CUSTOMERS and LOANS. The sample data in these tables is given below:

Table: CUSTOMERS

C_IDC_NamePhone
00001Raj Malhotra1234567890
00003David Xavier3456789012
00004Damini Iyer3156789012
00008Abdul2345678901

Table: LOANS

SNoC_IDL_AmtL_DateTermsRoI
1000032000002025-12-06607.80
20000825000002023-08-09609.00
3000015000002025-08-13486.00
4000033000002026-12-07368.00
5000046000002026-12-07606.00

Note: The tables may contain more records than shown here.

The management of the Finance House needs certain reports from you. Write the queries to extract the following data to create the reports:

Question: 1

Number of records from LOANS table where Rate of Interest (RoI) is above 7.0.

Show Hint

Use COUNT(*) on the LOANS table with the condition WHERE RoI > 7.0.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Start from the result we want:
The report must show how many loans carry a rate above 7.0. A single number is the result.
An aggregate function is the tool that turns many rows into one number.

Step 2: Pick the function:
COUNT(*) counts every row that reaches it.
If we write COUNT(*) alone, it counts all five loans. That is too many.
So the rows must be filtered before they are counted.

Step 3: Add the filter:
A WHERE clause runs before the aggregate function.
It drops every row whose RoI is 7.0 or less.
The comparison is RoI > 7.0. A value of exactly 7.0 would not count.

Step 4: Put it together and test it:
SELECT COUNT(*) FROM LOANS WHERE RoI > 7.0;

Rows left after WHERE: (1, 7.80), (2, 9.00) and (4, 8.00).
The rows with RoI 6.00 (SNo 3 and 5) are dropped.
COUNT(*) over the 3 remaining rows gives 3.

Final Answer:
The query is SELECT COUNT(*) FROM LOANS WHERE RoI > 7.0; and it returns 3 for the data shown.
Was this answer helpful?
0
Question: 2

Names of the customers whose loan amount (L_Amt) is above 1000000.

Show Hint

Join CUSTOMERS and LOANS on C_ID, then keep rows with L_Amt > 1000000 and show C_Name.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Filter the loans first:
Look at the L_Amt column of LOANS. Only one value is above 1000000.
That value is 2500000 in SNo 2, and the loan belongs to C_ID 00008.

Step 2: Turn the idea into SQL:
The LOANS table holds only the C_ID, not the name.
To get the name, we must match this C_ID with the CUSTOMERS table.
The match is done with a join on the common column C_ID.

Step 3: Write the query with the JOIN keyword:
SELECT C_Name
FROM CUSTOMERS
INNER JOIN LOANS
ON CUSTOMERS.C_ID = LOANS.C_ID
WHERE L_Amt > 1000000;

The ON part links the two tables. The WHERE part keeps the large loans only.
The comma form (FROM CUSTOMERS, LOANS WHERE CUSTOMERS.C_ID = LOANS.C_ID AND ...) is also accepted and gives the same result.

Step 4: Check the answer:
The joined row for C_ID 00008 shows C_Name as Abdul.
No other customer has a loan above 1000000, so the output has one name.

Final Answer:
The query returns the single name Abdul on the data shown.
Was this answer helpful?
0
Question: 3

C_ID, C_Name and Terms of all those records where Loan Date (L_Date) is after 31st December, 2024.

Show Hint

Join the two tables on C_ID and use the condition L_Date > '2024-12-31'.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Decide which loans qualify:
A loan qualifies when its date comes after 31 December 2024.
The 2023 loan (SNo 2) fails. The other four loans are dated 2025 or 2026, so they pass.

Step 2: Bring in the customer data:
The name is not in LOANS, so each qualifying loan must be linked to its customer.
The link is the shared column C_ID. We use INNER JOIN with ON.

Step 3: Mind the ambiguous column:
C_ID exists in both tables. If we write only C_ID in SELECT, MySQL raises an ambiguity error.
So we write CUSTOMERS.C_ID.

Step 4: Final query:
SELECT CUSTOMERS.C_ID, C_Name, Terms
FROM CUSTOMERS
INNER JOIN LOANS
ON CUSTOMERS.C_ID = LOANS.C_ID
WHERE L_Date > '2024-12-31';


Step 5: Output table for the sample data:
00003 | David Xavier | 60
00001 | Raj Malhotra | 48
00003 | David Xavier | 36
00004 | Damini Iyer | 60
David Xavier appears twice because he has two loans after the date.

Final Answer:
Join on C_ID and filter with WHERE L_Date > '2024-12-31'.
Was this answer helpful?
0
Question: 4

Details of all the loans in the descending order of RoI.

Show Hint

Use SELECT * FROM LOANS and add ORDER BY RoI DESC.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Split the question into two jobs:
Job one is to show all details of all loans. Job two is to arrange them from the largest RoI to the smallest.

Step 2: Solve job one:
The wildcard * picks every column, and with no WHERE clause every row is picked.
This gives SELECT * FROM LOANS.

Step 3: Solve job two:
Sorting is always the last clause of a SELECT statement.
ORDER BY RoI sorts by the rate. Without a keyword the order is ascending (ASC).
The question wants descending order, so we write DESC after the column name.

Step 4: Join the two jobs and test:
SELECT * FROM LOANS ORDER BY RoI DESC;

Output order on the sample data:
2 | 00008 | 2500000 | 2023-08-09 | 60 | 9.00
4 | 00003 | 300000 | 2026-12-07 | 36 | 8.00
1 | 00003 | 200000 | 2025-12-06 | 60 | 7.80
3 | 00001 | 500000 | 2025-08-13 | 48 | 6.00
5 | 00004 | 600000 | 2026-12-07 | 60 | 6.00
The last two rows have the same RoI, so MySQL may list them in either order.

Final Answer:
SELECT * FROM LOANS ORDER BY RoI DESC; is the required query.
Was this answer helpful?
0
Question: 5

C_ID and average term for each C_ID from the LOANS table.

Show Hint

Use the aggregate function AVG(Terms) together with GROUP BY C_ID on the LOANS table.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Think of the answer as a small table:
The output has two columns, the customer id and the average term. There is one row for every customer who has loans.

Step 2: Find the grouping column:
The phrase 'for each C_ID' means that rows with the same C_ID are treated as one set.
That is exactly what GROUP BY C_ID does.

Step 3: Add the aggregate function:
Inside each set, AVG(Terms) adds the Terms values and divides by how many there are.
The plain column C_ID may be shown with it, because C_ID is the grouping column.
A column that is not grouped could not be shown next to AVG() in this way.

Step 4: Final query and check:
SELECT C_ID, AVG(Terms)
FROM LOANS
GROUP BY C_ID;

Output on the sample data:
00001 | 48
00003 | 48
00004 | 60
00008 | 60
The only customer with two loans is 00003. His terms are 60 and 36, which average to 48.

Final Answer:
Use AVG(Terms) with GROUP BY C_ID on the LOANS table.
Was this answer helpful?
0