SlideShare a Scribd company logo
© Copyright Microsoft Corporation. All rights reserved.
Course DP-080:
Querying Data with
Microsoft Transact-SQL
© Copyright Microsoft Corporation. All rights reserved.
About This Course
Learn how to write queries using SQL Server and Azure SQL Database
• This course focuses on learning core Transact-SQL syntax used to work with data for
reporting and application development
• Using SELECT to retrieve columns from a table
• Sorting and filtering query results
• Using joins and subqueries to retrieve data from multiple tables
• Using built-in functions, aggregations, and groupings
• Inserting, updating, and deleting data
• Additional learning materials are available on Microsoft Learn
© Copyright Microsoft Corporation. All rights reserved.
Course Agenda
Day 1: Getting Started with Transact-SQL
Day 2: Filtering Query Results and Data type functions
Day 3: Introduction to Join and Union
Day 4: Using Joins and Subqueries
Day 5: Aggregate functions and Group by
Day 6: Practise and course summarize
© Copyright Microsoft Corporation. All rights reserved.
Lab Environment
Hosted Virtual Machine
• Windows 10
• SQL Server Express
• Azure Data Studio
© Copyright Microsoft Corporation. All rights reserved.
Getting Started with
Transact-SQL
© Copyright Microsoft Corporation. All rights reserved.
Module
Agenda
Introduction to Transact-SQL
Using the SELECT Statement
Sorting and Limiting Query Results
© Copyright Microsoft Corporation. All rights reserved.
Lesson 1: Introduction to Transact-SQL
What is Transact-SQL?
Structured Query Language (SQL)
• Developed in the 1970s as a language for querying
databases
• Adopted as a standard by ANSI and ISO standards bodies
• Widely used across multiple database systems
Microsoft’s implementation is Transact-SQL
• Often referred to as T-SQL
• Query language for SQL Server (the box product), Azure
SQL Database (the cloud platform), and other Microsoft
relational database services (RDBMs)
SQL is declarative, not procedural
• Describe what you want, don’t specify steps
SELECT…
… … … …
… … … …
… … … …
… … … …
Main SQL Concept
• Server
• Database
• Schema
• Table
• Column and Row
• Data server is the warehouse that hosts your databases
• In relational databases, data is organized in a hierarchy, like files and folders
→ Databases have schemas. Schemas have tables. Tables have columns.
Schemas
• Schema defines the database structure (how the database is constructed and
managed).
• A database can have one or multiple schemas.
• The schema name must be distinct from the name of any existing schema in the
current database (i.e. schema name must be unique).
• The dbo schema is the default schema for a newly created database.
• Benefits of using schemas:
✓ Logically organize your DB
✓ Improve security by allowing which users can access which part of your DB
Tables
• Table is the primary storage
object for data in a relational
database. A database most
often contains one or more
tables.
• A table consists of row(s) and
column(s), both of which hold
the data.
• A table takes up physical space
in a database and can be
permanent or temporary
Columns and Rows in a Table
Columns
• A column (field) is also called an attribute.
• The columns in a table hold specific types of data, such as name, age or
address of customers.
Rows
• A row is a record of data in a database table.
• For example, a row of data in a customer table might consist of a particular
customer's identification number, name, address, phone number, fax
number, and so on
Primary Key and Foreign Key
Most relational databases are normalized, with relationships defined between tables
through primary and foreign keys
Primary key
• A primary key is a field (or collection of fields)
• The primary key constraint uniquely identifies each record (row) in a database table.
• Primary keys must contain unique values and cannot contain NULL.
• A table can have only one primary key, which may consist of single or multiple
fields (columns)
Foreign Key
• A foreign key is a key used to link two tables together.
• A foreign key is a field (or collection of fields) in one table that refers to the
primary key(s) in another table.
SalesOrderDetail
OrderID LineItemNo ProductID Quantity
1 1 3 1
2 1 2 5
2 2 3 1
3 1 1 1
Primary Key and Foreign Key
Customer
CustomerID FirstName LastName
1 Dan Drayton
2 Aisha Witt
3 Rosie Reeves
Product
ProductID Name ListPrice
1 Widget 2.99
2 Gizmo 1.79
3 Thingybob 3.49
SalesOrder
OrderID OrderDate CustomerID
1 1/1/2015 1
2 1/1/2015 3
3 1/2/2015 1
Exercise: Determine PK and FK in below tables through existence relationships
Query Objects name
• Fully-qualified names:
[server_name.][database_name.][schema_name.]object_name
• Within database context, best practice is to include
schema name:
schema_name.object_name
Sales Production
Order
Customer
Product
Order
Sales.Orde
r
Sales.Custom
er
Production.Produc
t
Production.Orde
r
SQL Statement Types
Data Manipulation Language (DML) Data Definition Language (DDL) Data Control Language (DCL)
Statements for querying and
modifying data:
• SELECT
• INSERT
• UPDATE
• DELETE
Statements for defining database
objects:
• CREATE
• ALTER
• DROP
Statements for assigning security
permissions:
• GRANT
• REVOKE
• DENY
Focus of this course
© Copyright Microsoft Corporation. All rights reserved.
Lesson 2: Using the SELECT Statement
The SELECT Statement
Element Expression Role
SELECT <select list> Defines which columns to return
FROM <table source> Defines table(s) to query
WHERE <search condition> Filters rows using a predicate
GROUP BY <group by list> Arranges rows by groups
HAVING <search condition> Filters groups using a predicate
ORDER BY <order by list> Sorts the output
5
1
2
3
4
6
SELECT ProductLine, COUNT(ProductKey) as NoPro
duct
FROM dbo.DimProduct
WHERE Color IN ('Red', 'Black')
GROUP BY ProductLine
HAVING COUNT(ProductKey) > 10
ORDER BY ProductLine DESC;
Basic SELECT Statement
The SELECT statement is used to select data from a database.
SELECT *
FROM table_name
Or
SELECT column1, column2, column3
FROM table_name
Try:
SELECT * FROM dbo.DimProduct
SELECT EmployeeKey, FirstName,LastName FROM dbo.DimEmployee;
SQL Comments
Comments are used to explain sections of SQL statements, or to prevent execution of SQL
statements.
o Single line comments
o Multiple-line comments
-- This is a single line comment in SQL
SELECT ProductKey, Color, ListPrice
FROM dbo.DimProduct
/* This comment can be placed in
multiple lines in SQL */
SELECT EmployeeKey /*or even here*/, FirstName, LastName
FROM dbo.DimEmployee
SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name (only exists for the
duration of the query).
Try:
SELECT EmployeeKey as 'Ma nhan vien',
FirstName,
LastName,
Gender as 'Gioi tinh'
FROM dbo.DimEmployee
Aliases can be useful when:
• There are more than one table involved in a query (and some of their columns have the same
name)
• Functions are used in the query
• Column names are big or not very readable
• Two or more columns are combined together
© Copyright Microsoft Corporation. All rights reserved.
Lesson 3: Sorting and Limiting Query Results
Sorting Results
Use ORDER BY to sort results by one or more columns
• Aliases created in SELECT clause are visible to ORDER BY
• You can order by columns in the source that are not included in the SELECT clause
• You can specify ASC or DESC (ASC is the default)
SELECT ProductCategoryID AS Category, [Name]
FROM dbo.DimProduct
ORDER BY Category ASC, ListPrice DESC;
Limiting Results
SELECT TOP Clause: used to specify the number of records to return
SELECT TOP N [Percent] [WITH TIES]
Try:
SELECT TOP 10 * FROM dbo.DimProduct;
SELECT TOP 10 ProductKey, EnglishProductName, ListPrice
FROM dbo.DimProduct;
SELECT TOP 10 PERCENT ProductKey, EnglishProductName, ListPrice
FROM dbo.DimProduct;
SELECT TOP 10 ProductKey, EnglishProductName, ListPrice
FROM dbo.DimProduct
ORDER BY ListPrice DESC ;
Limiting Results
SELECT DISTINCT Clause: used to return only distinct (unique) records.
SELECT DISTINCT column1, column2, colum3
FROM table_name
Try:
SELECT Title
FROM dbo.DimEmployee
Compare with the result of:
SELECT DISTINCT Title
FROM dbo.DimEmployee
© Copyright Microsoft Corporation. All rights reserved.

