Skip to content
Advertisement

How I can retrieve all data from the first table and some of the second table

I have two tables and the relation between them are NO. .

table1
---------------
NO.        courses 
---------------
1         CHEM 101
2         ENGL 101
3         MATH 101
4         PE 101
5         PHYS 101
6         IAS 101

table2
----------------------------
NO.         ID        Grades 
----------------------------
1           5050       A+
2           5050       B
1           4040       A
2           4040       C

How I can write SQl query to give me output form table1 and table2 only for who id is 5050 and the output should be like this

output
--------------------------
courses         Grades
-------------------------- 
CHEM 101        A+
ENGL 101        B
MATH 101        null
PE 101          null
PHYS 101        null
IAS 101         null

Advertisement

Answer

This is an example of a LEFT JOIN — keeping all the rows in the first table but only matching rows in the second.

In your case, this looks like:

select t1.*, t2.grade
from table1 t1 left join
     table2 t2
     on t1.no = t2.no and t2.id = 5050;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement