When should I use cross apply over inner join?

前端 未结 14 1393
忘了有多久
忘了有多久 2020-11-22 06:51

What is the main purpose of using CROSS APPLY?

I have read (vaguely, through posts on the Internet) that cross apply can be more efficient when selectin

14条回答
  •  离开以前
    2020-11-22 07:23

    Cross apply can be used to replace subquery's where you need a column of the subquery

    subquery

    select * from person p where
    p.companyId in(select c.companyId from company c where c.companyname like '%yyy%')
    

    here i won't be able to select the columns of company table so, using cross apply

    select P.*,T.CompanyName
    from Person p
    cross apply (
        select *
        from Company C
        where p.companyid = c.companyId and c.CompanyName like '%yyy%'
    ) T
    

提交回复
热议问题