0% found this document useful (0 votes)
32 views74 pages

SQL 5

The document outlines a training module on advanced databases and SQL querying, led by Yaroslav Dobrianskyi, a lead software engineer with extensive experience. It covers key topics such as SQL transactions, error handling, and concurrency issues, emphasizing the importance of ACID properties and various transaction control commands. Additionally, it provides recommendations for participants, including muting microphones and utilizing a Q&A channel.

Uploaded by

and311003
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)
32 views74 pages

SQL 5

The document outlines a training module on advanced databases and SQL querying, led by Yaroslav Dobrianskyi, a lead software engineer with extensive experience. It covers key topics such as SQL transactions, error handling, and concurrency issues, emphasizing the importance of ACID properties and various transaction control commands. Additionally, it provides recommendations for participants, including muting microphones and utilizing a Q&A channel.

Uploaded by

and311003
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/ 74

Module “DBMS

Basics”
Submodule “Advanced Databases and SQL
Querying”
Yaroslav Dobrianskyi
Lead Software Engineer

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Recommendations

1 MUTE YOUR MIC

2 RAISE HAND ASK QUESTIONS

3 DURATION: 3 HOURS

4 COFFEE BREAK: 10-15 MINUTES

5 Q&A AFTER EACH MODULE

6 TEAMS QUESTIONS CHANNEL

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 2


YA R O S L AV D O B R I A N S K Y I
Lead Software Engineer

• 13 years experience in software development


• Worked at different positions -> DBA, DB Development, ETL,
Python Developer
• Worked at EPAM in 2015-2019 years, joined second time in
2021

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 3


Agenda

1. SQL transactions and Error Handling


2. SQL Grouping Functions and Ranking Functions
3. SQL XML Data Types
4. SQL Partitions

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 4


SQL TRANSACTIONS
AND ERROR HANDLING

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 5


Concept of Transactions
WHAT ASSOCIATION DO YOU HAVE WITH WORD «TRANSACTION» ?

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Concept of Transactions
MOST PEOPLE IMAGINE TRANSACTION LIKE THIS

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Concept of Transactions
FAILURE SCENARIO’S

SCENARIO 1:
• YOU PUT CREDIT CARD INTO ATM RECEIVER TO GET 100$
• YOU HAVE ENTERED PIN AND SELECTED TO TAKE 100$ CASH
• WHEN YOU WAITING TO GET THE MONEY, THE ATM CRASHED

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Concept of Transactions
FAILURE SCENARIO’S

SCENARIO 1:
• YOU PUT CREDIT CARD INTO ATM RECEIVER TO GET 100$
• YOU HAVE ENTERED PIN AND SELECTED TO TAKE 100$ CASH
• WHEN YOU WAITING TO GET THE MONEY, THE ATM CRASHED

SCENARIO 2:
• YOU ARE MAKING AN ORDER AT ONLINE STORE MARKET
• YOU ENTERED YOU CREDIT CARD INFORMATION AND CLICK «PAY»
• SODDENLY INTERNET CONNECTION HAS BEEM LOST

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


What is transaction?
• A transaction is a single unit of work
• If a transaction is successful, all of the data modifications made during the transaction are
committed and become a permanent part of the database
• If a transaction encounters errors and must be canceled or rolled back, then all of the data
modifications are erased

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 10


What is transaction?

TRANSACTION
IS ALL
OR NONE

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


Understanding ACID Properties
A SQL transaction is characterized by four properties, often referred to as the ACID properties: atomicity,
consistency, isolation, and durability
• Atomicity requires that each transaction is "all or • Consistency ensures that any transaction will take the
nothing". If one part of a transaction fails, the entire database from one valid state to another.
transaction fails.

• Isolation: every transaction has a well-defined boundary; • Durability: data modifications that occur within a
that is, it is isolated from another transaction. One successful transaction are kept permanently within the
transaction shouldn't affect other transactions running at system regardless of what else occurs
the same time.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 12


Transaction Control

The following commands are used to control transactions:


• BEGIN TRANSACTION − to begin transaction

• COMMIT − to save the changes


• ROLLBACK − to roll back the changes
• SAVEPOINT − creates points within the groups of transactions in which to ROLLBACK

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 13


BEGIN TRANSACTION with T-SQL
It indicates the start point of an explicit or local transaction
BEGIN { TRAN | TRANSACTION }
{ transaction_name | @tran_name_variable } --optional
WITH MARK [ 'description' ] --optional

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 14


