Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intermediate Query Structure and Development

Similar presentations


Presentation on theme: "Intermediate Query Structure and Development"— Presentation transcript:

1 Intermediate Query Structure and Development
Dave Valentine

2 Agenda JOINS SQL Functions Grouping SQL Window Functions SQL PIVOT Common Table Expressions Table Variables Temp Tables Performance Tuning

3 About Dave MCP MCSA: SQL Server 2012 Database Architect / Database Developer Adjunct IngeniousSQL.com

4 Table Joins Joining tables to single result set
Found in the FROM section of a query Types INNER LEFT RIGHT CROSS FULL Basic JOIN Syntax: SELECT Col1, Col2 FROM TableName1 tn1 INNER JOIN TableName2 tn2 ON tn1.Col1 = tn2.Col1 WHERE Col3 = [SomeValue] GROUP BY Col1, Col2

5 INNER JOIN Matches and returns all rows between two tables
INNER JOIN / JOIN Typically based on Primary and Foreign Key relationships Most common join Operators Set Theory A B A = {1,2,3,4,5,6} B = {4,5,6,7,8,9} A B = {4,5,6}

6 LEFT & RIGHT JOIN B (A B) A (B A) A = {1,2,3,4,5,6} A = {1,2,3,4,5,6}
LEFT JOIN returns all data from the left table and only records that match from the right table LEFT JOIN / LEFT OUTER JOIN RIGHT JOIN returns all data from the right table and only records that match from the left table RIGHT JOIN / RIGHT OUTER JOIN B (A B) A (B A) A = {1,2,3,4,5,6} A = {1,2,3,4,5,6} B = {4,5,6,7,8,9} B = {4,5,6,7,8,9} = {4,5,6,7,8,9} = {1,2,3,4,5,6}

7 Full and Cross Joins A B A = {1,2,3,4,5,6} B = {4,5,6,7,8,9} A B
FULL OUTER JOIN returns all data that matches between the tables and also the data that does not match either side FULL JOIN / FULL OUTER JOIN CROSS JOIN returns all data from unrelated tables and produces a Cartesian Product A B A = {1,2,3,4,5,6} B = {4,5,6,7,8,9} A B = {1,2,3,4,5,6,7,8,9}

8 Cartesian Product Basic CROSS JOIN Syntax:
1 Jordan 2 Alexis 3 Cory 4 Daniel 5 Benjamin 1 Jordan Football 2 Softball 3 All Sports 4 Baseball 5 Bowling Basic CROSS JOIN Syntax: SELECT PersonID, FistName, SportID, SportName FROM Person P CROSS JOIN Sports s 2 Alexis 1 Football Softball 3 All Sports 4 Baseball 5 Bowling 1 Football 2 Softball 3 All Sports 4 Baseball 5 Bowling 3 Cory 1 Football 2 Softball All Sports 4 Baseball 5 Bowling 4 Daniel 1 Football 2 Softball 3 All Sports Baseball 5 Bowling 5 Benjamin 1 Football 2 Softball 3 All Sports 4 Baseball Bowling

9 Numerical Functions Built in functionality that aggregates numerical columns Popular SQL Server Numerical Functions ABS Returns the absolute value of a number AVG Returns the average value of an expression COUNT Returns the number of records returned by a select query MAX Returns the maximum value in a set of values MIN Returns the minimum value in a set of values POWER Returns the value of a number raised to the power of another number ROUND Rounds a number to a specified number of decimal places SQRT Returns the square root of a number SQUARE Returns the square of a number SUM Calculates the sum of a set of values

10 Product ID Name Number Sold 1 Adjustable Race 2500 2 Bearing Ball 2000 1000 3 BB Ball Bearing 800 500 4 Headset Ball Bearings 200 Headset Ball Bearings 600

11 GROUP BY Used to arrange identical rows of data into groups
Groups are determined by the columns specified  Basic GROUP BY Syntax SELECT Col1, Col2 FROM TableName WHERE Col3 = [SomeValue] GROUP BY Col1, Col2 Typically follows the WHERE statement Commonly used with aggregating functions

12 SQL Window Functions Ranking returns a ranking value for each row in a specified partition RANK, ROW_NUMBER, NTILE, DENSE_RANK Aggregate returns an aggregated value for each row in a specified partition AVG, MIN, SUM, COUNT, STDEV, VAR, MAX Analytic also returns an aggregated value for each row in a specified partition, but much more LEAD, FIRST_VALUE, LAG, LAST_VALUE Basic SQL Window Function Syntax SELECT Col1, ROW_NUMBER OVER (PARTION BY Col1 ORDER BY Col2) FROM TableName

13 SQL Window Functions SELECT PurchaseOrderDetailID , ProductID
, UnitPrice , OrderQty , TotalCost = UnitPrice * OrderQty , Rank = RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) FROM Purchasing.PurchaseOrderDetail; PurchaseOrderDetailID ProductID UnitPrice OrderQty TotalCost Rank 8842 881 41.57 6000 249420 1 8843 882 8844 883 8845 884 8807 849 24.75 5000 123750 5 8808 850 8809 851 8827 867 26.18 2000 52360 8 8828 868 8830 869 8785 948 550 11 8786 907 8618 8619 8119 8120

14 SQL Window Functions SELECT PurchaseOrderDetailID , ProductID
, UnitPrice , OrderQty , TotalCost = UnitPrice * OrderQty , Rank = RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) , DenseRank = DENSE_RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) FROM Purchasing.PurchaseOrderDetail; PurchaseOrderDetailID ProductID UnitPrice OrderQty TotalCost Rank DenseRank 8842 881 41.57 6000 249420 1 8843 882 8844 883 8845 884 8807 849 24.75 5000 123750 5 2 8808 850 8809 851 8827 867 26.18 2000 52360 8 3 8828 868 8830 869 8785 948 550 11 4 8786 907 8618 8619 8119 8120

