Introduction To T-SQL Querying

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Module 1

Introduction to T-SQL Querying


Module Overview

• Introducing T-SQL
• Understanding the Logical Order of Operations in
SELECT Statements
Lesson 1: Introducing T-SQL

• About T-SQL
• Categories of T-SQL Statements
• T-SQL Language Elements
• T-SQL Language Elements: Predicates and Operators
• T-SQL Language Elements: Functions
• T-SQL Language Elements: Variables
• T-SQL Language Elements: Expressions
• T-SQL Language Elements: Control of Flow, Errors, and
Transactions
• T-SQL Language Elements: Comments
• T-SQL Language Elements: Batch Separators
• Demonstration: T-SQL Language Elements
About T-SQL

• Structured Query Language (SQL)


• Developed by IBM in the 1970s
• Adopted by ANSI and ISO standards bodies
• Widely used in the industry
• PL/SQL (Oracle), SQL Procedural Language (IBM),
Transact-SQL (Microsoft)
• Transact-SQL is commonly referred to as T-SQL
• The querying language of SQL Server 2016

• SQL is declarative
• Describe what you want, not the individual steps
Categories of T-SQL Statements

DML* DDL DCL

• Data Manipulation • Data Definition • Data Control


Language Language Language

• Used to query and • Used to define • Used to manage


manipulate data database objects security
permissions
• SELECT, INSERT, • CREATE, ALTER,
UPDATE, DELETE DROP • GRANT, REVOKE,
DENY

*DML with SELECT is the focus of this course


T-SQL Language Elements

• Predicates and Operators


• Functions
• Variables
• Expressions
• Batch Separators
• Control of Flow
• Comments
T-SQL Language Elements: Predicates and
Operators

Elements: Predicates and Operators:

ALL, ANY, BETWEEN, IN, LIKE,


Predicates
OR, SOME
=, >, <, >=, <=, <>, !=, !>,
Comparison Operators
!<

Logical Operators AND, OR, NOT

Arithmetic Operators *, /, %, +, -,

Concatenation +
T-SQL Language Elements: Functions

String Date and Time Aggregate


Functions Functions Functions

• SUBSTRING • GETDATE • SUM


• LEFT, RIGHT • SYSDATETIME • MIN
• LEN • GETUTCDATE • MAX
• REPLACE • DATEADD • AVG
• REPLICATE • DATEDIFF • COUNT
• UPPER, LOWER • YEAR • COUNT_BIG
• LTRIM, RTRIM • MONTH • STDEV
• STUFF • DAY • STDEVP
• SOUNDEX • DATENAME • VAR
• DATEPART
• ISDATE
T-SQL Language Elements: Variables

• Local variables in T-SQL temporarily store a value


of a specific data type
• Name begins with single @ sign
• @@ reserved for system functions
• Assigned a data type
• Must be declared and used within the same
batch
• In SQL Server 2016, you can declare and initialize
a variable in the same statement
DECLARE @search varchar(30) = 'Match%';
T-SQL Language Elements: Expressions

• Combination of identifiers, values, and operators


evaluated to obtain a single result
• Can be used in SELECT statements
• SELECT clause
• WHERE clause

• Can be single constant, single-valued function, or


variable
• Can be combined if expressions have the same
data type
SELECT YEAR(orderdate) + 1 ...
SELECT qty * unitprice ...
T-SQL Language Elements: Control of Flow,
Errors, and Transactions

Control of Flow Error Handling Transaction Control

• IF … ELSE • TRY • BEGIN


• WHILE • CATCH TRANSACTION
• BREAK • THROW • ROLLBACK
• CONTINUE TRANSACTION
• BEGIN … END • COMMIT
• WAITFOR TRANSACTION
• ROLLBACK WORK
• SAVE
TRANSACTION

The above are used in programmatic code objects


T-SQL Language Elements: Comments

• Two methods for marking text as comments


• A block comment, surround text with /* and */
/*
All the text in this paragraph will be treated as
comments by SQL Server.
*/

• An inline comment, precede text with --


-- This is an inline comment

• Many T-SQL editors will color comments as above


T-SQL Language Elements: Batch Separators

• Batches are sets of commands sent to SQL 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] executes the preceding batch [count] times
Demonstration: T-SQL Language Elements
Demonstration: T-SQL Language Elements
Lesson 3: Understanding the Logical Order of
Operations in SELECT Statements

• Elements of a SELECT Statement


• Logical Query Processing
• Applying the Logical Order of Operations to
Writing SELECT Statements
• Demonstration: Logical Query Processing
Elements of a SELECT Statement

Element Expression Role

SELECT <select list> Defines which columns to return

FROM <table source> Defines table(s) to query

Filters returned data using a


WHERE <search condition>
predicate

GROUP BY <group by list> Arranges rows by groups

HAVING <search condition> Filters groups by a predicate

ORDER BY <order by list> Sorts the results


Logical Query Processing

5. SELECT <select list>


1. FROM <table source>
2. WHERE <search condition>
3. GROUP BY <group by list>
4. HAVING <search condition>
6. ORDER BY <order by list>

The order in which a query is written is not the order in which it is


evaluated by SQL Server
Applying the Logical Order of Operations to
Writing SELECT Statements

USE TSQL;

SELECT EmployeeId, YEAR(OrderDate) AS OrderYear


FROM Sales.Orders
WHERE CustomerId = 71
GROUP BY EmployeeId, YEAR(OrderDate)
HAVING COUNT(*) > 1
ORDER BY EmployeeId, OrderYear;
Demonstration: Logical Query Processing
Demonstration: Logical Query Processing

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy