ManikandanKannan ManikandanKannan - 6 months ago 11
SQL Question

Database left outer join with a base table

I have 3 tables A, B, C with the data as follows

Table A

C1 C2
----------
100 X
200 Y
300 z


Table B

C1 C2
-----------
100 bb
300 bb1


Table C

C1 C2
-----------
200 cc
300 cc1


I want a query which can out put as follows; The count of records in the result should be same as the count of records in A.

100 X 100 bb null null
200 y null null 200 cc
300 z 300 bb1 300 cc1


Note: All the tables have one-one relationship and 3 tables are just given for example. This can be any number of tables.

Answer

Try this:

SELECT *
FROM tableA a
LEFT JOIN tableB b ON a.c1 = b.c1
LEFT JOIN tableC c ON a.c1 = c.c1