0

I am not so advanced sql user. Could you please review my following query is it optimal ? or I can do something more more optimized and more readable?

      select  Distinct  DT.Station  , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
            from    (
                        select  Station, Slot, SubSlot, CompID
                        from    DeTrace
                        where   DeviceID = '1151579773'     
                    ) as DT 
            Left outer CList as CL  
                  on  DT.CompID = CL.CompID 
                  where CL.CompName = '9234220'
                  order by CompName 

Thanks for help.

5 Answers 5

2

It is easier to read like this:

 select  Distinct  DT.Station  , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
            from  DeTrace DT    
            Left outer join CList as CL  
                  on  DT.CompID = CL.CompID 
                  where CL.CompName = '9234220'
                    and DT.DeviceID = '1151579773'
                  order by CompName 

The optimiser should be able to perform this query as efficiently as yours, but you should check the query execution plan just to be sure.

Sign up to request clarification or add additional context in comments.

Comments

0

Why not just:

SELECT DISTINCT DT.Station  , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName
FROM DeTrace DT
LEFT OUTER JOIN CList CL ON DT.CompID = CL.CompID
                       AND DT.DeviceID = '1151579773'
                       AND CL.CompName = '9234220'
ORDER BY CL.CompName

Comments

0

I think you don't need subquery. Check below:

select  Distinct  DT.Station  , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName            
from    DeTrace as DT 
Left outer CList as CL  
on  DT.CompID = CL.CompID 
where CL.CompName = '9234220' and DeviceID = '1151579773'     
            order by CompName 

2 Comments

If you use a WHERE clause in a join, it first joins ALL the records and then filter the result. If you have the condition IN the join, then it first filters before joining all the records
I agree with you. But it may not return expected output and hence it's always advisable to put search predicate in where clause when using Outer Join as shown here
0

I like this way best:

SELECT  DISTINCT
        DeTrace.Station,
        DeTrace.Slot,
        DeTrace.SubSlot,
        DeTrace.CompID,
        CL.CompName
    FROM
        DeTrace
    LEFT OUTER JOIN
        CList AS CL ON
            DeTrace.CompID = CL.CompID
    WHERE
        DeviceID = '1151579773' AND
        CL.CompName = '9234220'
    ORDER BY
        CL.CompName

Comments

0
select  Distinct  DT.Station  , DT.Slot , DT.SubSlot, DT.CompID , CL.CompName                  
from    DeTrace DT
Left outer join CList as CL  
          on  DT.CompID = CL.CompID 
where DT.DeviceID = '1151579773' 
and  CL.CompName = '9234220'    
order by CL.CompName

The Sql Server Cost Based Optimizer should be able to determine the most efficient order in which to apply the conditions.

Comments

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.