Example 1

advertisement
Best Practices
Transact-SQL
 Transact-SQL Syntax Elements

Batch Directives

System Functions

Comments

Operators

Identifiers

Expressions

Types of Data


Variables
Control-of-Flow
Language Elements

Reserved Keywords
Batch Directives

GO



Delineates batches of Transact-SQL statements to tools
and utilities
Is not an actual Transact-SQL statement
EXEC


Executes a user-defined function, system procedure,
user-defined stored procedure, or an extended stored
procedure
Controls the execution of a character string within a
Transact-SQL batch
Comments

In-line Comments
Example 1
SELECT productname
, (unitsinstock - unitsonorder) -- Calculates inventory
, supplierID
FROM products
GO

Block Comments
Example 3
/*
This code retrieves all rows of the products table
and displays the unit price, the unit price increased
by 10 percent, and the name of the product.
*/
USE northwind
SELECT unitprice, (unitprice * 1.1), productname
FROM products
GO
 Identifiers

Standard Identifiers


Other characters can include letters, numerals, or
symbols
Identifiers starting with symbols have special uses
Delimited Identifiers
 Use when names contain embedded spaces
 Use when reserved words are portions of names
 Enclose in brackets ([ ]) or quotation marks (" ")


First character must be alphabetic
Naming Guidelines for Identifiers

Keep Names Short

Use Meaningful Names Where Possible

Use Clear and Simple Naming Conventions

Use an Identifier That Distinguishes Types of Object


Views

Stored procedures
Keep Object Names and User Names Unique

Sales table and sales role
Types of Data

Numbers

SQL Variants

Dates

Image and Text

Characters

Table

Binary

Cursor

Unique Identifiers (GUID)

User-defined
Variables

User-defined with DECLARE @ Statement

Assigned Values with SET or SELECT @ Statement

Variables Have Local Scope
USE northwind
DECLARE @EmpID varchar(11)
,@vlName char(20)
SET @vlname = 'Dodsworth'
SELECT @EmpID = employeeid
FROM employees
WHERE LastName = @vlname
SELECT @EmpID AS EmployeeID
GO
 System Functions

Aggregate Functions
USE northwind
SELECT AVG (unitprice) AS AvgPrice FROM products
GO

Scalar Functions
USE northwind
SELECT DB_NAME() AS 'database‘
GO

Rowset Functions
SELECT *
FROM OPENQUERY
(OracleSvr, 'SELECT name, id FROM owner.titles')
System Function Examples
Example 1
SELECT 'ANSI:', CONVERT(varchar(30), GETDATE(), 102) AS
Style
UNION
SELECT 'Japanese:', CONVERT(varchar(30), GETDATE(), 111)
UNION
SELECT 'European:', CONVERT(varchar(30), GETDATE(), 113)
GO
Result
Style
ANSI:
1998.03.19
Japanese:
1998/03/19
European:
19 Mar 1998 16:34:40:616
Operators


Types of Operators

Arithmetic

Comparison

String concatenation

Logical
Operator Precedence Levels
Expressions

Combination of Symbols and Operators

Evaluation to Single Scalar Value

Result Data Type Dependent on the Elements
Within the Expression
USE
SELECT
northwind
OrderID, ProductID
,(UnitPrice * Quantity) as ExtendedAmount
FROM [Order Details]
WHERE (UnitPrice * Quantity) > 10000
GO
Control-of-Flow Language Elements
Example 2


Statement Level

BEGIN … END block

IF … ELSE block

WHILE constructs
Row Level

CASE function
DECLARE @n tinyint
SET @n = 5
IF (@n BETWEEN 4 and 6)
BEGIN
WHILE (@n > 0)
BEGIN
SELECT @n AS 'Number'
,CASE
WHEN (@n % 2) = 1
THEN 'EVEN'
ELSE 'ODD'
END AS 'Type'
SET @n = @n - 1
END
END
ELSE
PRINT 'NO ANALYSIS‘
GO
Reserved Keywords


Identifier Names That Have Special Meaning

Transact-SQL keywords

ANSI SQL-92 keywords

