You are on page 1of 28

MANAGE DATA WITH TRANSACT-SQL

 Create Transact-SQL SELECT queries


 Query multiple tables by using joins
 Implement functions and aggregate data
 Modify Data

15 November 2022 Dave Jong-A-Lock 1


CREATE TRANSACT-SQL SELECT QUERIES

 Foundations of T-SQL
 Logical query processing
 The SELECT statement
 Filter data with predicates
 Sorting data
 Filtering data with TOP and OFFSET-FETCH
 Combining sets with set operators

15 November 2022 Dave Jong-A-Lock 2


FOUNDATIONS OF T-SQL

 Evolution
 Using T-SQL in a relational way
 Using correct terminology

15 November 2022 Dave Jong-A-Lock 3


USING T-SQL IN A RELATIONAL WAY

 T-SQL deviates from the relational model


 Relation has a heading and a body (considered as a whole)
 Heading is a set of attributes (columns)
 Body is a set of tuples (rows)
 Write better code
 Interact with the table as a whole
 Avoid iterative constructs (cursors or loops)

15 November 2022 Dave Jong-A-Lock 4


USING T-SQL IN A RELATIONAL WAY (CONT.)

 A set has no duplicates


 T-SQL doesn’t always enforces rules
 Result could have duplicates

15 November 2022 Dave Jong-A-Lock 5


USING CORRECT TERMINOLOGY

 field -> column


 Record -> row
 “NULL values”

15 November 2022 Dave Jong-A-Lock 6


CREATE DATABASES

 Execute statements from file


 Query database

15 November 2022 Dave Jong-A-Lock 7


LOGICAL QUERY PROCESSING

 Declarative English-like language


 Logical query processing phases

15 November 2022 Dave Jong-A-Lock 8


LOGICAL QUERY PROCESSING PHASES

keyed-in order Processing order

1. SELECT 1. FROM
2. FROM 2. WHERE
3. WHERE 3. GROUP BY
4. GROUP BY 4. HAVING
5. HAVING 5. SELECT
6. ORDER BY 6. ORDER BY

15 November 2022 Dave Jong-A-Lock 9


THE SELECT STATEMENT

 From clause
 Select clause

15 November 2022 Dave Jong-A-Lock 10


FILTER DATA WITH PREDICATES

 Predicates and three valued logic


 Combining predicates -> and, or and not
 NOT precedes AND + OR
 AND precedes OR
 Filtering character data
 Filtering date and time data

15 November 2022 Dave Jong-A-Lock 11


SORTING DATA

 Data will be returned unsorted without a sort defined


 Data will be returned as a whole (relational)
 Data will be returned in the order MS SQL Server reads the records

15 November 2022 Dave Jong-A-Lock 12


FILTERING DATA WITH TOP AND OFFSET-FETCH

 Top (amount or percentage)


 OFFSET-FETCH

15 November 2022 Dave Jong-A-Lock 13


COMBINING SETS WITH SET OPERATORS

 Union / Union all


 Intersect
 Except

15 November 2022 Dave Jong-A-Lock 14


QUERY MULTIPLE TABLES BY USING JOINS

 Cross joins
 Inner joins
 Outer joins
 Queries with composite joins and NULLs join columns
 Multi-join queries

15 November 2022 Dave Jong-A-Lock 15


CROSS JOIN

15 November 2022 Dave Jong-A-Lock 16


JOIN

15 November 2022 Dave Jong-A-Lock 17


IMPLEMENT FUNCTIONS AND AGGREGATE DATA

 Type conversion functions


 Date and time functions
 Character functions
 CASE expressions and related functions
 System functions
 Arithmetic operators and aggregate functions
 Search arguments
 Function determinism

15 November 2022 Dave Jong-A-Lock 18


MODIFY DATA

 Inserting data
 Updating data
 Deleting data
 Merging data
 Using the OUTPUT option
 Impact of structural changes on data

15 November 2022 Dave Jong-A-Lock 19


QUERY DATA WITH TSQL COMPONENTS

 Query data using subqueries and apply


 Query data by using table expressions
 Group and pivot data by using queries

15 November 2022 Dave Jong-A-Lock 20


QUERY DATA USING SUBQUERIES AND APPLY

 Subqueries
 Apply

15 November 2022 Dave Jong-A-Lock 21


QUERY DATA BY USING TABLE EXPRESSIONS

 Derived tables
 Common table expressions

15 November 2022 Dave Jong-A-Lock 22


GROUP AND PIVOT DATA BY USING QUERIES

 Writing grouped queries


 Pivoting and unpivoting data
 Using window functions

15 November 2022 Dave Jong-A-Lock 23


PROGRAM DATABASES BY USING TSQL

 Create database objects by using TSQL


 Implement error handling and transactions
 Implement data types and NULLs

15 November 2022 Dave Jong-A-Lock 24


CREATE DATABASE PROGRAMMABILITY OBJECTS BY USING TSQL

 Views
 User-defined functions
 Stored procedures

15 November 2022 Dave Jong-A-Lock 25


IMPLEMENT ERROR HANDLING AND TRANSACTIONS

 Understanding transactions
 Error handling with TRY-CATCH

15 November 2022 Dave Jong-A-Lock 26


IMPLEMENT DATA TYPES AND NULLS

 Working with data types


 Handling NULLs

15 November 2022 Dave Jong-A-Lock 27


THANK YOU

15 November 2022 Dave Jong-A-Lock 28

You might also like