T-SQL - Working with subqueries and APPLY

  13 Mar 2019
   tsql

Note : The following scripts are the part of the course on edx titled as: Querying Data with Transact-SQL -
Querying Data with Transact-SQL
These queries works on AdventureWorks database and information regarding the same
can be accessed by visiting following link -
AdventureWorks Installation and configuration


/* Sub Queries - nested queries
Scalar subquery returns single values.
Multi values subquery return mutliple values as a single column which can be access using IN
*/

Select Max(UnitPrice) from SalesLT.SalesOrderDetail

Select * from SalesLT.Product where ListPrice > (Select Max(UnitPrice) From SalesLT.SalesOrderDetail)

/* Self Contained - Correlated
Most subqueries are self-contained and have no connection with outer query other than passing results
where as
correlated sub queries refers to elements of tables used in outer query, behaves as inner query executed
once per outer row */

Select CustomerID, SalesOrderID, OrderDate From SalesLT.SalesOrderHeader as S01

Select CustomerID, SalesOrderID, OrderDate From SalesLT.SalesOrderHeader as S01
where OrderDate = (Select Max(OrderDate) From SalesLT.SalesOrderHeader)


Select CustomerID, SalesOrderID, OrderDate From SalesLT.SalesOrderHeader as S01
Where orderdate = (Select Max(orderdate) from SalesLT.SalesOrderHeader As S02 where S02.CustomerID = S01.CustomerID)
Order By CustomerID


/* Apply operator
Cross Apply -  Apply right table expression to each row in left table - Conceptually lik Outer Join
Outer Apply -  add rows for thoses with null in columns for right table - conceptually like Left Outer Join


Select SOH.SalesOrderID, MUP.MaxUnitPrice From SalesLT.SalesOrderHeader as SOH Cross Apply SalesLT.udfMaxUnitPrice(SOH.SalesOrderID) as MUP Order BY SOH.SalesOrderID

*/

-- Setup
CREATE FUNCTION SalesLT.udfMaxUnitPrice (@SalesOrderID int)
RETURNS TABLE
AS
RETURN
SELECT SalesOrderID,Max(UnitPrice) as MaxUnitPrice FROM
SalesLT.SalesOrderDetail
WHERE SalesOrderID=@SalesOrderID
GROUP BY SalesOrderID;

--Display the sales order details for items that are equal to
-- the maximum unit price for that sales order
SELECT * FROM SalesLT.SalesOrderDetail AS SOH
CROSS APPLY SalesLT.udfMaxUnitPrice(SOH.SalesOrderID) AS MUP
WHERE SOH.UnitPrice=MUP.MaxUnitPrice
ORDER BY SOH.SalesOrderID;

  T-SQL - Using Functions and aggregating data

  09 Mar 2019
   tsql

Note : The following scripts are the part of the course on edx titled as: Querying Data with Transact-SQL -
Querying Data with Transact-SQL
These queries works on AdventureWorks database and information regarding the same
can be accessed by visiting following link -
AdventureWorks Installation and configuration


Functions in TSQL

We can use multiple inbuilt functions in TSQL to achieve complex tasks in a very simpler manner. We’ll discuss the following -

For more details regarding built in functions in TSQL, please visit -
https://docs.microsoft.com/en-us/sql/t-sql/functions/functions?view=sql-server-2017

  T-SQL - Querying multiple tables using Set Operators

  08 Mar 2019
   tsql

Note : The following scripts are the part of the course on edx titled as: Querying Data with Transact-SQL -
Querying Data with Transact-SQL
These queries works on AdventureWorks database and information regarding the same
can be accessed by visiting following link -
AdventureWorks Installation and configuration


Union, Intersectm Except Union returns result set of distinct rows combined from all statements Union all retains duplicates Column Names aka are decided in the first query only not in further queries, No. of Columns should be same and compatible data types

Select FirstName, LastName , 'Employee' As Type
From SalesLT.Customer
Union
Select FirstName, LastName, 'Employee Duplicate'
From SalesLT.Customer
Order By LastName;
Select FirstName, LastName , 'Employee' As Type
From SalesLT.Customer
Union ALL
Select FirstName, LastName, 'Employee Duplicate'
From SalesLT.Customer
Order By LastName;