2

I have two tables

Table_1
id|subject_code|subject_id
 1|Test1       |1
 2|Test2       |2
 3|Test3       |3

Table2
id|subject_id|grade|status
 1|1         |5.00 |Fail
 2|3         |2.25 |Pass

Now, I want to create a query that will return the following,

Table3
subject_code|grade|status
Test1       |5.00 |Fail
Test2       |NULL |NULL
Test3       |2.25 |Pass

I have read about combining left join and union all but I am lost on how to do that. Thanks in advance.

Ruben_PH
  • 1,692
  • 7
  • 25
  • 42

3 Answers3

4

Try a left join, like this::

Select 
DISTINCT(table1.subject_code), 
grade, 
status

from table1 
LEFT join table2 on (table1.subject_id=table2.subject_id)
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
4
SELECT subject_code, grade, status 
FROM Table_1 t1 
LEFT JOIN Table2 t2 ON t1.subject_id = t2.subject_id;
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Stanley
  • 5,057
  • 4
  • 34
  • 44
1

I suppose a simple LEFT JOIN would do the trick :

SELECT Table_1.subject_code, Table2.grade, Table2.status FROM Table_1 LEFT JOIN Table2 ON Table2.subject_id = Table_1.subject_id;

Eric MORAND
  • 6,373
  • 4
  • 27
  • 34