Documente Academic
Documente Profesional
Documente Cultură
Module Overview
Introducing T-SQL
Understanding Sets
Understanding Predicate Logic
Understanding the Logical Order of
About T-SQL
Structured Query Language (SQL)
Developed by IBM in 1970s
Adopted as a standard by ANSI and ISO standards
bodies
Widely used in industry
Statements
for querying
and modifying
data
Data Definition
Language (DDL)
Data Control
Language (DCL)
Statements for
object
definitions
Statements for
security
permissions
SELECT,
INSERT,
UPDATE,
DELETE
* DML with SELECT is the focus
of
this course
Predicates and
Operators:
Predicates
IN, BETWEEN,
LIKE
Comparison
Operators
Logical Operators
Arithmetic
Operators
+, -, *, /, %
Concatenation
String Functions
SUBSTRING
LEFT, RIGHT
LEN
DATALENGTH
REPLACE
REPLICATE
UPPER, LOWER
RTRIM, LTRIM
GETDATE
SYSTDATETIME
GETUTCDATE
DATEADD
DATEDIFF
YEAR
MONTH
DAY
Aggregate
Functions
SUM
MIN
MAX
AVG
COUNT
same batch
In SQL Server 2008 and later, you can
declare and initialize in the same
statement
DECLARE @MyVar
@MyVar int = 30;
30;
Control of Flow
IF...ELSE
WHILE
BREAK
CONTINUE
BEGIN...END
Error Handling
TRY...CATCH
TRY...CATCH
Transaction Control
BEGIN
TRANSACTION
COMMIT
TRANSACTION
ROLLBACK
TRANSACTION
For inline text, precede the comments with --- This line of text will be ignored
Server as a unit
Batches determine variable scope, name
resolution
To separate statements into batches, use a
separator:
SQL Server tools use the GO keyword
GO is not an SQL Server T-SQL command!
GO [count] times feature added in SQL Server
2005
Characteristics of Set
Element
Example
Customer as member
of set Customers
Customer ID
Comments
Avoid cursor-based
processing
Queries
Expression
<select list>
FROM
<table source>
WHERE
<search
condition>
Role
Defines which columns
to return
Defines table(s) to
query
Filters rows using a
predicate
<select
<select list>
list>
<table source>
<search
<search condition>
condition>
3:
3: GROUP
GROUP BY
BY <group
<group by
by list>
list>
4:
4: HAVING
HAVING <search
<search condition>
condition>
6:
6: ORDER
ORDER BY
BY <order by
by list>
list>
USE
USE TSQL;
TSQL;
SELECT
SELECT empid,
empid, YEAR(orderdate)
YEAR(orderdate) AS orderyear
orderyear
FROM
FROM Sales.Orders
Sales.Orders
WHERE
WHERE custid
custid =71
=71
GROUP
GROUP BY
BY empid,
empid, YEAR(orderdate)
YEAR(orderdate)
HAVING
HAVING COUNT(*)
COUNT(*) >
>1
ORDER
ORDER BY
BY empid,
empid, orderyear;
orderyear;
Lab Scenario
You are a business analyst for Adventure
Work, who will be writing reports against
corporate databases stored in SQL Server
2014. For you to become more comfortable
with SQL Server querying, the Adventure
Works IT department has provided you with
a selection of common queries to run
against their databases. You will review and
execute these queries.