BEGIN TRANSACTION with T-SQL
It indicates the start point of an explicit or local transaction
BEGIN { TRAN | TRANSACTION }
{ transaction_name | @tran_name_variable } --optional
WITH MARK [ 'description' ] --optional

Example:

BEGIN TRANSACTION DeleteJobCandidate


DELETE FROM HumanResources.JobCandidate
WHERE JobCandidateID = 13

--COMMIT TRANSACTION DeleteJobCandidate


--ROLLBACK TRANSACTION DeleteJobCandidate

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 15


COMMIT TRANSACTION with T-SQL
If everything is in order with all statements within a single transaction, all changes are recorded together in the database
is called committed
COMMIT [ { TRAN | TRANSACTION }
[ transaction_name | @tran_name_variable ] ]--optional

Example:

BEGIN TRANSACTION DeleteJobCandidate


DELETE FROM HumanResources.JobCandidate
WHERE JobCandidateID = 13

--COMMIT TRANSACTION DeleteJobCandidate

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 16


ROLLBACK TRANSACTION with T-SQL
If any error occurs with any of the SQL grouped statements, all changes need to be aborted. The process of reversing
changes is called rollback
ROLLBACK { TRAN | TRANSACTION }
[ transaction_name | @tran_name_variable
| savepoint_name | @savepoint_variable ]

Example:

BEGIN TRANSACTION DeleteJobCandidate


DELETE FROM HumanResources.JobCandidate
WHERE JobCandidateID = 1024541

--ROLLBACK TRANSACTION DeleteJobCandidate


--ROLLBACK

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 17


SAVEPOINT with T-SQL
Creates points within the groups of transactions in which to ROLLBACK. In SQL Server command SAVE

SAVE { TRAN | TRANSACTION }


{ savepoint_name | @savepoint_variable }

Example:

BEGIN TRAN
SAVE TRAN SP1
DELETE FROM HumanResources.JobCandidate
WHERE JobCandidateID = 12

SAVE TRAN SP2


ROLLBACK TRANSACTION SP1;

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 18


Concurrent Access Problems Using Transactions
With parallel execution of transactions, the following problems are possible:

• Lost update – when one data block is simultaneously changed by different transactions, all changes are lost except for
the last one;

• "Dirty" read – reading data added or modified by a transaction, which will not be confirmed (rolled back);

• Non-repeatable read – when re-reading within one transaction, previously read data is changed;

• Phantom reads – one transaction during its execution several times selects many rows according to the same criteria.
Another transaction, in the intervals between these selections, adds rows or changes columns. As a result, it turns out
that the same samples in the first transaction give different sets of rows.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 19


Concurrent Access Problems Using Transactions
DIRTY READS

Here,
1.T1 reads the value of A.
2.T1 updates the value of A in the buffer.
3.T2 reads the value of A from the buffer.
4.T2 writes the updated the value of A.
5.T2 commits.
6.T1 fails in later stages and rolls back.

In this example,
•T2 reads the dirty value of A written by the uncommitted
transaction T1.
•T1 fails in later stages and roll backs.
•Thus, the value that T2 read now stands to be incorrect.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 20


Concurrent Access Problems Using Transactions
NON-REPEATABLE READS

Here,
1.T1 reads the value of X (= 10 say).
2.T2 reads the value of X (= 10).
3.T1 updates the value of X (from 10 to 15 say) in the
buffer.
4.T2 again reads the value of X (but = 15).

In this example,
•T2 gets to read a different value of X in its second
reading.
•T2 wonders how the value of X got changed because
according to it, it is running in isolation.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 21


Concurrent Access Problems Using Transactions
LOST UPDATES

Here,
1.T1 reads the value of A (= 10 say).
2.T2 updates the value to A (= 15 say) in the buffer.
3.T2 does blind write A = 25 (write without read) in the
buffer.
4.T2 commits.
5.When T1 commits, it writes A = 25 in the database.

In this example,
•T1 writes the over written value of X in the database.
•Thus, update from T1 gets lost.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 22


Concurrent Access Problems Using Transactions
PHANTOM READS

Here,
1.T1 reads X.
2.T2 reads X.
3.T1 deletes X.
4.T2 tries reading X but does not find it.

In this example,
•T2 finds that there does not exist any variable X when it
tries reading X again.
•T2 wonders who deleted the variable X because
according to it, it is running in isolation.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 23


