Intermediate Query Structure and Development

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

1 Introduction to Web Application Introduction to Data Base.
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Databases Chapter 7: Data Access and Manipulation.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Chapter 9 Joining Data from Multiple Tables
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
More Windowing Functions KEVIN MCCARTY. What are Windowing Functions Again? Introduced in SQL Server 2005 (SQL 2003 Standard) Used to provide operations.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
Random Query Generator for Hive November 2015 Hive Contributor Meetup Szehon Ho.
Background Lots of Demos(That’s it.)
A Glance at the Window Functions. Window Functions Introduced in SQL 2005 Enhanced in SQL 2012 So-called because they operate on a defined portion of.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Jeremy Kingry, eBECS | ADVANCED SQL SERVER FOR ADMINS AND ANALYSTS.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
T-SQL Power! Windows That Open Doors Adam
Data Analysis with SQL Window Functions Adam McDonald IT Architect / Senior SQL Developer Smith Travel
SQL Query Getting to the data ……..
More SQL: Complex Queries,
CS3220 Web and Internet Programming More SQL
Structured Query Language
Rob Gleasure robgleasure.com
In this session, you will learn to:
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Relational Database Design
T-SQL: Simple Changes That Go a Long Way
T-SQL: Simple Changes That Go a Long Way
Data Analysis with SQL Window Functions
Working with Tables: Join, Functions and Grouping
05 | Using Functions and Aggregating Data
Using Window Ranking, Offset, and Aggregate Functions
Chapter 2: Intro to Relational Model
Lecture#7: Fun with SQL (Part 2)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
GROUP BY & Subset Data Analysis
MIS2502: Review for Exam 1 JaeHwuen Jung
Rob Gleasure robgleasure.com
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
T-SQL gotchas and power-ups
MIS2502: Review for Exam 1 Aaron Zhi Cheng
Data Analysis with SQL Window Functions
Structured Query Language – The Fundamentals
T-SQL Performance Tuning
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Build on-the-fly reporting with Dynamic SQL
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 1: Basic Queries Aaron Zhi Cheng
Database Management System
Introduction to SQL Server and the Structure Query Language
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

Intermediate Query Structure and Development Dave Valentine dkValz@comcast.net www.ingenioussql.com

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

About Dave MCP MCSA: SQL Server 2012 Database Architect / Database Developer Adjunct Professor @IngeniousSQL dkValz@comcast.net IngeniousSQL.com

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

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}

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}

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}

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

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

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

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

SQL Window Functions Ranking returns a ranking value for each row in a specified partition RANK, ROW_NUMBER, NTILE, DENSE_RANK http://technet.microsoft.com/en-us/library/ms189798.aspx Aggregate returns an aggregated value for each row in a specified partition AVG, MIN, SUM, COUNT, STDEV, VAR, MAX http://technet.microsoft.com/en-us/library/ms173454.aspx Analytic also returns an aggregated value for each row in a specified partition, but much more LEAD, FIRST_VALUE, LAG, LAST_VALUE http://technet.microsoft.com/en-us/library/hh213234.aspx Basic SQL Window Function Syntax SELECT Col1, ROW_NUMBER OVER (PARTION BY Col1 ORDER BY Col2) FROM TableName

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 82.8345 550 45558.975 11 8786 907 8618 8619 8119 8120

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 82.8345 550 45558.975 11 4 8786 907 8618 8619 8119 8120

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 82.8345 550 45558.975 11 8786 907 12 8618 13 8619 14 8119 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) , 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 82.8345 550 45558.975 11 8786 907 12 8618 13 8619 14

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 82.8345 550 45558.975 11 4 8786 8618 8619

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; https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot

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

Common Table Expressions CTE Basic Common Table Expression Syntax ;WITH CteName (Col1, Col2) AS ( SELECT Col1, Col2 FROM TableName WHERE Col3 = [SomValue] ) SELECT Col1, Col2 FROM CteName; Simplify query syntax Multiple reference Recursive queries VIEW alternative http://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

Common Table Expressions Query syntax

Common Table Expressions CTE Basic Common Table Expression Syntax ;WITH CteName AS ( SELECT Col1, Col2 FROM TableName WHERE Col3 = [SomValue] ) SELECT Col1, Col2 FROM CteName; Simplify query syntax Multiple reference Recursive queries VIEW alternative http://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

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

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

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

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

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