Left outer join - where

Posted on 2010-01-04
Last Modified: 2012-05-08
I am using LEFT OUTER JOIN to Table2. If t2.CId is not =@CId I do not want to return that row.
How is best to do that?

T1Id   T1Text
1          One
2          Two

T2Id    T1Id   CId   T2Text
1          2       1        Eins
2          2       2        Zwei


Result should be:

T1Id  T1Text    T2Id      CId       T2Text
1        One        NULL   NULL    NULL
2        Two        1           1            Eins
It shold not return:
2        Two        2          2            Zwei
Question by:johnkainn
    LVL 26

    Expert Comment

    try this
    SELECT T1Id, T1Text, T2Id, CId, T2Text
    FROM Table1 T1
    		LEFT OUTER JOIN Table2 T2 ON T1.T1Id = T2.T2Id
    WHERE T2.CId = 1

    Open in new window

    LVL 19

    Accepted Solution

    SELECT T1Id, T1Text, T2Id, CId, T2Text
    FROM Table1 T1
                    LEFT OUTER JOIN Table2 T2 ON T1.T1Id = T2.T2Id AND T2.CId = 1
    LVL 26

    Expert Comment

    my query gives exactly what you are looking for. Why did you choose the other solution?
    LVL 19

    Expert Comment

    because your where clause overrules your left join :) give it a try in a testdatabae

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now