Concurrent Access Problems Using Transactions

SET TRANSACTION ISOLATION LEVEL


{ READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SNAPSHOT
| SERIALIZABLE
}

READ COMMITTED is the default isolation level for SQL Server

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 24


Concurrent Access Problems Using Transactions

READ UNCOMMITTED is the lowest isolation level . With this isolation level,
we allow a transaction to read the data which is being updated by other
transaction and not yet committed and resulting a dirty reads.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 25


Concurrent Access Problems Using Transactions

READ UNCOMMITTED is the lowest isolation level . With this isolation level,
we allow a transaction to read the data which is being updated by other
transaction and not yet committed and resulting a dirty reads.

READ COMITTED is the default isolation level for SQL Server. This prevents
Dirty Read. When this level is set, the transactions can not read the data that is
being modified by the current transaction. This will force user to wait for the
current transaction to finish up its job. But the problem with this level that is
doesn’t resolve Phantom reads or Inconsistency Analysis – as it asks User A to
wait for Read but not for update or insert.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 26


Concurrent Access Problems Using Transactions

READ UNCOMMITTED is the lowest isolation level . With this isolation level,
we allow a transaction to read the data which is being updated by other
transaction and not yet committed and resulting a dirty reads.

READ COMITTED is the default isolation level for SQL Server. This prevents
Dirty Read. When this level is set, the transactions can not read the data that is
being modified by the current transaction. This will force user to wait for the
current transaction to finish up its job. But the problem with this level that is
doesn’t resolve Phantom reads or Inconsistency Analysis – as it asks User A to
wait for Read but not for update or insert.

REPEATABLE READ do same job as READ COMMITED and prevents non-


repeatable reads and lost updates. User A will wait for the transaction being
executed by User B to execute it's Update query as well, like Read Query. But
Insert query doesn't wait, this also creates Phantom Read problem.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 27


Concurrent Access Problems Using Transactions

SERIALIZABLE This is the maximum level of Isolation level provided by SQL


Server transaction. We can prevent Phantom Read problem by implementing
this level of isolation. It asks User A to wait for the current transaction for any
kind of operation he wants to perform. But this cost a performance and
execution time for other transactions to wait current transaction completes.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 28


Concurrent Access Problems Using Transactions

SERIALIZABLE This is the maximum level of Isolation level provided by SQL


Server transaction. We can prevent Phantom Read problem by implementing
this level of isolation. It asks User A to wait for the current transaction for any
kind of operation he wants to perform. But this cost a performance and
execution time for other transactions to wait current transaction completes.

SNAPSHOT This level takes a snapshot of current data. Every transaction


works on its own copy of data. When User A tries to update or insert or read
anything, we ask him to re-verify the table row once again from the starting
time of its execution, so that he can work on fresh data. with this level. We are
not giving full faith to User A that he is going to work on fresh data but giving
high-level changes of data integrity.

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 29


Concurrent Access Problems Using Transactions

Read
phenomena
Non-repeatable
Dirty reads Lost updates Phantoms
reads

Isolation level

Read
- - - -
Uncommitted
Read
+ - - -
Committed
Repeatable
+ + + -
Read

Snapshot + + + +

Serializable + + + +

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 30


What is Error Handling

• Error handling in SQL Server gives us control over the Transact-SQL code
• For example, when things go wrong, we get a chance to do something about it and possibly make it right
again. SQL Server error handling can be as simple as just logging that something happened, or it could
be us trying to fix an error

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 31


@@ERROR

• This global variable returns the error number for the last Transact-SQL statement executed

@@error = 0 – success
@@error = 1 – fail

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 32


RAISERROR

• Generates an error message and initiates error processing for the session. RAISERROR can
either reference a user-defined message stored in the sys.messages catalog view or build a
message dynamically
• 16 – severity, 1 - state

RAISERROR('STATEMENT FAILED - THIS IS MY CUSTOM MESSAGE', 16, 1)

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 33


TRY…CATCH

• Implements error handling for Transact-SQL. A group of Transact-SQL statements can be


enclosed in a TRY block. If an error occurs in the TRY block, control is passed to another
group of statements that is enclosed in a CATCH block

Syntax
BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
{ sql_statement | statement_block }
END CATCH

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 34


TRY…CATCH

Now, inside the CATCH statement, we can try to fix the error, report the error or even log the
error, so we know when it happened, who did it by logging the username, all the useful stuff.
We even have access to some special data only available inside the CATCH statement:

