Question:medium

Consider the following tables:

Table 1: TEACHER, which stores Teacher ID (TID), Teacher Name (TName), Experience(Experience) and City (City) that they live in.

TIDTNameExperienceCity
1Kartik5Bhopal
2Shahnaz6Nagpur
3Rajendra7Delhi
4Tanvi4Bhopal
5Alam9Delhi

Table 2: SUBJECT, which stores Subject ID (SID), Subject Name (SubName) and ID of the teacher (TID) teaching that Subject.

SIDSubNameTID
101Physics1
102Chemistry2
103Mathematics3
104Informatics Practices4
105Computer Science5

Write appropriate SQL query for the following:

(I) Delete the record of the subject whose TID is equal to 4.

(II) Display the names of teachers who have more than 5 years of experience and who stay in Nagpur.

(III) Display subject names along with their teacher names.

Show Hint

Use DELETE with WHERE for part I, AND for the two conditions in part II, and match TID in both tables for part III.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Decide the SQL command for each part:
Part (I) removes a row, so the command is DELETE.
Part (II) filters rows of one table, so it is SELECT with WHERE.
Part (III) needs columns from two tables, so it is a join.

Step 2: Write part (I):
The subject record is stored in SUBJECT. The row to remove is the one with TID equal to 4.
DELETE FROM SUBJECT WHERE TID = 4;

In the data given, this removes the row 104, Informatics Practices, which is taught by Tanvi (TID 4).
If WHERE is left out, every row of the table is deleted, so the condition matters.

Step 3: Write part (II):
We want the name column only, so TName comes after SELECT.
The two conditions are Experience above 5 and City equal to Nagpur. AND makes both compulsory.
SELECT TName FROM TEACHER WHERE Experience > 5 AND City = 'Nagpur';

Only Nagpur row in TEACHER is Shahnaz, and her experience is 6, which is more than 5. So the output is Shahnaz.

Step 4: Write part (III):
SubName is in SUBJECT and TName is in TEACHER. We connect the tables through the equal values of TID.
SELECT SubName, TName FROM SUBJECT, TEACHER WHERE SUBJECT.TID = TEACHER.TID;

Matching TID values gives these pairs: 1 with Physics and Kartik, 2 with Chemistry and Shahnaz, 3 with Mathematics and Rajendra, 4 with Informatics Practices and Tanvi, 5 with Computer Science and Alam.
Writing the table name before the dot (TEACHER.TID) avoids confusion because both tables have a column named TID.

Final Answer:
(I) DELETE FROM SUBJECT WHERE TID = 4;
(II) SELECT TName FROM TEACHER WHERE Experience > 5 AND City = 'Nagpur';
(III) SELECT SubName, TName FROM SUBJECT, TEACHER WHERE SUBJECT.TID = TEACHER.TID;
Was this answer helpful?
0