ODBC reserved keywords
Do Not Use Reserved Keywords for Identifier Names
 Retrieving Data by Using the SELECT Statement

Using the SELECT Statement

Specifying Columns

Using the WHERE Clause to Specify Rows
Using the SELECT Statement

Select List Specifies the Columns

WHERE Clause Specifies the Condition Restricting the
Query

FROM Clause Specifies the Table
Partial Syntax
SELECT [ALL | DISTINCT] <select_list>
FROM {<table_source>} [,…n]
WHERE <search_condition>
Specifying Columns
USE northwind
SELECT employeeid, lastname, firstname, title
FROM employees
GO
employeeid
lastname
firstname
title
1
Davolio
Nancy
Sales Representative
2
Fuller
Andrew
Vice President, Sales
3
Leverling
Janet
Sales Representative
4
Peacock
Margaret
Sales Representative
5
Buchanan
Steven
Sales Manager
6
Suyama
Michael
Sales Representative
7
King
Robert
Sales Representative
8
Callahan
Laura
Inside Sales Coordinator
9
Dodsworth
Anne
Sales Representative
Using the WHERE Clause to Specify Rows
USE northwind
SELECT employeeid, lastname, firstname, title
FROM employees
WHERE employeeid = 5
GO
employeeid
5
lastname
Buchanan
firstname
Steven
title
Sales Manager
 Filtering Data

Using Comparison Operators

Using String Comparisons

Using Logical Operators

Retrieving a Range of Values

Using a List of Values as Search Criteria

Retrieving Unknown Values
Using Comparison Operators
Example 1
USE northwind
SELECT lastname, city
FROM employees
WHERE country = 'USA'
GO
lastname
Davolio
Fuller
Leverling
Peacock
Callahan
city
Seattle
Tacoma
Kirkland
Redmond
Seattle
Using String Comparisons
USE northwind
SELECT companyname
FROM customers
WHERE companyname LIKE '%Restaurant%'
GO
companyname
GROSELLA-Restaurante
Lonesome Pine Restaurant
Tortuga Restaurante
Using Logical Operators
Example 1
USE northwind
SELECT productid, productname, supplierid, unitprice
FROM products
WHERE (productname LIKE 'T%' OR productid = 46)
AND (unitprice > 16.00)
GO
productid
14
29
62
productname
Tofu
Thüringer Rostbratwurst
Tarte au sucre
supplierid unitprice
6
23.25
12
123.79
29
49.3
Retrieving a Range of Values
USE northwind
SELECT productname, unitprice
FROM products
WHERE unitprice BETWEEN 10 AND 20
GO
productname
Chai
Chang
Aniseed Syrup
Genen Shouyu
Pavlova
Sir Rodney’s Scones
…
unitprice
18
19
10
15.5
17.45
10
…
Example 1
Using a List of Values as Search Criteria
Example 1
USE northwind
SELECT companyname, country
FROM suppliers
WHERE country IN ('Japan', 'Italy')
GO
companyname
Tokyo Traders
Mayumi’s
Formaggi Fortini s.r.l.
Pasta Buttini s.r.l.
country
Japan
Japan
Italy
Italy
Retrieving Unknown Values
USE northwind
SELECT companyname, fax
FROM suppliers
WHERE fax IS NULL
GO
companyname
Exotic Liquids
New Orleans Cajun Delights
Tokyo Traders
Cooperativa de Quesos ‘Las Cabras’
…
fax
NULL
NULL
NULL
NULL
…
 Formatting Result Sets

Sorting Data

Eliminating Duplicate Rows

Changing Column Names

