Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.
        DECLARE @AName        VARCHAR(100)
        DECLARE @BName        VARCHAR(100)
        DECLARE @CName        VARCHAR(100)

        SET @AName            = ''
        SET @BName            = ''
        SET @CName            = ''

        SELECT
            ChildsChildId,
            ChildsChildName,
            ParentsChildId,
            ParentsChildName,
            ParentId,
            ParentName
        FROM
            (
                SELECT
                        ''                        AS 'ChildsChildId',
                        ''                        AS 'ChildsChildName',
                        b.Id                    AS 'ParentsChildId',
                        b.[Name]                AS 'ParentsChildName',
                        p.Id                    AS 'ParentId',
                        p.[Name]                AS 'ParentName'
                FROM [sch].[ParentsChild] b
                    INNER JOIN [sch].[Parent] a
                            ON a.Id = b.Id
                WHERE NOT EXISTS
                (
                    SELECT *
                        FROM [sch].[ChildsChild] c
                        WHERE b.Id = c.Id
                          AND c.Id IS NOT NULL
                          AND c.Active = 1
                )
                AND b.Active = 1
                AND a.Active = 1

            UNION

                SELECT
                        c.Id        AS 'ChildsChildId',
                        c.[Name]    AS 'ChildsChildName',
                        b.Id        AS 'ParentsChildId',
                        b.[Name]    AS 'ParentsChildName',
                        a.Id        AS 'ParentId',
                        a.[Name]    AS 'ParentName'
                FROM    [sch].[ChildsChild] c RIGHT JOIN [sch].[ParentsChild] b
                    ON b.Id = c.Id
                        INNER JOIN [sch].[Parent] a
                    ON a.Id = b.Id
                WHERE    a.Active = 1
                        AND b.Active = 1
                        AND c.Active = 1

        ) a
        WHERE        (a.ParentName            = @AName    OR ISNULL(@AName, '') = '')
                AND    (a.ParentsChildName        = @BName    OR ISNULL(@BName, '') = '')
                AND (a.ChildsChildName        = @CName    OR ISNULL(@CName, '') = '')

I've this above query where I need to fetch all the records from the three tables. First table [sch].[Parent] has a field referenced as Foreign Key in second table [sch].[ParentsChild]. There are records which are in both the tables with their FK. Now, there is a third table [sch].[ChildsChild] which is also referenced by FK of second table [sch].[ParentsChild] table, but there are records missing for the entries in second table. So now I need to fetch all the records from three tables including JOINS for all three tables and for those also whose records are missing from third table i.e. [sch].[ChildsChild] table. I've the query, but need to know if there is any other more COST effective query than the above one.

share|improve this question
1  
You haven't provided the tables' definitions. So, we don't really know which columns are the primary keys, we don't know which columns are foreign keys and what they are referring to. – ypercube Dec 17 '12 at 19:30
As the question is now, the most sensible comment I can make is that you have a wrong query, because you mention foreign keys but you seem to be joining only on (what appear to be) primary keys and then you name those keys with different aliases (ChildsChildId, ParentsChildId) in the SELECT list. – ypercube Dec 17 '12 at 19:48

closed as too localized by Paul White, Mark Storey-Smith, jcolebrand Dec 19 '12 at 15:34

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.