skip to Main Content

Hi I am working in postgres. I have below table contract_para

contract_para

Id

1
2
3
4
5


contract_lines

Id   |  para_id
1    |  1
2    |  2
3    |  3


Output

contract_para_id    | contract_lines_id     |   contract_lines_para_id
1                   | 1                     |   1
2                   | 2                     |   2
3                   | 3                     |   3
4                   | null                  |   null
5                   | null                  |   null

I would like to join two tables and return matching data and also return data appears in first table and not in second. Can someone please help me with the query. Thanks

2

Answers


  1. This will retrieves rows from contract_para (from contract_para) even if there are no matching rows in contract_lines (left join contract_lines) :

    select *
    from contract_para cp
    left join contract_lines cl on cp.id = cl.para_id
    
    Login or Signup to reply.
  2. Left outer join will work here :

    Refer :

    select * from contract_para c_para left join contract_lines c_lines on c_para.id = c_lines.para_id
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search