15 SQL Window Functions SELECT PurchaseOrderDetailID , ProductID
, UnitPrice , OrderQty , TotalCost = UnitPrice * OrderQty , Rank = RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) , DenseRank = DENSE_RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) , RowNumber = ROW_NUMBER() OVER (ORDER BY UnitPrice * OrderQty DESC) FROM Purchasing.PurchaseOrderDetail; PurchaseOrderDetailID ProductID UnitPrice OrderQty TotalCost Rank DenseRank RowNumber 8842 881 41.57 6000 249420 1 8843 882 2 8844 883 3 8845 884 4 8807 849 24.75 5000 123750 5 8808 850 6 8809 851 7 8827 867 26.18 2000 52360 8 8828 868 9 8830 869 10 8785 948 550 11 8786 907 12 8618 13 8619 14 8119 15

16 SQL Window Functions SELECT PurchaseOrderDetailID , ProductID
, UnitPrice , OrderQty , TotalCost = UnitPrice * OrderQty , Rank = RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) , DenseRank = DENSE_RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) , RowNumber = ROW_NUMBER() OVER (ORDER BY UnitPrice * OrderQty DESC) , RowDist = NTILE(1000) OVER (ORDER BY UnitPrice * OrderQty DESC) FROM Purchasing.PurchaseOrderDetail; PurchaseOrderDetailID ProductID UnitPrice OrderQty TotalCost Rank DenseRank RowNumber RowDist 8842 881 41.57 6000 249420 1 8843 882 2 8844 883 3 8845 884 4 8807 849 24.75 5000 123750 5 8808 850 6 8809 851 7 8827 867 26.18 2000 52360 8 8828 868 9 8830 869 10 8785 948 550 11 8786 907 12 8618 13 8619 14

17 SQL Window Functions SELECT PurchaseOrderDetailID , ProductID
, UnitPrice , OrderQty , TotalCost = UnitPrice * OrderQty , Rank = RANK() OVER (ORDER BY UnitPrice * OrderQty DESC) , PartitionRank = RANK() OVER (PARTITION BY ProductID ORDER BY UnitPrice * OrderQty DESC) FROM Purchasing.PurchaseOrderDetail; PurchaseOrderDetailID ProductID UnitPrice OrderQty TotalCost Rank PartitionRank 8842 881 41.57 6000 249420 1 8843 882 8844 883 8845 884 8807 24.75 5000 123750 5 2 8808 8809 8827 867 26.18 2000 52360 8 8828 8830 8785 550 11 4 8786 8618 8619

18 PIVOT Transforms unique values from a single column into a data set with multiple columns Basic PIVOT syntax SELECT AnyCol, Value1, Value2 FROM ( SELECT AnyCol, DataCol, AggrCol FROM TableName ) t PIVOT(MIN(AggrCol) FOR DataCol IN ([Value1], [Value2])) AS pvt;

19 PIVOT Example SELECT [CustomerID] , [Butterfinger] , [Rolo]
ProductName Quantity 1 Snickers Butterfinger Rolo 2 CustomerID Butterfinger Rolo Snickers 1 2 NULL SELECT [CustomerID] , [Butterfinger] , [Rolo] , [Snickers] FROM ( , [ProductName] , [Quantity] FROM CustomerProductOrder ) P PIVOT ( SUM(Quantity) FOR P.ProductName IN ([Butterfinger],[Rolo],[Snickers]) ) AS PVT

20 Common Table Expressions
CTE Basic Common Table Expression Syntax ;WITH CteName (Col1, Col2) AS ( SELECT Col1, Col FROM TableName WHERE Col3 = [SomValue] ) SELECT Col1, Col2 FROM CteName; Simplify query syntax Multiple reference Recursive queries VIEW alternative

21 Common Table Expressions
Query syntax

22 Common Table Expressions
CTE Basic Common Table Expression Syntax ;WITH CteName AS ( SELECT Col1, Col FROM TableName WHERE Col3 = [SomValue] ) SELECT Col1, Col2 FROM CteName; Simplify query syntax Multiple reference Recursive queries VIEW alternative

23 Table Variables Temporary table variable that temporarily persists data Useful for storing small datasets needed multiple times Very Fast Limited Basic Table Variable Syntax TABLE ( Col1 INT Col2 NVARCHAR(50) ) Can help with query performance

24 Temp Tables Temporary table that temporarily persists data
Useful for storing datasets needed multiple times Full functionality Basic Temp Table Syntax CREATE TABLE #TempTable ( Col1 INT Col2 NVARCHAR(50) ) Alternate Temp Table Syntax SELECT Col1, Col2 INTO #TempTable FROM TableName; Can help with query performance

25 Temp Tables vs Table Variables
Table Variables are Created in Memory initially Temp Tables are created in TempDB Table Variables are not included in transaction Temp Tables can have indexes Temp Tables can have schema modifications Table Variables small data sets Temp Tables large data sets

26 Summary JOINS SQL Functions Grouping SQL Window Functions SQL PIVOT Common Table Expressions Table Variables Temp Tables Performance Tuning

27 Questions Dave Valentine @ingeniousSQL ingeniousSQL.com
linkedin.com/in/ingenioussql


Download ppt "Intermediate Query Structure and Development"

Similar presentations


Ads by Google