Beruflich Dokumente
Kultur Dokumente
Master subtitle
style
Course Topics
Querying Microsoft SQL Server 2012 Jump Start
01 | Introducing SQL Server 2012
SQL Server types of statements; other SQL statement elements; basic SELECT statements
| Lunch Break
Eat, drink, and recharge for the afternoon session
Module Overview
Advanced SELECT
Clauses
Understanding DISTINCT
Specifies that only unique rows can appear in the result
set
Removes duplicates based on column list results, not
source table
Provides uniqueness across set of selected columns
Removes rows already operated on by WHERE, HAVING,
and GROUP BY clauses
Some queries may improve performance by filtering out
duplicates prior to execution of SELECT clause
SELECT
SELECT SalesOrderID,
SalesOrderID, UnitPrice,
UnitPrice, OrderQty
OrderQty AS
AS Quantity
Quantity
FROM
FROM
Sales.SalesOrderDetail;
Sales.SalesOrderDetail;
SELECT SalesOrderID,
SalesOrderID, UnitPrice, Quantity
Quantity =
=
OrderQty
OrderQty
FROM
FROM
Sales.SalesOrderDetail;
Sales.SalesOrderDetail;
SELECT
SELECT SalesOrderID,
SalesOrderID, UnitPrice
UnitPrice Quantity
Quantity
FROM
FROM
Sales.SalesOrderDetail;
Sales.SalesOrderDetail;
SELECT
SELECT SalesOrderID,
SalesOrderID, ProductID
ProductID
FROM
FROM Sales.SalesOrderDetail
Sales.SalesOrderDetail AS
AS SalesOrders;
SalesOrders;
Table aliases without AS
SELECT
SELECT SalesOrderID,
SalesOrderID, ProductID
ProductID
FROM
FROM Sales.SalesOrderDetail
Sales.SalesOrderDetail SalesOrders;
SalesOrders;
Using table aliases in the SELECT clause
SELECT
SELECT SalesOrders.SalesOrderID,
SalesOrders.SalesOrderID,
SalesOrders.ProductID
SalesOrders.ProductID
FROM
FROM Sales.SalesOrderDetail
Sales.SalesOrderDetail AS
AS SalesOrders;
SalesOrders;
Expression component
SELECT
<select list>
CASE
<value to compare>
WHEN
<value to match>
THEN
<result>
END
FROM
N/A
<table source>
SELECT
SELECT ProductID,
ProductID, Name,
Name, ProductSubCategoryID,
ProductSubCategoryID,
CASE
CASE ProductSubCategoryID
ProductSubCategoryID
WHEN
WHEN 1
1 THEN
THEN 'Beverages'
'Beverages'
ELSE
ELSE 'Unknown
'Unknown Category'
Category'
END
END
FROM
FROM Production.Product
Production.Product
Demo
Using basic SELECT clauses
JOIN Statements
Description
Cross
Inner
Outer
SELECT
SELECT CUST.CustomerID,
CUST.CustomerID, CUST.StoreID,
CUST.StoreID,
ORD.SalesOrderID,
ORD.SalesOrderID, ORD.OrderDate
ORD.OrderDate
FROM
FROM Sales.Customer
Sales.Customer AS
AS CUST
LEFT
LEFT OUTER
OUTER JOIN
JOIN Sales.SalesOrderHeader
Sales.SalesOrderHeader AS
AS
ORD
ORD
ON
ON CUST.CustomerID
CUST.CustomerID =
= ORD.CustomerID
ORD.CustomerID
WHERE
WHERE ORD.SalesOrderID IS
IS NULL;
NULL;
SELECT
SELECT EMP1.BusinessEntityID,
EMP1.BusinessEntityID, EMP2.JobTitle
EMP2.JobTitle
FROM
FROM HumanResources.Employee AS
AS EMP1
EMP1
CROSS
CROSS JOIN
JOIN HumanResources.Employee
HumanResources.Employee AS
AS EMP2;
EMP2;
Understanding Self-Joins
Why use self-joins?
Compare rows in same table to each other
Create two instances of same table in FROM clause
At least one alias required
Example: Return all employees and
the name of the employees manager
Self-Join examples
Return all employees with ID of employees manager when a
manager exists (INNER JOIN):
SELECT
EMP.EmpID,
EMP.EmpID, EMP.LastName,
EMP.LastName,
EMP.JobTitle,
EMP.JobTitle, EMP.MgrID,
EMP.MgrID, MGR.LastName
MGR.LastName
FROM
HR.Employees
HR.Employees AS
AS EMP
EMP
INNER JOIN HR.Employees
HR.Employees AS
AS MGR
MGR
ON EMP.MgrID
EMP.MgrID =
= MGR.EmpID
MGR.EmpID ;
Return all employees with ID of manager (OUTER JOIN). This will
return NULL for the CEO:
SELECT
SELECT EMP.EmpID,
EMP.EmpID, EMP.LastName,
EMP.LastName,
EMP.Title,
EMP.Title, MGR.MgrID
MGR.MgrID
FROM
FROM HumanResources.Employee
HumanResources.Employee AS
AS EMP
EMP
LEFT
LEFT OUTER
OUTER JOIN
JOIN HumanResources.Employee
HumanResources.Employee AS
AS
MGR
MGR
ON
ON EMP.MgrID
EMP.MgrID =
= MGR.EmpID;
Demo
Using JOINS to view data from
multiple tables
SELECT
SELECT SalesOrderID,
SalesOrderID, CustomerID,
CustomerID, OrderDate
OrderDate
FROM
FROM Sales.SalesOrderHeader
Sales.SalesOrderHeader
ORDER
ORDER BY
BY OrderDate;
OrderDate;
ORDER BY with column alias:
SELECT
SELECT SalesOrderID,
SalesOrderID, CustomerID,
CustomerID,
YEAR(OrderDate)
YEAR(OrderDate) AS
AS OrderYear
OrderYear
FROM
FROM Sales.SalesOrderHeader
Sales.SalesOrderHeader
ORDER
ORDER BY
BY OrderYear;
OrderYear;
ORDER BY with descending order:
SELECT
SELECT SalesOrderID,
SalesOrderID, CustomerID,
CustomerID, OrderDate
OrderDate
FROM
FROM Sales.SalesOrderHeader
Sales.SalesOrderHeader
ORDER
ORDER BY
BY OrderDate
OrderDate DESC;
DESC;
SELECT
SELECT CustomerID,
CustomerID, TerritoryID
FROM
FROM Sales.Customer
Sales.Customer
WHERE
WHERE TerritoryID
TerritoryID =
= 6;
6;
Filter rows for orders in territories greater than or equal to 6
SELECT CustomerID,
CustomerID, TerritoryID
FROM Sales.Customer
Sales.Customer
WHERE
WHERE TerritoryID
TerritoryID >= 6;
Filter orders within a range of dates
SELECT CustomerID,
CustomerID, TerritoryID, StoreID
FROM Sales.Customer
Sales.Customer
WHERE
WHERE StoreID
StoreID >=
>= 1000
1000 AND
AND StoreID
StoreID <=
<= 1200;
1200;
SELECT TOP
TOP (20)
(20) SalesOrderID,
SalesOrderID, CustomerID,
CustomerID,
TotalDue
FROM
FROM Sales.SalesOrderHeader
ORDER
ORDER BY TotalDue
TotalDue DESC;
DESC;
Filter rows for customers to display top 20 TotalDue items with
ties
SELECT TOP
TOP (20)
(20) WITH
WITH TIES
TIES SalesOrderID,
SalesOrderID,
CustomerID, TotalDue
TotalDue
FROM
FROM Sales.SalesOrderHeader
ORDER
ORDER BY TotalDue
TotalDue DESC;
DESC;
SELECT
SELECT TOP
TOP (1)
(1) PERCENT
PERCENT SalesOrderID,
SalesOrderID, CustomerID,
CustomerID,
TotalDue
TotalDue
FROM
FROM Sales.SalesOrderHeader
ORDER
ORDER BY
BY TotalDue
TotalDue DESC;
DESC;
Demo
Sorting and filtering data
Summary
The SELECT statement requires columns specified (* all
columns) and the FROM clause to identify what view or
table the rows of data are being pulled from
Clauses like DISTINCT provide control over what items are
returned in the result set.
Aliases are used to define the names of columns or tables
being referenced in the SELECT statement
CASE statements are used for performing comparisons in a
list of values and returns first match
Summary
JOINS are used to display content from multiple tables in a
single result set. INNER, OUTER, CROSS, and SELF JOINS
can all be used to create the desire result set
ORDER BY is used to sort the rows returned in the result
set
WHERE is used to filter the rows returned in the result set
The TOP clause is used to define the number of rows
returned in the result set by specifying the number of rows
or a percentage of rows returned.
2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are
or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes
only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to
changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the
accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR
STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.