Using Literals
Sorting Data
Example 1
USE northwind
SELECT productid, productname, categoryid, unitprice
FROM products
ORDER BY categoryid, unitprice DESC
GO
productid
38
43
2
…
63
8
61
…
productname
Cote de Blaye
Ipoh Coffee
Chang
…
Vegie-spread
Northwoods Cranberry Sauce
Sirop d'érable
…
categoryid
1
1
1
…
2
2
2
…
unitprice
263.5000
46.0000
19.0000
…
43.9000
40.0000
28.5000
…
Eliminating Duplicate Rows
Example 1
USE northwind
SELECT DISTINCT country
FROM suppliers
ORDER BY country
GO
country
Australia
Brazil
Canada
Denmark
Finland
France
Germany
Italy
Japan
Netherlands
Norway
Singapore
Spain
Sweden
UK
USA
Changing Column Names
USE northwind
SELECT firstname AS First, lastname AS Last
,employeeid AS 'Employee ID:'
FROM employees
GO
First
Nancy
Andrew
Janet
Margaret
Steven
Michael
Robert
Laura
Anne
Last
Davolio
Fuller
Leverling
Peacock
Buchanan
Suyama
King
Callahan
Dodsworth
Employee ID:
1
2
3
4
5
6
7
8
9
Using Literals
USE northwind
SELECT firstname, lastname
,'Identification number:', employeeid
FROM employees
GO
First
Nancy
Andrew
Janet
Margaret
Steven
Michael
Robert
Laura
Anne
Last
Davolio
Fuller
Leverling
Peacock
Buchanan
Suyama
King
Callahan
Dodsworth
Employee ID:
Identification Number: 1
Identification Number: 2
Identification Number: 3
Identification Number: 4
Identification Number: 5
Identification Number: 6
Identification Number: 7
Identification Number: 8
Identification Number: 9
 How Queries Are Processed
Uncached Queries (Ad Hoc)
Parse
Resolve
Optimize
Compile
Execute
Optimize
Compile
Execute
Procedure
Cache
Execute
Cached Queries
First Execution
Parse
Resolve
Subsequent Execution
Performance Considerations

Not Search Conditions May Slow Data Retrieval

LIKE Search Conditions Slow Data Retrieval

Exact Matches or Ranges May Speed Data Retrieval

ORDER BY Clause May Slow Data Retrieval
Listing the TOP n Values

Lists Only the First n Rows of a Result Set

Specifies the Range of Values in the ORDER BY Clause

Returns Ties if WITH TIES Is Used
Example 1
USE northwind
SELECT TOP 5 orderid, productid, quantity
FROM [order details]
ORDER BY quantity DESC
GO
Example 2
USE northwind
SELECT TOP 5 WITH TIES orderid, productid, quantity
FROM [order details]
ORDER BY quantity DESC
GO
 Using Aggregate Functions
Aggregate function
Description
AVG
Average of values in a numeric expression
COUNT
Number of values in an expression
COUNT (*)
Number of selected rows
MAX
Highest value in the expression
MIN
Lowest value in the expression
SUM
Total values in a numeric expression
STDEV
Statistical deviation of all values
STDEVP
Statistical deviation for the population
VAR
Statistical variance of all values
VARP
Statistical variance of all values for the population
Using Aggregate Functions with Null Values

Most Aggregate Functions Ignore Null Values

COUNT(*) Function Counts Rows with Null Values
USE northwind
SELECT COUNT (*)
FROM employees
GO
Example 1
USE northwind
SELECT COUNT(reportsto)
FROM employees
GO
Example 2
 GROUP BY Fundamentals

Using the GROUP BY Clause

Using the GROUP BY Clause with the HAVING Clause
Using the GROUP BY Clause
USE northwind
SELECT productid, orderid
,quantity
FROM orderhist
GO
productid
USE northwind
SELECT productid
,SUM(quantity) AS total_quantity
FROM orderhist
GROUP BY productid
GO
orderid quantity
productid
total_quantity
1
1
5
1
15
1
1
10
2
35
2
1
10
3
45
2
2
25
3
1
15
productid
total_quantity
3
2
30
2
35
Only rows that
satisfy the WHERE
clause are grouped
USE northwind
SELECT productid
,SUM(quantity) AS total_quantity
FROM orderhist
WHERE productid = 2
GROUP BY productid
GO
Using the GROUP BY Clause with the HAVING Clause
USE northwind
SELECT productid, orderid
,quantity
FROM orderhist
GO
productid
orderid quantity
USE northwind
SELECT productid, SUM(quantity)
AS total_quantity
FROM orderhist
GROUP BY productid
HAVING SUM(quantity)>=30
GO
1
1
5
1
1
10
productid
total_quantity
2
1
10
2
35
2
2
25
3
45
3
1
15
3
2
30
 Generating Aggregate Values Within Result Sets

