Cross apply and outer apply in sql server — Transcript
Full transcript
- 0:00this is part 91 of SQL Server tutorial
- 0:02in this video we'll discuss the cross
- 0:04apply and outer apply operators in SQL
- 0:07server with examples for the purpose of
- 0:10this demo we'll be using these two
- 0:12tables department and employee here is
- 0:14the SQL script to create those tables
- 0:16and populate them with test data I'll
- 0:18have the script available on my blog in
- 0:20case you need it Department ID column in
- 0:23this employee table is a foreign key
- 0:25referencing the ID column from
- 0:27Department table now what we want to do
- 0:30is write a query that's going to
- 0:31retrieve all the matching rows between
- 0:33these two tables within our result set
- 0:36we want Department name from Department
- 0:38table and employee name gender and
- 0:41salary from employee table so the result
- 0:43set should look like this notice we want
- 0:46only the matching rows between these two
- 0:49tables now we can very easily achieve
- 0:51this using an inner join so let's
- 0:53quickly write that in a join query so we
- 0:57want to select star
- 1:01from Department table let's give it an
- 1:04alas D and we want to innerjoin the
- 1:08department table with employee table
- 1:11let's give an alas for this employee
- 1:12table e and we need to specify the join
- 1:16condition D do ID equals e do Department
- 1:21ID and what are the columns that we want
- 1:23in our result set we want Department
- 1:26name from the Department table employee
- 1:29name
- 1:30employee agenda and employee salary so
- 1:34if we execute this query we should get
- 1:36all the matching rules between
- 1:38department and employee table notice
- 1:41within the result set we don't have any
- 1:43rules for administration and sales
- 1:44departments that's basically because
- 1:46within the employee table we don't have
- 1:48any employees belonging to that
- 1:51departments right so that's the reason
- 1:53why we don't have those rows in the
- 1:55result set now let's say we want to
- 1:57write another query which is going to
- 1:58retrieve all the matching rows plus
- 2:00non-matching rows from the Department
- 2:02table we can very easily achieve that
- 2:05using a left join instead of an inner
- 2:08join so when we perform a left join
- 2:10between department and employee table we
- 2:12are going to get all the rows from the
- 2:14Department table irrespective of whether
- 2:16they have a matching record in the
- 2:18employee table or not notice we have
- 2:20Administration and sales departments
- 2:22here since they don't have any employees
- 2:24in the employee table the name g and
- 2:27salary column from the employee table
- 2:29have n
- 2:30all right now let's say for some reason
- 2:32we don't have access to this physical
- 2:35table employee instead they have given
- 2:38us a function and that's a table valued
- 2:40function so if you give the department
- 2:43ID to that function it's going to give
- 2:45you the list of all employees belonging
- 2:48to that department first let's go ahead
- 2:51and create that function so we want to
- 2:54create a function let's call it FN
- 2:57uncore get
- 3:00employees
- 3:02by Department ID so we want to pass
- 3:06Department ID to this function let's
- 3:08call that department ID and the data
- 3:12type is integer so this is going to be a
- 3:14table valued function so this returns a
- 3:19table
- 3:21as return and then we'll specify our
- 3:24query here so select
- 3:27star from
- 3:30employee
- 3:31table
- 3:33where Department ID equals our input
- 3:38parameter at Department ID so let's go
- 3:42ahead and create this
- 3:45function so we have created the function
- 3:47now let's quickly test
- 3:50this select star from the name of our
- 3:53function is FN get employees by
- 3:56Department ID so for example if we pass
- 3:58Department ID one one this function is
- 4:01going to return us all the employees
- 4:03within that department so notice
- 4:06Department ID is one all right now what
- 4:09we want to do is we want to join the
- 4:11department table with this table valued
- 4:14function now to do that can we use an
- 4:17inner join or a left join if we try to
- 4:19do that you know we are going to get an
- 4:21error let's look at that quickly in
- 4:23action so here on the left we have our
- 4:25department table now what I want to do
- 4:28is join that with this function the
- 4:31table valued
- 4:33function and if you look at this
- 4:35function it has got an input parameter
- 4:38Department ID and I'm going to use this
- 4:41table name and pass d. ID there and the
- 4:46alas is still going to be e okay now
- 4:49look at this when I execute this query
- 4:51we are going to get an error look at
- 4:53that it says the multiart identifier d.
- 4:56ID cannot be bound okay so you cannot do
- 5:00an inner join like this between a
- 5:02physical table and a table valued
- 5:04function so this is where we use the
- 5:07cross apply and outer apply operators in
- 5:10SQL Server now I'm simply going to
- 5:12replace this inner join
- 5:16with cross apply
- 5:19operator okay so the cross apply
- 5:21operator is going to semantically behave
- 5:24like an inner joint whereas an outer
- 5:26apply operator is semantically going to
- 5:28behave like a left out to join so when
- 5:31we use cross apply we don't require any
- 5:33join condition because all this function
- 5:35requires is a department ID and on the
- 5:39left here we have a table department and
- 5:41we are passing the department ID so this
- 5:44function right here gets called for
- 5:47every row within the department table
- 5:50okay and the department ID is passed the
- 5:52employees are retrieved and you know the
- 5:55department name column is retrieved for
- 5:57every Row from the Department table
- 6:00and employee name gender and salary are
- 6:02retrieved from this function okay so we
- 6:06don't require this join condition so
- 6:09let's go ahead and execute this now we
- 6:12should get the same result only the
- 6:15matching rows between those two okay
- 6:18look at this when I execute this I get
- 6:21you know only it payroll and HR
- 6:23departments where there are employees
- 6:26whereas Administration and sales we
- 6:28don't have that in the results at so if
- 6:30you want even the non-matching rows from
- 6:32the Department table then instead of
- 6:34cross apply use outer apply so let's
- 6:38make a copy of
- 6:44this outer apply now when we execute
- 6:47this we should get the non-matching rows
- 6:49from Department table as well and notice
- 6:52the columns from the table valued
- 6:54function are null so this is
- 6:56semantically equivalent to um left out
- 7:00to
- 7:02join so what do this cross apply and
- 7:05outer apply operators in SQL Server
- 7:08these operators are actually introduced
- 7:09in SQL Server 2005 and they used to join
- 7:12a table with a table valued function the
- 7:16table valued function on the right hand
- 7:17side of the apply operator gets called
- 7:20for each row from the left table this
- 7:23left table is also usually called as out
- 7:25table cross apply returns only matching
- 7:28row so it's semantically equivalent to
- 7:30an engine join whereas outer apply
- 7:33returns matching plus non-matching rows
- 7:36so this is semantically equivalent to a
- 7:38left out join the unmatched Columns of
- 7:40the table valued function will be set to
- 7:44nulls as we can see here in the result
- 7:46set so name gener and salary are coming
- 7:48from the table valued function and since
- 7:51we don't have you know any result
- 7:54produced for these two rows you know
- 7:57from the Department table those Colum s
- 7:59from the table valued function will be
- 8:01set to
- 8:03null so here we have the cross apply
- 8:06operator example and here we have the
- 8:09outer apply operator
- 8:11example thank you for listening and have
- 8:13a great day
About this transcript
This page contains the full transcript of Cross apply and outer apply in sql server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,261 words across 191 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.