Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading