Why should I learn this stuff?
Database definitions
I don't have a server!
Everything is stored electronically; a lot of it is stored in databases
Maybe there is information stored in there that you need to automate your work
Order
table has one column, CustomerID
, that points back to one row in the Customer
tableSELECT
INSERT
UPDATE
DELETE
Search for "SQL Server Virtual Labs"
You can take them for a spin for free
Live in the cloud
Created on the fly automatically
When you close it up, the virtual computer is destroyed
Use IE & do bing search for SQL Server Virtual Labs
Look for one that has the word "report" in it
Hotlaunchpad?
Opens a VM and a manual
Scroll until you find the info on how to login to the VM
(Mine automatically logged me in)
Had to read doc instructions because her instructions were different to launch SQL Server 2008 R2 Management Studio
My VM has 2012?
She goes to the start menu, all programs, SQL Server 2008 R2, SQL Server 2008 R2 Management Studio
Hit Connect button
Server name: DEMO2010A
Expand Database
AdventureWorks2008R2 & AdventureWorksDW2008R2 are the ones to work with
Expand, Tables, HumResources.Employee, Right Click, Select Top 1000 Rows
Query window pops open
Top shows SELECT statement, bottom shows table
SELECT
Part 1Tools
so we can follow along or work on our own to learn more
Hello, World!
Retrieving Data
Filtering Data
Ordering Data
SQL Server
If you have a SQL Server available, download the sample OLTP database, AdventureWorks, from www.Codeplex.com
Figure out which version of SQL server you are running and download that specific version
You can start with anything from 2008 to 2012 to follow along
SQL Server free virtual labs
http://www.microsoft.com/sqlserver/en/us/learning-center/virtual-labs.aspx
Go back to first video to learn more on how to do this
She goes to the start menu, all programs, SQL Server 2008 R2, SQL Server 2008 R2 Management Studio
AdventureWorks2008R2 is the ones to work with
(I went to MSBI box to get into SSMS)
How to print something on the screen
Click New Query
Select line you want to run, otherwise everything will run
Typed in PRINT 'Hello, World!'
Highlight it
Press Execute button
Hello, World!
displays in Messages tab
Type in SELECT 'Hello, World!'
Highlight it
Execute
This time the results come back in a grid
There are buttons up top that let you select whether you want the results to go to text, grid or a file
Selected text and you get text in a Results tab at bottom of screen
Type in
USE AdventureWorks2012
GO
GO
is a batch separator (separate batches of information from one another)
dbo
SELECT
, (*
in this example)FROM
, followed by the table nameSchema.Table
dbo
, she may or may not specify dbo
SELECT * FROM HumanResources.Department
Results to Grid
SELECT * FROM Department
Highlight and Execute
--
two dashes in front of a line is a single line comment
/* */
is a multi line comment
SELECT *
USE [AdventureWorks2012]
GO
SELECT [DepartmentID]
,[Name]
,[GroupName]
,[ModifiedDate]
FROM [HumanResources].[Department]
GO
She commented out some of the lines
*
WHERE
clause to compare one of the columns to a value
[]
surrounding columns are optional except for column names that are not legal; I.E. a column name with a space in it or a reserved keyword in it
SELECT [BusinessEntityID]
, [JobTitle]
, [BirthDate]
, [MaritalStatus]
, [Gender]
, [HireDate]
FROM [HumanResources].[Employee]
Highlight and Execute
WHERE [BusinessEntityID] = 3
Highlight and Execute
WHERE 1 = 1
She got back the entire table
WHERE 1 = 2
She got back nothing
SELECT [BusinessEntityID]
, [JobTitle]
, [BirthDate]
, [MaritalStatus]
, [Gender]
, [HireDate]
FROM [HumanResources].[Employee]
WHERE Gender = 'F'
Highlight and Execute
SELECT [BusinessEntityID]
, [JobTitle]
, [BirthDate]
, [MaritalStatus]
, [Gender]
, [HireDate]
FROM [HumanResources].[Employee]
WHERE BirthDate BETWEEN '1/1/1970' AND '12/31/1970'
Highlight and Execute
BETWEEN
is inclusive
WHERE
clauses
ORDER BY
clause
SELECT [BusinessEntityID]
, [JobTitle]
, [BirthDate]
, [MaritalStatus]
, [Gender]
, [HireDate]
FROM [HumanResources].[Employee]
ORDER BY [BirthDate]
Highlight and Execute
ORDER BY [HireDate]
Highlight and Execute
ORDER BY [HireDate] DESC
Can specify more than one column Highlight and Execute Type in
ORDER BY [JobTitle], [BirthDate] DESC
Highlight and Execute
SELECT
statementSELECT
statements
SELECT
Part 2How to use expressions and functions in your T-SQL statements
Tools
Concatenate Columns
Mathematical Operations
Using Functions
CASE
SQL Server
If you have a SQL Server available, download the sample OLTP database, AdventureWorks, from www.Codeplex.com
Figure out which version of SQL server you are running and download that specific version
You can start with anything from 2008 to 2012 to follow along
SQL Server free virtual labs
http://www.microsoft.com/sqlserver/en/us/learning-center/virtual-labs.aspx
Go back to first video to learn more on how to do this
--Concatenating character data
SELECT Name, GroupName,
[Name] + [GroupName]
FROM HumanResources.Department
Highlight and Execute
+
sign we can concatenate these two columns together
--Adding an alias
SELECT Name, GroupName,
[Name] + [GroupName] AS "New Name"
FROM HumanResources.Department
Highlight and Execute
SELECT Name AS "Old Name", GroupName,
[Name] + [GroupName] AS "New Name"
FROM HumanResources.Department
Highlight and Execute
SELECT Name GroupName,
[Name] + [GroupName] AS "New Name"
FROM HumanResources.Department
Highlight and Execute
AS
keyword, even though it's optional so that she's very clear in what she's trying to accomplish
--Add a literal value
SELECT Name OldName, GroupName,
[GroupName] + ': ' + [Name] AS "New Name"
FROM HumanResources.Department
Highlight and Execute
--Add a number to a string
SELECT Name + DepartmentID
FROM HumanResources.Department
Highlight and Execute
CAST
function
CONVERT
function
--Cast and Convert
SELECT Name + CAST(DepartmentID AS VARCHAR) AS "Cast",
Name + CONVERT(VARCHAR, DepartmentID) AS "Convert"
FROM HumanResources.Department
Highlight and Execute
--NULLS
SELECT AddressLine1, AddressLine2
FROM Person.Address
Highlight and Execute
AddressLine1
has good data, AddressLine2
has NULL
NULL
value means unknown
SELECT AddressLine1, AddressLine2,
AddressLine1 + ' ' + AddressLine2
FROM Person.Address
Highlight and Execute
AddressLine1 + NULL = NULL
NULL
, we end up with NULL
as a result
AddressLine2
and we get what we were after
ISNULL
COALESCE
--ISNULL and COALESCE
SELECT AddressLine1, AddressLine2,
AddressLine1 + ' ' + ISNULL(AddressLine2,'') AS "ISNULL",
AddressLine1 + ' ' + COALESCE(AddressLine2,'') AS "COALESCE"
FROM Person.Address
Highlight and Execute
ISNULL
, we provide two arguments
NULL
or not
NULL
then the second argument is the replacement
COALESCE
, we actually have multiple values (more than two)
ISNULL
and COALESCE
are working exactly the same
--More COALESCE
SELECT Size, Class, Color
,COALESCE(Size,Class,Color) AS "COALESCE"
FROM Production.Product
WHERE Color IS NOT NULL OR Size IS NOT NULL
OR Class IS NOT NULL
Highlight and Execute
WHERE
clause to filter out where all 3 are NULL
COALESCE
is looking through the values and returning the first non-NULL value
CONCAT
CONCAT
we can supply the values we want to concatenate and we don't have to worry about NULL
NULL
values to an empty string
/* NEW FUNCTION IN SQL SERVER 2012!
CONCAT(Co11,Co12,Co13)
*/
What if we are working with numbers?
--Mathematical operators
SELECT BusinessEntityID,
BusinessEntityID * 2 AS "Multiply",
BusinessEntityID / 2 AS "Divide",
BusinessEntityID + 2 AS "Add",
BusinessEntityID - 2 AS "Subtract",
BusinessEntityID % 2 AS "Modulo"
FROM HumanResources.Employee
Highlight and Execute
--Mathematical functions
SELECT BusinessEntityID,
SQUARE(BusinessEntityID) AS "Square",
SQRT(BusinessEntityID) AS "SQRT",
ROUND(SQRT(BusinessEntityID),2) AS "Round"
FROM HumanResources.Employee
Highlight and Execute
SQRT
function is nested inside the ROUND
function
ROUND
rounds the SQRT
decimals to two places
--String functions
SELECT Description, LEFT(Description,10) AS "Left 10",
SUBSTRING(Description,11,30) AS "Substring",
CHARINDEX('Alu',Description) AS "Charindex"
FROM Production.ProductDescription
Highlight and Execute
LEFT
selects just the first 10 chars from the string
SUBSTRING
starts from the middle of the string
CHARINDEX
allows us to search for a char or number of characters and returns the position
--Date functions
SELECT BirthDate, HireDate,
DATEADD(yyyy,10,HireDate) AS "DateAdd",
DATEDIFF(YYYY,BirthDate,HireDate) AS "DateDiff",
YEAR(BirthDate) AS "Year",
GETDATE()
FROM HumanResources.Employee
Highlight and Execute
DATEADD
adds 10 years to the HireDate
DATEDIFF
finds out how many years between BirthDate and HireDate
YEAR
returns just the year
GETDATE
returns the current date and time
DATEDIFF
is not calcuating the real difference between the entire date
/* NEW FUNCTIONS in SQL Server 2012!!!
FORMAT, EOMONTH, DATEFROMPARTS
*/
FORMAT
is just like the format function in .Net
EOMONTH
returns the last day of the month
DATEFROMPARTS
has several sub-functions
CONVERT
function with the optional STYLE
parameter to specify how to format your date
--Convert
SELECT BirthDate,
CONVERT(VARCHAR,BirthDate,111) AS "Slashes",
CONVERT(VARCHAR,BirthDate,107) AS "Spelled out"
FROM HumanResources.Employee
Highlight and Execute
BirthDate
is stored as a number but is presented in human readable form
--CASE
SELECT BusinessEntityID,
CASE BusinessEntityID % 2 WHEN 0 THEN 'Even'
WHEN 1 THEN 'Odd' Else 'UNKNOWN' END AS OddOREven
FROM HumanResources.Employee
Highlight and Execute
/* NEW FUNCTIONS IN SQL SERVER 2012
IIF
*/
IIF
- in-line IF
CASE
--Order by
SELECT HireDate
FROM HumanResources.Employee
ORDER BY MONTH(HireDate)
Highlight and Execute
SELECT
Part 3In this video, we'll learn more about the WHERE
clause
Tools
Using functions and expressions
NULL
Multiple conditions
The IN
list
SQL Server
If you have a SQL Server available, download the sample OLTP database, AdventureWorks, from www.Codeplex.com
Figure out which version of SQL server you are running and download that specific version
You can start with anything from 2008 to 2012 to follow along
SQL Server free virtual labs
http://www.microsoft.com/sqlserver/en/us/learning-center/virtual-labs.aspx
Go back to first video to learn more on how to do this
--Using an expression in the WHERE clause
SELECT BusinessEntityID
FROM HumanResources.Employee
WHERE BusinessEntityID % 2 = 0
Highlight and Execute
--Using an expression in the WHERE clause
SELECT BusinessEntityID
FROM HumanResources.Employee
WHERE BusinessEntityID % 2 != 0
Highlight and Execute
!=
is not equals
<>
is the same as !=
(from Visual Basic)
--Using a function in the WHERE clause
SELECT BusinessEntityID, HireDate
FROM HumanResources.Employee
WHERE YEAR(HireDate) = 2003
Highlight and Execute
--A better way to write this!
SELECT BusinessEntityID, HireDate
FROM HumanResources.Employee
WHERE HireDate >= '1/1/2003' AND HireDate < '1/1/2004'
Highlight and Execute
BETWEEN
operator
--BETWEEN
SELECT BusinessEntityID, HireDate
FROM HumanResources.Employee
WHERE HireDate BETWEEN '1/1/2003' AND '12/31/2003'
Highlight and Execute
BETWEEN
SELECT BusinessEntityID, HireDate
FROM HumanResources.Employee
WHERE BusinessEntityID BETWEEN 10 AND 15
Highlight and Execute
SELECT BusinessEntityID, HireDate
FROM HumanResources.Employee
WHERE BusinessEntityID BETWEEN 15 AND 10
Highlight and Execute
SELECT BusinessEntityID, HireDate
FROM HumanResources.Employee
WHERE BusinessEntityID NOT BETWEEN 10 AND 15
Highlight and Execute
NOT
operator is used a lot in queries
SET STATISTICS IO ON
Highlight and Execute
SELECT FirstName, LastName
FROM Person.Person
WHERE LEFT(Lastname,1) ='M'
Highlight and Execute
LastName
LIKE
--Like
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName LIKE 'M%'
Highlight and Execute
LIKE
and other ways to specify all characters
--The second column in the index
SELECT FirstName, LastName
FROM Person.Person
WHERE FirstName = 'Zheng'
Highlight and Execute
--Nulls
--19614 values
SELECT AddressLine1, AddressLine2
FROM Person.Address
Highlight and Execute
--4 values
SELECT AddressLine1, AddressLine2
FROM Person.Address
WHERE AddressLine2 = '# 4'
Highlight and Execute
--358 values
SELECT AddressLine1, AddressLine2
FROM Person.Address
WHERE AddressLine2 <> '# 4'
Highlight and Execute
NULL
SELECT AddressLine1, AddressLine2
FROM Person.Address
WHERE AddressLine2 <> '# 4'
OR AddressLine2 IS NULL
Highlight and Execute
ISNULL
function
SELECT AddressLine1, AddressLine2
FROM Person.Address
WHERE ISNULL(AddressLine2,'') <> '# 4'
Highlight and Execute
--Multiple conditions
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName = 'Adams' AND FirstName = 'Edward'
Highlight and Execute
--Multiple conditions
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName = 'Adams' OR FirstName = 'Edward'
Highlight and Execute
/*Return a list of names the last name
must be Adams or Brown, the first name
must be Edward
*/
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName = 'Adams' OR LastName = 'Brown'
AND FirstName = 'Edward'
Highlight and Execute
AND
first, then the OR
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName = 'Adams' OR FirstName = 'Edward'
AND LastName = 'Brown'
Highlight and Execute
()
to force SQL Server to evaluate differently
SELECT FirstName, LastName
FROM Person.Person
WHERE (LastName = 'Adams' OR LastName = 'Brown')
AND FirstName = 'Edward'
Highlight and Execute
SELECT FirstName, LastName
FROM Person.Person
WHERE LastName IN ('Adams','Brown')
AND FirstName = 'Edward'
Highlight and Execute
JOIN
ing Tables Part 1Learn how to JOIN
two or more tables in one query
Why do we need to JOIN
tables?
How to JOIN
tables
Examples
SQL Server
If you have a SQL Server available, download the sample OLTP database, AdventureWorks, from www.Codeplex.com
Figure out which version of SQL server you are running and download that specific version
You can start with anything from 2008 to 2012 to follow along
SQL Server free virtual labs
http://www.microsoft.com/sqlserver/en/us/learning-center/virtual-labs.aspx
Go back to first video to learn more on how to do this
JOIN
tables?Why not have all the data in 1 gigantic table?
App wouldn't perform very well
Designed via a process called normalization
One table for each type of object
Key icon next to a field means that field is a primary key
JOIN
TablesSELECT col1, col2
FROM table1, table2
WHERE col1 = col2
Preferred syntax:
SELECT col1, col2
FROM table1 INNER JOIN table2
ON col1 = col2
Column names of primary key and foreign key can be identical or not
Will explain reason why she doesn't like old syntax later
/*
Requirements: Create a list of current employees,
their names, titles and departments
*/
First thing is to take a look at the tables we have available
SELECT * FROM HumanResources.Employee
Highlight and Execute
--Join the Employee table to the Person.Person
--table to display the employee name with the title.
SELECT emp.BusinessEntityID AS EmployeeID, contact.FirstName,
contact.Lastname, emp.JobTitle
FROM HumanResources.Employee AS emp
INNER JOIN Person.Person AS contact
ON emp.BusinessEntityID = contact.BusinessEntityID
ORDER BY emp.BusinessEntityID
Highlight and Execute
BusinessEntityID
becuase the field name is exactly the same in both tables & this qualifies where the data is coming from
HumanResources.Employee
Person.Person
INNER
is optional
EmployeeDepartmentHistory
that we have to JOIN
to
SELECT emp.BusinessEntityID AS EmployeeID, contact.FirstName,
contact.Lastname, emp.JobTitle,
edh.DepartmentID, edh.StartDate, edh.EndDate
FROM HumanResources.Employee AS emp
INNER JOIN Person.Person AS contact
ON emp.BusinessEntityID = contact.BusinessEntityID
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
ON emp.BusinessEntityID = edh.BusinessEntityID
WHERE edh.EndDate IS NULL
Highlight and Execute (except for last WHERE line)
EndDate
fields are NULL
WHERE
line)
SELECT emp.BusinessEntityID AS EmployeeID, contact.FirstName,
contact.Lastname, emp.JobTitle,
dept.DepartmentID, dept.Name
FROM HumanResources.Employee AS emp
INNER JOIN Person.Person AS contact
ON emp.BusinessEntityID = contact.BusinessEntityID
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
ON emp.BusinessEntityID = edh.BusinessEntityID
INNER JOIN HumanResources.Department AS dept
ON edh.DepartmentID = dept.DepartmentID
WHERE edh.EndDate IS NULL
ORDER BY emp.BusinessEntityID
Highlight and Execute
dept
name
dept
table
dept.Name
EmployeeDepartmentHistory
but not displaying any columns from that table
--The "other" way
SELECT emp.BusinessEntityID AS EmployeeID, contact.FirstName,
contact.LastName, emp.JobTitle,
dept.DepartmentID, dept.Name
FROM HumanResources.Employee AS emp,
Person.Person AS contact,
HumanResources.EmployeeDepartmentHistory AS edh,
HumanResources.Department AS dept
WHERE edh.EndDate IS NULL
AND emp.BusinessEntityID = contact.BusinessEntityID
AND emp.BusinessEntityID = edh.BusinessEntityID
AND edh.DepartmentID = dept.DepartmentID
ORDER BY emp.BusinessEntityID
Highlight and Execute
JOIN
conditions listed in the WHERE
clause
WHERE
conditions and JOIN
conditions are mixed together; would rather keep them separate for clarity
JOIN
this way, you don't have to have a WHERE
clause at all
JOIN
conditions
--What if I forget one of the join condtions?
SELECT emp.BusinessEntityID AS EmployeeID, contact.FirstName,
contact.LastName, emp.JobTitle,
dept.DepartmentID,dept.Name
FROM HumanResources.Employee AS emp,
Person.Person AS contact,
HumanResources.EmployeeDepartmentHistory AS edh,
HumanResources.Department AS dept
WHERE edh.EndDate IS NULL
AND emp.BusinessEntityID = contact.BusinessEntityID
AND edh.DepartmentID = dept.DepartmentID
ORDER BY emp.BusinessEntityID
Highlight and Execute
JOIN
s we'll talk about in later videos where you can't use this method at all, so better off sticking with the other way
JOIN
ing Tables Part 2Tools
OUTER JOIN
Sub-Query
FULL OUTER JOIN
CROSS JOIN
SQL Server
If you have a SQL Server available, download the sample OLTP database, AdventureWorks, from www.Codeplex.com
Figure out which version of SQL server you are running and download that specific version
You can start with anything from 2008 to 2012 to follow along
SQL Server free virtual labs
http://www.microsoft.com/sqlserver/en/us/learning-center/virtual-labs.aspx
Go back to first video to learn more on how to do this
--The list of customers with the sales order ID
SELECT c.CustomerID, c.AccountNumber, o.SalesOrderID
FROM Sales.Customer AS c
INNER JOIN Sales.SalesOrderHeader AS o
ON c.CustomerID = o.CustomerID
Highlight and Execute
SalesOrderHeader
without a customer
/*
Display all the customers with orders even
if no orders yet
Using LEFT OUTER JOIN
*/
SELECT c.CustomerID, c.AccountNumber, o.SalesOrderID
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS o
ON c.CustomerID = o.CustomerID
Highlight and Execute
OUTER
keyword is optional
SalesOrderHeader
table
NULL
values in the SalesOrderID
column
RIGHT OUTER JOIN
gets all the rows from the table on the right even if there's not a match
--Using LEFT OUTER JOIN
SELECT c.CustomerID, c.AccountNumber, o.SalesOrderID
FROM Sales.SalesOrderHeader AS o
RIGHT OUTER JOIN Sales.Customer AS c
ON c.CustomerID = o.CustomerID
Highlight and Execute
LEFT OUTER JOIN
because she likes to get the results from the most important table first
--Use a sub-query to find the customers
--Who have placed an order
SELECT CustomerID, AccountNumber
FROM Sales.Customer
WHERE CustomerID IN (SELECT CustomerID FROM Sales.SalesOrderHeader)
Highlight and Execute
()
and proceeded by the word IN
JOIN
SalesOrderHeader
table
/*
Number of orders: 31465
Number of customers: 19820 [NOTE: run the first two lines of code above to get this number]
Number of customers with orders: 19119
*/
Type in
--Finding the customers who haven't
--Placed an order
SELECT c.CustomerID, c.AccountNumber
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader AS o
ON c.CustomerID = o.CustomerID
WHERE o.CustomerID IS NULL
ORDER BY c.CustomerID
Highlight and Execute
--Step 2 get all rows from the left side
SELECT c.CustomerID, c.AccountNumber, o.SalesOrderID
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader o
ON c.CustomerID = o.CustomerID
ORDER BY c.CustomerID
Highlight and Execute
--Step 3 filter out rows that are a match
SELECT c.CustomerID, c.AccountNumber, o.SalesOrderID
FROM Sales.Customer AS c
LEFT OUTER JOIN Sales.SalesOrderHeader o
ON c.CustomerID = o.CustomerID
WHERE o.SalesOrderID IS NULL
ORDER BY c.CustomerID
Highlight and Execute
SalesOrderID
are NULL
, we can get rid of the last column if we want by removing o.SalesOrderID
from the first SELECT
query line in our code
--Use a sub-query
SELECT CustomerID, AccountNumber
FROM Sales.Customer
WHERE CustomerID NOT IN
(SELECT CustomerID FROM Sales.SalesOrderHeader)
Highlight and Execute
NOT
/*
FULL OUTER JOIN
Hard to find a reason to do this in a properly
designed database, so we'll create a table that doesn't belong
*/
IF OBJECT_ID('DepartmentValue') IS NOT NUL
DROP TABLE DepartmentValue
CREATE TABLE DepartmentValue(DepartmentID int,SomeValue varchar(50))
INSERT INTO DepartmentValue (DepartmentID, SomeValue)
VALUES (1, 'one'), (2, 'two'), (20, 'twenty')
Highlight and Execute
SELECT * FROM DepartmentValue
Highlight and Execute
--Do an INNER JOIN
SELECT d.DepartmentID, d.Name, dv.SomeValue
FROM HumanResources.Department AS d
INNER JOIN DepartmentValue AS dv
ON d.DepartmentID = dv.DepartmentID
Highlight and Execute
--Do an LEFT OUTER JOIN
SELECT d.DepartmentID, d.Name, dv.SomeValue
FROM HumanResources.Department AS d
LEFT OUTER JOIN DepartmentValue AS dv
ON d.DepartmentID = dv.DepartmentID
Highlight and Execute
--A RIGHT OUTER JOIN
SELECT d.DepartmentID, d.Name, dv.SomeValue, dv.DepartmentID
FROM HumanResources.Department AS d
RIGHT OUTER JOIN DepartmentValue AS dv
ON d.DepartmentID = dv.DepartmentID
Highlight and Execute
--FULL OUTER JOIN
SELECT d.DepartmentID, d.Name, dv.SomeValue, dv.DepartmentID
FROM HumanResources.Department AS d
FULL OUTER JOIN DepartmentValue AS dv
ON d.DepartmentID = dv.DepartmentID
Highlight and Execute
DepartmentValue
table
DepartmentValue
table that doesn't have a match in Department
JOIN
, I.E. a customer can have one to many orders.
CROSS JOIN
is pretty rare
JOIN
two tables that don't have a relationship
--Using CROSS JOIN to join two tables with no relationship
--This is a Cartesian Product!
--First, how many rows in the Location and Product tables?
SELECT * FROM Production.Location --14
SELECT * FROM Production.Product --504
Highlight and Execute
--This join is like multiplying the tables together
SELECT l.LocationID, l.Name, p.ProductID, p.Name as ProductName
FROM Production.Product AS p
CROSS JOIN Prodution.Location AS l
Highlight and Execute
SELECT 14 * 504
Highlight and Execute
Tools
Common Aggregate Functions
Syntax
Examples
SQL Server
If you have a SQL Server available, download the sample OLTP database, AdventureWorks, from www.Codeplex.com
Figure out which version of SQL server you are running and download that specific version
You can start with anything from 2008 to 2012 to follow along
SQL Server free virtual labs
http://www.microsoft.com/sqlserver/en/us/learning-center/virtual-labs.aspx
Go back to first video to learn more on how to do this
COUNT
MIN
MAX
SUM
AVG
Used to summarize our data or group data into sections
SELECT FUNCTION(col1)
FROM table1
SELECT FUNCTION(col1),col2
FROM table1
GROUP BY col2
--Find the count of the rows
SELECT COUNT(*) AS "Cost of Sales"
FROM Sales.SalesOrderHeader
Highlight and Execute
*
--Look at subTotal
SELECT COUNT(SubTotal) AS "Count of SubTotal",
SUM(SubTotal) AS "Sum of SubTotal",
MIN(SubTotal) AS "Min SubTotal",
MAX(SubTotal) AS "Max SubTotal",
AVG(SubTotal) AS "Avg SubTotal"
FROM Sales.SalesOrderHeader
Highlight and Execute
--Use an expression
SELECT SUM(SubTotal + TaxAmt + Freight) AS "Total Due"
FROM Sales.SalesOrderHeader
Highlight and Execute
--Nulls?
SELECT COUNT(*) AS "Cost of rows",
COUNT(SubTotal) AS "Count of SubTotal",
COUNT(CurrencyRateID) AS "Count of CurrencyRateID"
FROM Sales.SalesOrderHeader
Highlight and Execute
NULL
values
SELECT AVG(CurrencyRateID) AS "Avg CurrencyRateID",
SUM(CurrencyRateID)/COUNT(*) AS "Sum/Count of Rows"
FROM Sales.SalesOrderHeader
Highlight and Execute
NULL
values being ignored
--Display the count of orders for each customer
SELECT CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader
Highlight and Execute
--Add a GROUP BY clause
SELECT CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader
GROUP BY CustomerID
Highlight and Execute
--Order by the expression
SELECT CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader
GROUP BY CustomerID
ORDER BY COUNT() DESC
Highlight and Execute
--Get some more details about customer
SELECT o.CustomerID, COUNT(*) AS "Order Count",
c.AccountNumber
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY o.CustomerID, c.AccountNumber
Highlight and Execute
/* Any column that appears in the SELECT list or
ORDER BY clause that is not part of an aggregate
function MUST appear in the GROUP BY clause
*/
SELECT o.CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY o.CustomerID, c.AccountNumber
ORDER BY c.AccountNumber
Highlight and Execute
c.AccountNumber
and we get the earlier error again
--Don't include anything you don't need
SELECT o.CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY o.CustomerID, c.AccountNumber, o.SalesOrderID
ORDER BY c.AccountNumber
Highlight and Execute
o.SalesOrderID
GROUP BY
statement & then starts eliminating all of the aggregate expression
--Don't use an aggregate function in WHERE
SELECT o.CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
WHERE COUNT(*) > 5
GROUP BY o.CustomerID
Highlight and Execute
WHERE
clause
WHERE
clause is evaluated and processed before the aggregate is applied
--Use HAVING
SELECT o.CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY o.CustomerID
HAVING COUNT(*) > 5
Highlight and Execute
HAVING
filters out after aggregate queries have been applied
HAVING
statement
HAVING
clause
SELECT o.CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY o.CustomerID
HAVING SUM(SubTotal) > 10000
Highlight and Execute
--Compare to this
SELECT o.CustomerID, COUNT(*) AS "Order Count"
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
WHERE SubTotal > 10000
GROUP BY o.CustomerID
Highlight and Execute
--Let's get a count of orders by customer by year
SELECT c.CustomerID, COUNT(SalesOrderID) AS CountOfSales,
YEAR(o.OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY c.CustomerID
Highlight and Execute
--Let's get a count of orders by customer by year
SELECT c.CustomerID, COUNT(SalesOrderID) AS CountOfSales,
YEAR(o.OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY c.CustomerID, o.OrderDate
ORDER BY c.CustomerID, YEAR(o.OrderDate)
Highlight and Execute
--Add complete expression to GROUP BY
SELECT c.CustomerID, COUNT(SalesOrderID) AS CountOfSales,
YEAR(o.OrderDate) AS OrderYear
FROM Sales.SalesOrderHeader AS o
INNER JOIN Sales.Customer AS c
ON o.CustomerID = c.CustomerID
GROUP BY c.CustomerID, YEAR(o.OrderDate)
ORDER BY c.CustomerID, YEAR(o.OrderDate)
Highlight and Execute
--Aggregate of character data
SELECT MIN(LastName) AS "Min last name",
MAX(LastName) AS "Max last name"
FROM Person.Person
Highlight and Execute
SELECT AVG(LastName)
FROM Person.Person
Highlight and Execute