Skip to content
Question 77 of 95

Q.Write the output of SQL queries

(a) and
(b) based on the following two tables DOCTOR and PATIENT belonging to the same database: Table: DOCTOR DNO DNAME FEES D1 AMITABH 1500 D2 ANIKET 1000 D3 NIKHIL 1500 D4 ANJANA 1500 Table: PATIENT PNO PNAME ADMDATE DNO P1 NOOR 2021-12-25 D1 P2 ANNIE 2021-11-20 D2 P3 PRAKASH 2020-12-10 NULL P4 HARMEET 2019-12-20 D1
(a) SELECT DNAME, PNAME FROM DOCTOR NATURAL JOIN PATIENT;
(b) SELECT PNAME, ADMDATE, FEES FROM PATIENT P, DOCTOR D WHERE D.DNO = P.DNO AND FEES > 1000;
Puducherry TnboardCBSE Class XII Board 2022Subjective· 2mImportance★★★★★
81% · 77/95 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Natural join matches only rows with common column values, while a Cartesian product with a condition filters rows based on the given criteria.

Let us begin with the first query. A natural join is a special kind of join that automatically matches rows from two tables based on columns that have the same name. In our case, both DOCTOR and PATIENT have a column called DNO. The natural join will pair each doctor with every patient who has that doctor's DNO. Crucially, it also eliminates duplicate columns — so DNO appears only once in the result, not twice.

Now look at the data. Doctor D1 (AMITABH) appears in two patient rows: P1 (NOOR) and P4 (HARMEET). Doctor D2 (ANIKET) appears in one patient row: P2 (ANNIE). Doctor D3 (NIKHIL) and D4 (ANJANA) have no matching patients — their DNO values do not appear in the PATIENT table. Also, patient P3 (PRAKASH) has a NULL in DNO, so it cannot match any doctor. Natural join, like all joins, ignores NULL values because NULL is not equal to anything, not even another NULL.

So the natural join will produce exactly three rows:

DNAMEPNAME
AMITABHNOOR
AMITABHHARMEET
ANIKETANNIE
Note

The order of rows in the output is not guaranteed by SQL unless you use an ORDER BY clause. But in practice, most database systems return rows in the order they are processed — often the order of the left table first.

Now for the second query. This is a Cartesian product (also called cross join) followed by a condition in the WHERE clause. The query writes FROM PATIENT P, DOCTOR D — that is an old-style join that first pairs every patient with every doctor. Since there are 4 patients and 4 doctors, the intermediate result has 16 rows. Then the condition D.DNO = P.DNO AND FEES > 1000 filters these down.

The condition has two parts, both must be true:

  • The doctor's DNO must equal the patient's DNO.
  • The doctor's FEES must be greater than 1000.

Look at the FEES column: D1 has 1500, D2 has 1000, D3 has 1500, D4 has 1500. So only doctors with FEES > 1000 are D1, D3, and D4. But D3 and D4 have no matching patients (no patient has DNO = D3 or D4). So only D1 qualifies on both counts. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.