ERROR_NUMBER – returns the internal number of the error


ERROR_STATE – returns the information about the source
ERROR_SEVERITY – returns the information about anything from informational errors to errors
user of DBA can fix, etc.
ERROR_LINE – returns the line number at which an error happened on
ERROR_PROCEDURE – returns the name of the stored procedure or function
ERROR_MESSAGE – returns the most essential information and that is the message text of the
error

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 35


TRY…CATCH

BEGIN TRY
SELECT
1 / 0 AS Error;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_STATE() AS ErrorState,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 36


Limitations and Disadvantages of Transactions and EH
in SQL Server

1 KEEP TRANSACTIONS SHORT

BEGIN THEM AS LATE AS POSSIBLE END THEM AS


2
EARLY AS POSSIBLE

3 WE ARE GETTING MORE LOCK W AITING AND DEADLOCKS

CONCURRENT ACCESS PROBLEMS USING TRANSACTIONS (LOST UPDATE,


4
DIRTY READ, NON-REPEATABLE READ, PHANTOM READS)

EXCEPTIONS PROVIDE A CLEAN WAY TO CHECK FOR


5
ERRORS WITHOUT CLUTTERING CODE

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 37


SQL GROUPING FUNCTIONS
AND RANKING FUNCTIONS

CONFIDENTIAL | © 2020 EPAM Systems, Inc. 38


SQL grouping functions and ranking functions

SQL Grouping Sets

GROUP BY – group data by single grouping set​


GROUP BY GROUPING SETS - group data by multiple grouping sets​
GROUP BY CUBE – accepts a list of expressions and defines all possible grouping sets from this input​
GROUP BY ROLLUP – is applied for hierarchical data structures (e.g. location -> country, region, city)​

Syntax: SELECT SELECT


SELECT
FROM FROM
FROM
GROUP BY ROLLUP ( GROUP BY CUBE (
GROUP BY GROUPING SETS (
groupSet groupSet
groupSet
) )
[ , groupSet ]
[ , …]
)

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [CountryRegionCode],[Group],[SalesYTD] FROM [AdventureWorks2019].[Sales].[SalesTerritory]

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [CountryRegionCode],[GROUP], SUM([SalesYTD]) AS Sales_YTD
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY [CountryRegionCode],[Group]
UNION ALL
-- Subtotal by GROUP
SELECT NULL,[GROUP], SUM([SalesYTD]) AS Sales_YTD
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY [Group]
-- GRAND TOTAL
UNION ALL
SELECT NULL,NULL, SUM([SalesYTD]) AS Sales_YTD
FROM [AdventureWorks2019].[Sales].[SalesTerritory]

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [CountryRegionCode],[GROUP], SUM([SalesYTD]) AS Sales
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY
GROUPING SETS (
([CountryRegionCode],[Group]),
([GROUP]),
() )

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [GROUP], SUM([SalesYTD]) AS Sales
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY CUBE ([GROUP])

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [GROUP], [CountryRegionCode], SUM([SalesYTD]) AS Sales
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY CUBE ([Group] , [CountryRegionCode])

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [GROUP], [CountryRegionCode], SUM([SalesYTD]) AS Sales
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY CUBE ([Group] , [CountryRegionCode])

GROUP BY CUBE -> ALL POSSIBLE GROUPING COMBINATIONS

SELECT [GROUP], [CountryRegionCode], SUM([SalesYTD])


FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY GROUPING SETS (
( [Group] , [CountryRegionCode]),
([GROUP]),
([CountryRegionCode]),
() )

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [GROUP], [CountryRegionCode], SUM([SalesYTD]) AS Sales
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY ROLLUP (Group] ,[CountryRegionCode])

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Grouping Sets


SELECT [GROUP], [CountryRegionCode], SUM([SalesYTD]) AS Sales
FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY ROLLUP (Group] ,[CountryRegionCode])

EQUAL TO FOLLOWUING GROUPING SETS COMBINATIONS

SELECT [GROUP], [CountryRegionCode], SUM([SalesYTD])


FROM [AdventureWorks2019].[Sales].[SalesTerritory]
GROUP BY GROUPING SETS (
( [Group] , [CountryRegionCode]),
( [GROUP]),
() )

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions

