Professional Documents
Culture Documents
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
Module
Agenda Using the SELECT Statement
… … … …
• Often referred to as T-SQL … … … …
• Server
• Database
• Schema
• Table
• Column and Row
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.
Primary Key and Foreign Key
SalesOrderDetail
Customer
OrderID LineItemNo ProductID Quantity
CustomerID FirstName LastName
1 1 3 1
1 Dan Drayton
2 1 2 5
2 Aisha Witt
2 2 3 1
3 Rosie Reeves
3 1 1 1
SalesOrder Product
OrderID OrderDate CustomerID ProductID Name ListPrice
1 1/1/2015 1 1 Widget 2.99
2 1/1/2015 3 2 Gizmo 1.79
3 1/2/2015 1 3 Thingybob 3.49
Query Objects name
• Fully-qualified names:
[server_name.][database_name.][schema_name.]object_name
Sales Production
Sales.Orde Order Product
r Production.Produc
t
Sales.Custom Customer Order
er
Production.Orde
r
SQL Statement Types
Data Manipulation Language (DML) Data Definition Language (DDL) Data Control Language (DCL)
Statements for querying and Statements for defining database Statements for assigning security
modifying data: objects: permissions:
SELECT *
FROM table_name
Or
Try:
SELECT EmployeeKey, FirstName,LastName FROM dbo.DimEmployee;
o Multiple-line comments
/* This comment can be placed in
multiple lines in SQL */
Try:
Try:
SELECT Title
FROM dbo.DimEmployee