Accounts And Transactions
SAMPLE SELECT SQL- USING APPLY COMMANDS
RESUME 
CROSS APPLY - Similar to INNER JOIN
OUTER APPLY - Similar to LEFT JOIN
 
 SQL Server APPLY operator has two variants; CROSS APPLY and OUTER APPLY
The CROSS APPLY operator returns only those rows from the left table expression (in its final output) if it matches with the right table expression. In other words, the right table expression returns rows for the left table expression match only.
The OUTER APPLY operator returns all the rows from the left table expression irrespective of its match with the right table expression. For those rows for which there are no corresponding matches in the right table expression, it contains NULL values in columns of the right table expression.
So you might conclude, the CROSS APPLY is equivalent to an INNER JOIN (or to be more precise its like a CROSS JOIN with a correlated sub-query) with an implicit join condition of 1=1 whereas the OUTER APPLY is equivalent to a LEFT OUTER JOIN.
 
Using APPLY
Both the left and right operands of the APPLY operator are table expressions. The main difference between these operands is that the right_table_source can use a table-valued function that takes a column from the left_table_source as one of the arguments of the function. The left_table_source can include table-valued functions, but it cannot contain arguments that are columns from the right_table_source.

The APPLY operator works in the following way to produce the table source for the FROM clause:

Evaluates right_table_source against each row of the left_table_source to produce rowsets.

The values in the right_table_source depend on left_table_source. right_table_source can be represented approximately this way: TVF(left_table_source.row), where TVF is a table-valued function.

Combines the result sets that are produced for each row in the evaluation of right_table_source with the left_table_source by performing a UNION ALL operation.

The list of columns produced by the result of the APPLY operator is the set of columns from the left_table_source that is combined with the list of columns from the right_table_source.

  
SAMPLE USING OUTER APPLY
Name
Account Number
Balance
Is Active
Count Itens
Amount
Andrea McCarthy
35692671
50001
False
4
321
Bernard
9217033
74523
True
0
0
Bradley
7209427
74523
True
0
0
Britney
6293664
74523
True
0
0
Bruno
5692671
74523
True
0
0
Camila
6139041
74523
True
0
0
Carl
9224662
74523
True
0
0
Casey
9507570
74523
True
0
0
Charles
3024663
74523
True
0
0
Dale Quinn
49507570
81025
False
3
334
Dennis Wise
27209427
52365
True
4
590
Edward Williams
49217033
8320
True
4
402
Emily
3266225
74523
True
0
0
Emily
5142770
74523
True
0
0
Erik Hubbard
69224662
37226
True
3
314.5
Fannie Woods
66139041
3420
True
3
591
Galter
0
0
True
0
0
George
6336512
74523
True
0
0
Haley
9343020
74523
True
0
0
Hamilton
7209427
74523
True
0
0
Hannah
5692671
74523
True
0
0
Harrison
9217033
74523
True
0
0
Ivan Gill
63266225
55012
False
2
334
Jacob
6139041
74523
True
0
0
Janice
3024663
74523
True
0
0
Jerome
9507570
74523
True
0
0
Justin
6293664
74523
True
0
0
Maggie Todd
26293664
4429
True
4
367
Myra Carson
39343020
58311
False
2
60
OSDC 2021 Teste
0
0
True
0
0
Robert Holland
85142770
33914
True
2
607
Rui testeq
0
0
True
0
0
Seth Lucas
96336512
1594
True
2
215
Willie Gibbs
70302466
17724
True
3
156
SAMPLE USING CROSS APPLY
Name
Account Number
Balance
Is Active
Count Itens
Amount
Andrea McCarthy
35692671
50001
False
4
321
Dale Quinn
49507570
81025
False
3
334
Dennis Wise
27209427
52365
True
4
590
Edward Williams
49217033
8320
True
4
402
Erik Hubbard
69224662
37226
True
3
314.5
Fannie Woods
66139041
3420
True
3
591
Ivan Gill
63266225
55012
False
2
334
Maggie Todd
26293664
4429
True
4
367
Myra Carson
39343020
58311
False
2
60
Robert Holland
85142770
33914
True
2
607
Seth Lucas
96336512
1594
True
2
215
Willie Gibbs
70302466
17724
True
3
156
Click here to see your activities