How to Select records from table inside another table and making joing

I’d really appreciate some help with an SQL query across tables.
I have:

  • Table A1 inside Table A
  • B1 inside Table B.
    I want to select columns from table A1 that have a corresponding tag in table

A1 table

Id       type message
1        2    'hello'
102      0    'bye'
302      2    'hey'

B1 Table

Id      data     refId   
1       70       102
2       6        2
3       8        302

Expected result:

Id       type data
1        2    
102      0    70
302      2    8

What i did:

SELECT Id, type, B.B1.data 
FROM A.A1 a
INNER JOIN B.B1 b ON a.id = b.refid
;

>Solution :

You have to use left join instead of inner join, to returns all rows from the left table A1, even if there are no matches in the right table B1.

SELECT a.Id, a.type, b.data 
FROM A.A1 a
LEFT JOIN B.B1 b ON a.id = b.refid

NB : Use aliases for better readability

Demo here

Leave a Reply