Using the GROUP BY Clause with the ROLLUP Operator

Using the GROUP BY Clause with the CUBE Operator

Using the GROUPING Function
Using the GROUP BY Clause with the ROLLUP Operator
USE northwind
SELECT productid, orderid, SUM(quantity) AS total_quantity
FROM orderhist
GROUP BY productid, orderid
WITH ROLLUP
ORDER BY productid, orderid
GO
productid orderid total_quantity
Description
NULL
NULL
95
Grand total
1
NULL
15
Summarizes only rows for productid 1
1
1
5
1
2
10
2
NULL
35
2
1
10
2
2
25
Detail value for productid 2, orderid 1
3
NULL
45
Summarizes only rows for productid 3
3
1
15
Detail value for productid 3, orderid 1
3
2
30
Detail value for productid 3, orderid 2
Detail value for productid 1, orderid 1
Detail value for productid 1, orderid 2
Summarizes only rows for productid 2
Using the GROUP BY Clause with the CUBE Operator
USE northwind
SELECT productid, orderid, SUM(quantity) AS total_quantity
FROM orderhist
GROUP BY productid, orderid
WITH CUBE
ORDER BY productid, orderid
GO
productid orderid total_quantity
The CUBE operator
produces two
more summary
values than the
ROLLUP operator
Description
NULL
NULL
95
Grand total
NULL
1
30
Summarizes all rows for orderid 1
NULL
2
65
Summarizes all rows for orderid 2
1
NULL
15
Summarizes only rows for productid 1
1
1
5
Detail value for productid 1, orderid 1
1
2
10
Detail value for productid 1, orderid 2
2
NULL
35
Summarizes only rows for productid 2
2
1
10
Detail value for productid 2, orderid 1
2
2
25
Detail value for productid 2, orderid 2
3
NULL
45
Summarizes only rows for productid 3
3
1
15
Detail value for productid 3, orderid 1
3
2
30
Detail value for productid 3, orderid 2
Using the GROUPING Function
SELECT productid, GROUPING (productid)
,orderid, GROUPING (orderid)
,SUM(quantity) AS total_quantity
FROM orderhist
GROUP BY productid, orderid
WITH CUBE
ORDER BY productid, orderid
GO
productid
1 represents summary values
in the preceding column
0 represents detail values in
the preceding column
NULL
NULL
NULL
1
1
1
2
2
2
3
3
3
1
1
1
0
0
0
0
0
0
0
0
0
orderid
NULL
1
2
NULL
1
2
NULL
1
2
NULL
1
2
total_quantity
1
0
0
1
0
0
1
0
0
1
0
0
95
30
65
15
5
10
35
10
25
45
15
30
Using the COMPUTE and COMPUTE BY Clauses
COMPUTE
COMPUTE BY
USE northwind
SELECT productid, orderid
,quantity
FROM orderhist
ORDER BY productid, orderid
COMPUTE SUM(quantity)
GO
productid orderid quantity
USE northwind
SELECT productid, orderid, quantity
FROM orderhist
ORDER BY productid, orderid
COMPUTE SUM(quantity) BY productid
COMPUTE SUM(quantity)
GO
productid orderid quantity
1
1
5
1
2
10
sum
15
1
1
5
1
2
10
2
1
10
2
1
10
2
2
25
2
2
25
3
1
15
sum
35
3
2
30
3
1
15
sum
95
3
2
30
sum
45
sum
95
Using Aliases for Table Names

Example 1 (without an alias name)
USE joindb
SELECT buyer_name, sales.buyer_id, qty
FROM buyers INNER JOIN sales
ON buyers.buyer_id = sales.buyer_id
GO

Example 2 (with an alias name)
USE joindb
SELECT buyer_name, s.buyer_id, qty
FROM buyers AS b INNER JOIN sales AS s
ON b.buyer_id = s.buyer_id
GO
Download