I have three tables
sem1
--------------------------
roll | sub1 | sub2 | gpa |
--------------------------
001 | 98 | 99 | 8.5 |
002 | 99 |100 | 9.9 |
--------------------------
sem2
--------------------------
roll | sub3 | sub4 | gpa |
--------------------------
001 | 88 | 87 | 8.1 |
002 | 89 | 90 | 9.0 |
--------------------------
sem3
--------------------------
roll | sub5 | sub6 | gpa |
--------------------------
001 | 85 | 75 | 8.5 |
002 | 90 | 80 | 8.9 |
--------------------------
I want to fetch only the roll and gpa columns in all three tables of a particular roll number(say 001) like this.
-------------------------
|roll | gpa | gpa |gpa |
-------------------------
| 001 | 8.5 | 8.1 | 8.5 |
-------------------------
I wrote the following query
select roll,gpa,gpa,gpa
from sem1 natural
join sem2 natural
join sem3
where roll=001;
But it doesn't seems to work. Any suggestions?
With a natural join you have to have a unique column in every table - you don't , gpa is the same so the natural join is on roll and gpa an because gpa differs you get nothing.
if you tables looked like this
Then your query (as amended) would produce a result -
For clarity it is best practice to qualify your column names for example
But don't use a natural joins use a left joins for example
but even this is not sufficiently robust in my view relying as it does on sem1 driving and always containing all the distinct roll numbers contained in sem2 and sem3. A more ribust approach would be to get all the distinct roll numbers from all 3 tables and drive from that.