More Related Content

PPTX
SQL SERVER Training in Pune Slides
PPSX
MS SQL Server
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
intro for sql
PDF
databases management system and other DBA1 نظري.pdf
PPTX
Querying_with_T-SQL_-_01.pptx
PPTX
Using Basic Structured Query Language lo1.pptx
PPTX
SQL Commands
SQL SERVER Training in Pune Slides
MS SQL Server
MYSQL Presentation for SQL database connectivity
intro for sql
databases management system and other DBA1 نظري.pdf
Querying_with_T-SQL_-_01.pptx
Using Basic Structured Query Language lo1.pptx
SQL Commands

Similar to DP080_Lecture_1 SQL lecture document .pdf (20)

PPTX
Unit - II.pptx
PPTX
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
PPT
Less07 schema
PPTX
SQL_Part1
PPT
Database management and System Development ppt
PPTX
Islamic University Previous Year Question Solution 2018 (ADBMS)
PPTX
Introduction to SQL, SQL*Plus
PPTX
ms-sql-server-150223140402-conversion-gate02.pptx
PPT
Manage schema object.ppt
PPTX
SQL.pptx for the begineers and good know
PDF
Structure query language, database course
PPTX
HPD SQL Training - Beginner - 20220916.pptx
PPT
SQL Inteoduction to SQL manipulating of data
PDF
IR SQLite Session #1
PPTX
How Clean is your Database? Data Scrubbing for all Skill Sets
PPT
Unit4_Lecture-sql.ppt and data science relate
PDF
Module 3 design and implementing tables
PPTX
Data Query Using Structured Query Language - WITH NOTES.pptx
PPTX
The SQL Query Language: Simple SELECT Commands
PPT
Module02
Unit - II.pptx
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
Less07 schema
SQL_Part1
Database management and System Development ppt
Islamic University Previous Year Question Solution 2018 (ADBMS)
Introduction to SQL, SQL*Plus
ms-sql-server-150223140402-conversion-gate02.pptx
Manage schema object.ppt
SQL.pptx for the begineers and good know
Structure query language, database course
HPD SQL Training - Beginner - 20220916.pptx
SQL Inteoduction to SQL manipulating of data
IR SQLite Session #1
How Clean is your Database? Data Scrubbing for all Skill Sets
Unit4_Lecture-sql.ppt and data science relate
Module 3 design and implementing tables
Data Query Using Structured Query Language - WITH NOTES.pptx
The SQL Query Language: Simple SELECT Commands
Module02
Ad

