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
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
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