Posts

Showing posts with the label Sub Queries

SQL Server, Sub-Queries vs. Cross Apply

Image
Hi Guys, Today for you a basic post! We do a comparison between a Query with Sub-Queries and with the Apply operator! So, what are differences between using sub-query and using cross/Outer Apply? Well, if you don't know you should absolutely read this post! Introduction For our comparison we choose these queries:     A query with two sub-queries (A subquery is simply a query with another query inside it's select) Select ( Select MAX (OrdRig.Qta1) as conta from OrdRig where Ordrig.idordtes = o.Id and OrdRig.IdComrig is not null), ( Select MAX (OrdRig.Qta2) as conta from OrdRig where Ordrig.idordtes = o.Id and OrdRig.IdComrig is not null) From eco..ordtes o   A query that use a Cross Apply operator select t . Qta1 , t . Qta2 from eco .. ordtes o CROSS APPLY   ( Select MAX ( OrdRig . Qta1 ) as Qta1 , MAX ( OrdRig . Qta2 ) as Qta2    from OrdRig    where Ordrig . ido...