Recently uploaded (20)

PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PPT
Quality review (1)_presentation of this 21
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
1_Introduction to advance data techniques.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Computer network topology notes for revision
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
Business Acumen Training GuidePresentation.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PDF
Mega Projects Data Mega Projects Data
PPTX
Qualitative Qantitative and Mixed Methods.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Galatica Smart Energy Infrastructure Startup Pitch Deck
oil_refinery_comprehensive_20250804084928 (1).pptx
Quality review (1)_presentation of this 21
.pdf is not working space design for the following data for the following dat...
1_Introduction to advance data techniques.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
Computer network topology notes for revision
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Reliability_Chapter_ presentation 1221.5784
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Business Acumen Training GuidePresentation.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Acceptance and paychological effects of mandatory extra coach I classes.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Mega Projects Data Mega Projects Data
Qualitative Qantitative and Mixed Methods.pptx
Ad

DP080_Lecture_1 SQL lecture document .pdf

  • 1. © Copyright Microsoft Corporation. All rights reserved. Course DP-080: Querying Data with Microsoft Transact-SQL
  • 2. © Copyright Microsoft Corporation. All rights reserved. About This Course Learn how to write queries using SQL Server and Azure SQL Database • This course focuses on learning core Transact-SQL syntax used to work with data for reporting and application development • Using SELECT to retrieve columns from a table • Sorting and filtering query results • Using joins and subqueries to retrieve data from multiple tables • Using built-in functions, aggregations, and groupings • Inserting, updating, and deleting data • Additional learning materials are available on Microsoft Learn
  • 3. © Copyright Microsoft Corporation. All rights reserved. Course Agenda Day 1: Getting Started with Transact-SQL Day 2: Filtering Query Results and Data type functions Day 3: Introduction to Join and Union Day 4: Using Joins and Subqueries Day 5: Aggregate functions and Group by Day 6: Practise and course summarize
  • 4. © Copyright Microsoft Corporation. All rights reserved. Lab Environment Hosted Virtual Machine • Windows 10 • SQL Server Express • Azure Data Studio
  • 5. © Copyright Microsoft Corporation. All rights reserved. Getting Started with Transact-SQL
  • 6. © Copyright Microsoft Corporation. All rights reserved. Module Agenda Introduction to Transact-SQL Using the SELECT Statement Sorting and Limiting Query Results
  • 7. © Copyright Microsoft Corporation. All rights reserved. Lesson 1: Introduction to Transact-SQL
  • 8. What is Transact-SQL? Structured Query Language (SQL) • Developed in the 1970s as a language for querying databases • Adopted as a standard by ANSI and ISO standards bodies • Widely used across multiple database systems Microsoft’s implementation is Transact-SQL • Often referred to as T-SQL • Query language for SQL Server (the box product), Azure SQL Database (the cloud platform), and other Microsoft relational database services (RDBMs) SQL is declarative, not procedural • Describe what you want, don’t specify steps SELECT… … … … … … … … … … … … … … … … …
  • 9. Main SQL Concept • Server • Database • Schema • Table • Column and Row • Data server is the warehouse that hosts your databases • In relational databases, data is organized in a hierarchy, like files and folders → Databases have schemas. Schemas have tables. Tables have columns.
  • 10. Schemas • Schema defines the database structure (how the database is constructed and managed). • A database can have one or multiple schemas. • The schema name must be distinct from the name of any existing schema in the current database (i.e. schema name must be unique). • The dbo schema is the default schema for a newly created database. • Benefits of using schemas: ✓ Logically organize your DB ✓ Improve security by allowing which users can access which part of your DB
  • 11. Tables • Table is the primary storage object for data in a relational database. A database most often contains one or more tables. • A table consists of row(s) and column(s), both of which hold the data. • A table takes up physical space in a database and can be permanent or temporary
  • 12. Columns and Rows in a Table Columns • A column (field) is also called an attribute. • The columns in a table hold specific types of data, such as name, age or address of customers. Rows • A row is a record of data in a database table. • For example, a row of data in a customer table might consist of a particular customer's identification number, name, address, phone number, fax number, and so on
  • 13. Primary Key and Foreign Key Most relational databases are normalized, with relationships defined between tables through primary and foreign keys Primary key • A primary key is a field (or collection of fields) • The primary key constraint uniquely identifies each record (row) in a database table. • Primary keys must contain unique values and cannot contain NULL. • A table can have only one primary key, which may consist of single or multiple fields (columns) Foreign Key • A foreign key is a key used to link two tables together. • A foreign key is a field (or collection of fields) in one table that refers to the primary key(s) in another table.
  • 14. SalesOrderDetail OrderID LineItemNo ProductID Quantity 1 1 3 1 2 1 2 5 2 2 3 1 3 1 1 1 Primary Key and Foreign Key Customer CustomerID FirstName LastName 1 Dan Drayton 2 Aisha Witt 3 Rosie Reeves Product ProductID Name ListPrice 1 Widget 2.99 2 Gizmo 1.79 3 Thingybob 3.49 SalesOrder OrderID OrderDate CustomerID 1 1/1/2015 1 2 1/1/2015 3 3 1/2/2015 1 Exercise: Determine PK and FK in below tables through existence relationships
  • 15. Query Objects name • Fully-qualified names: [server_name.][database_name.][schema_name.]object_name • Within database context, best practice is to include schema name: schema_name.object_name Sales Production Order Customer Product Order Sales.Orde r Sales.Custom er Production.Produc t Production.Orde r
  • 16. SQL Statement Types Data Manipulation Language (DML) Data Definition Language (DDL) Data Control Language (DCL) Statements for querying and modifying data: • SELECT • INSERT • UPDATE • DELETE Statements for defining database objects: • CREATE • ALTER • DROP Statements for assigning security permissions: • GRANT • REVOKE • DENY Focus of this course
  • 17. © Copyright Microsoft Corporation. All rights reserved. Lesson 2: Using the SELECT Statement
  • 18. The SELECT Statement Element Expression Role SELECT <select list> Defines which columns to return FROM <table source> Defines table(s) to query WHERE <search condition> Filters rows using a predicate GROUP BY <group by list> Arranges rows by groups HAVING <search condition> Filters groups using a predicate ORDER BY <order by list> Sorts the output 5 1 2 3 4 6 SELECT ProductLine, COUNT(ProductKey) as NoPro duct FROM dbo.DimProduct WHERE Color IN ('Red', 'Black') GROUP BY ProductLine HAVING COUNT(ProductKey) > 10 ORDER BY ProductLine DESC;
  • 19. Basic SELECT Statement The SELECT statement is used to select data from a database. SELECT * FROM table_name Or SELECT column1, column2, column3 FROM table_name Try: SELECT * FROM dbo.DimProduct SELECT EmployeeKey, FirstName,LastName FROM dbo.DimEmployee;
  • 20. SQL Comments Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements. o Single line comments o Multiple-line comments -- This is a single line comment in SQL SELECT ProductKey, Color, ListPrice FROM dbo.DimProduct /* This comment can be placed in multiple lines in SQL */ SELECT EmployeeKey /*or even here*/, FirstName, LastName FROM dbo.DimEmployee
  • 21. SQL Aliases SQL aliases are used to give a table, or a column in a table, a temporary name (only exists for the duration of the query). Try: SELECT EmployeeKey as 'Ma nhan vien', FirstName, LastName, Gender as 'Gioi tinh' FROM dbo.DimEmployee Aliases can be useful when: • There are more than one table involved in a query (and some of their columns have the same name) • Functions are used in the query • Column names are big or not very readable • Two or more columns are combined together
  • 22. © Copyright Microsoft Corporation. All rights reserved. Lesson 3: Sorting and Limiting Query Results
  • 23. Sorting Results Use ORDER BY to sort results by one or more columns • Aliases created in SELECT clause are visible to ORDER BY • You can order by columns in the source that are not included in the SELECT clause • You can specify ASC or DESC (ASC is the default) SELECT ProductCategoryID AS Category, [Name] FROM dbo.DimProduct ORDER BY Category ASC, ListPrice DESC;
  • 24. Limiting Results SELECT TOP Clause: used to specify the number of records to return SELECT TOP N [Percent] [WITH TIES] Try: SELECT TOP 10 * FROM dbo.DimProduct; SELECT TOP 10 ProductKey, EnglishProductName, ListPrice FROM dbo.DimProduct; SELECT TOP 10 PERCENT ProductKey, EnglishProductName, ListPrice FROM dbo.DimProduct; SELECT TOP 10 ProductKey, EnglishProductName, ListPrice FROM dbo.DimProduct ORDER BY ListPrice DESC ;
  • 25. Limiting Results SELECT DISTINCT Clause: used to return only distinct (unique) records. SELECT DISTINCT column1, column2, colum3 FROM table_name Try: SELECT Title FROM dbo.DimEmployee Compare with the result of: SELECT DISTINCT Title FROM dbo.DimEmployee
  • 26. © Copyright Microsoft Corporation. All rights reserved.