0% found this document useful (0 votes)
35 views12 pages

Lecture 05 DMS

Uploaded by

dhkhang0803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

Lecture 05 DMS

Uploaded by

dhkhang0803
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

11/7/2023

Data Science for Economics and Business

DATABASE MANAGEMENT SYSTEM

SQL for Data Analytics

Lecture 08: Introduction query language structure SQL

1
11/7/2023

About This Module

SQL for Data Analytics


This Module focuses on learning core 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

Lecture Agenda

• Overview SQL (and T-SQL)


• Structured query language
• Data type

2
11/7/2023

SQL for Data Analytics

Section 1: Introduction to SQL (and T-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 … … … …
SELECT…
• Query language for SQL Server, Azure SQL Database, and other …





Microsoft relational database services
• SQL is declarative, not procedural
• Describe what you want, don’t specify steps

3
11/7/2023

Relational Databases - Remind


• Entities are represented as relations (tables), in which their attributes are
represented as domains (columns)
• Most relational databases are normalized, with relationships defined
between tables through primary and foreign keys 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

SalesOrderHeader 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

Schemas and Object Names

• Schemas are namespaces for database objects


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
Sales.Order Order Product
Production.Product

Sales.Customer Customer Order

Production.Order

4
11/7/2023

SQL Editor Tools

10

5
11/7/2023

SQL for Data Analytics

Section 2: SQL Statement types

11

SQL Statement Types

Data Manipulation Language Data Definition Language (DDL) Data Control Language (DCL) Transaction Control Language
(DML) (TCL)
Statements for querying Statements for defining Statements for assigning Statements for managing
and modifying data: database objects: security permissions: transaction:

• SELECT • CREATE • GRANT ● COMMIT


● ROLLBACK
• INSERT • ALTER • REVOKE ● SAVEPOINT
• UPDATE • DROP • DENY
• DELETE

Module

12

6
11/7/2023

SQL for Data Analytics

Section 3: Data types

13

Data Types

String Types
Why Data types are important?
🡪 Further practices with data types will
Date - Time
be introduce in another lecture.

Numbers • Compatible data types can be implicitly converted


• Explicit conversion requires an explicit conversion function:
CAST / TRY_CAST
Others CONVERT / TRY_CONVERT
STR

14

7
11/7/2023

Data Types - Strings


Data type Description Max size

char(n) Fixed width character string 8,000 characters

varchar(n) Variable width character string 8,000 characters

varchar(max) Variable width character string 1,073,741,824 characters

text Variable width character string 2GB of text data

nchar(n) Fixed width Unicode string 4,000 characters

nvarchar(n) Variable width Unicode string 4,000 characters

nvarchar(max) Variable width Unicode string 536,870,912 characters

ntext Variable width Unicode string 2GB of text data

binary(n) Fixed width binary string 8,000 bytes

varbinary Variable width binary string 8,000 bytes

varbinary(max) Variable width binary string 2GB

image Variable width binary string 2GB

15

Data Types - Numbers

Data type Description Max size

bit Integer that can be 0, 1, or NULL

tinyint Allows whole numbers from 0 to 255 1 byte

smallint Allows whole numbers between -215 and 215 2 bytes

int Allows whole numbers between -231 and 231 4 bytes

bigint Allows whole numbers between -263 and 263 8 bytes

Fixed precision and scale numbers.


decimal (p,s) 5-17 bytes
Allows numbers from -1038 +1 to 1038 -1.
Fixed precision and scale numbers.
numeric(p,s) 5-17 bytes
Allows numbers from -1038 +1 to 1038 -1.

money Monetary data from -263 to 263 8 bytes

smallmoney Monetary data from -214,748.3648 to 214,748.3647 4 bytes

float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308. 4 or 8 bytes

real Floating precision number data from -3.40E + 38 to 3.40E + 38 4 bytes

16

8
11/7/2023

Data Types – Date Time

Data type Description Max size


datetime From 1/1/1753 to 31/12/9999 with an accuracy of 3.33 milliseconds 8 bytes

datetime2 From 1/1/0001 to 31/12/9999 with an accuracy of 100 nanoseconds 6-8 bytes

smalldatetime From 1/1/1900 to 06/06/2079 with an accuracy of 1 minute 4 bytes

date Store a date only. From 1/1/0001 to 31/12/9999 3 bytes

time Store a time only to an accuracy of 100 nanoseconds 3-5 bytes

datetimeoffset The same as datetime2 with the addition of a time zone offset 8-10 bytes

17

NULL Values

• NULL represents a missing or unknown value


• ANSI behaviour for NULL values:
• The result of any expression containing a NULL value is NULL
• 2 + NULL = NULL
• 'MyString: ' + NULL = NULL
• Equality comparisons (=) always return false for NULL values, use IS NULL
• NULL = NULL returns false
• NULL IS NULL returns true

• Useful functions:
• ISNULL(column/variable, value): Returns value if the column or variable is NULL
• NULLIF(column/variable, value): Returns NULL if the column or variable is value
• COALESCE(column/variable1, column/variable2, ...): Returns the value of the first non-NULL column or variable in the list

18

9
11/7/2023

Module Review
You must return the Name and Price columns from a table named Product in the Production schema. In the resulting rowset,
you want the Name column to be named ProductName. Which of the following Transact-SQL statements should you use?
❑ SELECT * FROM Product AS Production.Product;
❑ SELECT Name AS ProductName, Price FROM Production.Product;
❑ SELECT ProductName, Price FROM Production.Product;

You must retrieve data from a column that is defined as char(1). If the value in the column is a digit between 0 and 9, the
query should return it as an integer value. Otherwise, the query should return NULL.
Which function should you use?
❑ CAST
❑ NULLIF
❑ TRY_CONVERT

You must return the Cellphone column from the Sales.Customer table. Cellphone is a varchar column that permits NULL
values. For rows where the Cellphone value is NULL, your query should return the text 'None'. What query should you use?
❑ SELECT ISNULL(Cellphone, 'None') AS Cellphone FROM Sales.Customer;
❑ SELECT NULLIF(Cellphone, 'None') AS Cellphone FROM Sales.Customer;
❑ SELECT CONVERT(varchar, Cellphone) AS None FROM Sales.Customer;

19

SQL for Data Analytics

Section 4: Operators and expression

20

10
11/7/2023

Operators
Group Operator Description

= Equal
<> Or != Not equal
Comparison operators > Greater than
< Less than
>= Greater than or equal
<= Less than or equal

AND Return records that meet all the conditions separated by AND in WHERE clause.

Logical operators OR Return records that meet any of the conditions separated by OR in WHERE clause.

NOT Return records that do not satisfy any of the conditions in WHERE clause.
[NOT] BETWEEN Returns values [NOT] within a given range

[NOT] IN Specify multiple values in a WHERE clause (a shorthand for multiple OR conditions)

SQL operators IS [NOT] NULL Returns records having [NOT] NULL values in the given fileds.

[NOT] LIKE Returns records that [DO NOT] match a specified pattern in a column.

21

Expressions

SQL expression is a combination of one or more values, operators and


SQL functions that are all evaluated to a value.
Expressions are used in WHERE clause of an SQL query.
• Boolean expressions.
• Numeric expression.
• Date and time expression.

22

11
11/7/2023

Database Design

It’s time for your questions

23

THANK YOU !

24

12

You might also like