Ranking functions return a ranking value for each row in a partition. Depending on the function that is used, some rows might receive the same value
as other rows. Ranking functions are nondeterministic.
Transact-SQL provides the following ranking functions:
• RANK
• NTILE

• DENSE_RANK
• ROW_NUMBER

Syntax:
RANKING FUNCTION ( ) OVER ( [ partition_by_clause ] order_by_clause )

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


USE [AdventureWorks2019]
GO
CREATE VIEW [Production].[VProductInventory]
AS
SELECT P.[Name] as ProductName
,L.[Name] as LocationName
,Pri.[Quantity]
,P.[ListPrice]
FROM [Production].[ProductInventory] AS PrI
JOIN [Production].[Product] As P ON Pri.[ProductID] = P.[ProductID]
JOIN [Production].[Location] As L ON Pri.[LocationID] = L.[LocationID]
WHERE Pri.[Quantity]*P.[ListPrice] <> 0
GO

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


SELECT TOP 1000 * FROM [Production].[VProductInventory]

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


SELECT [ProductName], [LocationName], [ListPrice],
ROW_NUMBER() OVER (ORDER BY [ListPrice] DESC) AS RN
FROM [Production].[VProductInventory]

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


SELECT ROW_NUMBER() OVER (PARTITION BY LocationName ORDER BY ListPrice DESC ) AS RN,
ProductName, LocationName, ListPrice
FROM Production.VProductInventory

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


SELECT RANK() OVER (ORDER BY Quantity DESC ) AS RNK,
ProductName, LocationName, Quantity
FROM Production.VProductInventory

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


SELECT DENSE_RANK() OVER (ORDER BY Quantity DESC ) AS RNK,
ProductName, LocationName, Quantity
FROM Production.VProductInventory

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


SELECT NTILE(10) OVER (ORDER BY Quantity DESC ) AS GRP,
ProductName, LocationName, Quantity
FROM Production.VProductInventory
WHERE Quantity < 100

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL grouping functions and ranking functions

SQL Ranking functions


SELECT
NTILE(3) OVER (PARTITION BY LocationName ORDER BY Quantity DESC ) AS GRP,
ProductName, LocationName, Quantity
FROM Production.VProductInventory
ORDER BY LocationName DESC

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL XML DATA TYPES

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL XML data-types

• XML stands for eXtensible Markup Language. WHAT FOR? - XML Simplifies Things
• XML was designed to store and transport data. • It simplifies data sharing
• XML is a markup language much like HTML. • It simplifies data transport
• XML was designed to be self-descriptive. • It simplifies platform changes
• Many computer systems contain data in incompatible formats.
Exchanging data between incompatible systems (or upgraded systems) is
a time-consuming task for web developers. Large amounts of data must
The Difference Between XML and HTML
be converted, and incompatible data is often lost.
• XML was designed to carry data - with focus on
• XML stores data in plain text format. This provides a software- and
what data is
hardware-independent way of storing, transporting, and sharing data.
• HTML was designed to display data - with focus on
• XML also makes it easier to expand or upgrade to new operating systems,
how data looks
new applications, or new browsers, without losing data.
• XML tags are not predefined like HTML tags are
• With XML, data can be available to all kinds of "reading machines" like
people, computers, voice machines, news feeds, etc.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL XML data-types

More information you can find here:


https://docs.microsoft.com/ru-ru/sql/relational-databases/system-stored-procedures/sp-xml-preparedocument-transact-sql?view=sql-server-ver15
https://docs.microsoft.com/ru-ru/sql/t-sql/functions/openxml-transact-sql?view=sql-server-ver15

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL XML data-types

More information you can find here:


https://docs.microsoft.com/ru-ru/sql/relational-databases/system-stored-procedures/sp-xml-preparedocument-transact-sql?view=sql-server-ver15
https://docs.microsoft.com/ru-ru/sql/t-sql/functions/openxml-transact-sql?view=sql-server-ver15

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL XML data-types

Microsoft SQL Server supports system stored procedures that are used for SYNTAX:
sp_xml_preparedocument
XML text management. hdoc OUTPUT
[ , xmltext ]
sp_xml_preparedocument reads the XML text provided as input, parses the
text by using the MSXML parser (Msxmlsql.dll), and provides the parsed hdoc (int) - handle to the newly created
document.
document in a state ready for consumption. This parsed document is a tree [ xmltext ] - original XML document.
representation of the various nodes in the XML document: elements,
attributes, text, comments, and so on. SYNTAX:
sp_xml_removedocument
A parsed document is stored in the internal cache of SQL Server.
hdoc
sp_xml_removedocument removes the internal representation of the XML
hdoc (int) - handle to the newly created document.
document specified by the document handle and invalidates the document
handle.

