0

I have two tables table1(id,name,type) and table2(id,source,destination)

When I run query

SELECT
    name,
    source,
    destination
FROM
    table1,
    table2
WHERE
    table1.id=table2.id

If there's no id matching between two tables, can I still get empty column for source and destination .

1 Answer 1

1

Yes, you basically want an OUTER JOIN and remember to always use the explicit ANSI JOIN syntax and not the implicit comma syntax for joins.Also use proper table aliases to avoid ambiguity.

SELECT
    t1.name,
    t2.source,
    t2.destination
FROM
    table1 t1 left outer join
    table2 t2 ON t1.id = t2.id
Sign up to request clarification or add additional context in comments.

2 Comments

Seems good let me try this and update, can I do the same for more than two tables, when T1 has to join with t2 and also t1 has to join with t3.. and what if for only t2 and t3 tables i want empty columns, where as for other tables it exits and t1 primary key is used as filter , i dont wantempty columns there. there it should be direct
@JohnHumanyun : Sure. Read more about SQL Joins and outer joins in particular to understand better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.