More information you can find here:


https://docs.microsoft.com/ru-ru/sql/relational-databases/system-stored-procedures/sp-xml-preparedocument-transact-sql?view=sql-server-ver15
https://docs.microsoft.com/ru-ru/sql/t-sql/functions/openxml-transact-sql?view=sql-server-ver15

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL XML data-types
USE AdventureWorks2019;

SYNTAX:
-- CREATE TABLE TO STORE XML
OPENROWSET ( { 'provider_name' , { 'datasource' ;
IF OBJECT_ID(N'dbo.TESTXMLwithOpenXML', N'U') IS null 'user_id' ; 'password' | 'provider_string' } , {
<table_or_view> | 'query' } | BULK 'data_file' , {
CREATE TABLE TESTXMLwithOpenXML ( FORMATFILE = 'format_file_path' [ <bulk_options> ] |
SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB } } )
Id INT IDENTITY PRIMARY KEY,
XMLData XML,
CReatedDateTime DATETIME);
SYNTAX:
OPENROWSET (BULK 'path_to_file',
-- INSERT XML DATA FROM FILE SINGLE_BLOB)
.
INSERT INTO TESTXMLwithOpenXML(XMLData, CreatedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK 'C:\books.xml', SINGLE_BLOB) AS x;

More information you can find here:


SELECT * FROM TESTXMLwithOpenXML https://docs.microsoft.com/ru-ru/sql/relational-databases/system-stored-procedures/sp-xml-preparedocument-transact-sql?view=sql-server-ver15
https://docs.microsoft.com/ru-ru/sql/t-sql/functions/openxml-transact-sql?view=sql-server-ver15

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL XML data-types
DECLARE @x xml, @hdoc int

SELECT @x = B

FROM OPENROWSET (BULK 'C:\Books.xml', SINGLE_BLOB) AS Books(B)

--SELECT @x
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x

SELECT * FROM OPENXML (@hdoc, '/catalog/book')


WITH( ID varchar(10) '@id',
Author varchar(100) 'author',
Title varchar(250) 'title',
Genre varchar(50) 'genre',
Price numeric(6,2) 'price',
PublishDate date 'publish_date',
[Description] varchar(max) 'description')

More information you can find here:


EXEC sp_xml_removedocument @hdoc
https://docs.microsoft.com/ru-ru/sql/relational-databases/system-stored-procedures/sp-xml-preparedocument-transact-sql?view=sql-server-ver15
https://docs.microsoft.com/ru-ru/sql/t-sql/functions/openxml-transact-sql?view=sql-server-ver15

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

WHY? - performance and recovery.


• Performance improves when you spread files across multiple disks because you have multiple heads reading and writing, rather than one doing all
the work.
• Filegroups can be backed up and restored separately as well. This enables faster object recovery in the case of a disaster.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

What we need?
• Create filegroups and add files to them
• Create partition function
• Create partition scheme
What is a Filegroup?
A filegroup is a logical structure to group objects in a database.
Don’t confuse filegroups with actual files (.mdf, .ddf, .ndf, .ldf, etc.). You can have multiple filegroups per database. One filegroup will be the
primary. Then, you add additional filegroups. In a filegroup, you can have multiple files.

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


SQL Partitions

Function maps the rows of a table or index Scheme maps the partitions of a
in the current database into partitions partitioned table or index to filegroups in
based on the values of a specified column. current database.

SYNTAX:
SYNTAX:
CREATE PARTITION FUNCTION partition_function_name ( CREATE PARTITION SCHEME partition_scheme_name AS PARTITION
input_parameter_type ) AS RANGE [ LEFT | RIGHT ] FOR VALUES ( [ partition_function_name
boundary_value [ ,...n ] ] ) [ ; ] TO ( { file_group_name} , {…} )

NB: file_group_name must already exist in the database!


LEFT | RIGHT - specifies to which side of each boundary value interval,
left or right, the boundary_value belongs

CONFIDENTIAL | © 2020 EPAM Systems, Inc.


T H A N K S F O R AT T E N D I N G ! Q U E S T I O N S ?

CONFIDENTIAL | © 2020 EPAM Systems, Inc.

You might also like