2 Data Processing-DBandSQL
2 Data Processing-DBandSQL
processing"
Submodule “T-SQL"
DDL, DML. CRUD. Store procedure language
DDL
DML
CRUD
Indexers
User-definded functions
Procedures
Triggers
CONFIDENTIAL 2
T-SQL
BD object Description
Tables Tables are database objects that contain all the data in a database.
Views A view is a virtual table whose contents are defined by a query. Like a table, a view
consists of a set of named columns and rows of data.
Indexes Speed up data access operations, indexes are generated based on one or more fields
Triggers Triggers is a special type of stored procedure that automatically takes effect when a
DML event takes place that affects the table.
Stored A stored procedure in SQL Server is a group of one or more Transact-SQL statements
procedures
User-defined User-defined functions are routines that accept parameters, perform an action, such as
functions a complex calculation, and return the result of that action as a value.
Constraints Constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table.
CONFIDENTIAL 3
DDL
CONFIDENTIAL 4
Define DB Objects
Besides working with data, you can also work with database objects using CREATE, DROP and ALTER
statements.
CREATE statement creates tables, relationships between tables and other database objects
CONFIDENTIAL 5
Define DB Objects
Constrain declaration
A table can contain only one PRIMARY KEY constraint.
All columns defined within a PRIMARY KEY constraint must be defined as NOT NULL. If nullability is not
specified, all columns participating in a PRIMARY KEY constraint have their nullability set to NOT NULL.
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table
is created with the ALTER TABLE statement.
CONFIDENTIAL 6
Define DB objects
constraint fk2_organization
foreign key (codformorg) references refformorg (cod)
on delete set default
on update cascade
…
CONFIDENTIAL 7
Define DB objects
ALTER statement modifies existing database objects: tables, relationships between tables, etc
CONFIDENTIAL 8
Define DB objects
DROP statement deletes already existing database objects or their components: tables, relationships
between tables, table fields, etc.
✓ removing tables:
CONFIDENTIAL 9
Define DB objects
CONFIDENTIAL 10
Define DB objects
A view is a virtual table whose contents are defined by a query. Like a table, a view consists of a set of
named columns and rows of data. The rows and columns of data come from tables referenced in the query
defining the view and are produced dynamically when the view is referenced.
The query that defines the view can be from one or more tables or from other views in the current or other
databases
You can modify the data of the base table through the view as long as the following conditions are met:
✓ Any changes, including UPDATE, INSERT, and DELETE statements, must refer to columns of only one base
table.
✓ Columns to be modified in the view must directly reference the column data of the base table. Columns
cannot be formed in any other way, including:
using an aggregate function: for example, AVG, COUNT, SUM, MIN, MAX , etc;
based on calculation that include several columns.
columns formed using the UNION, UNION ALL, CROSSJOIN and other similar operators
CONFIDENTIAL 11
Define DB objects
CONFIDENTIAL 12
DML
CONFIDENTIAL 13
Selecting and Filtering data
Numeric types (int – bigint – bit – decimal – smallint – money – smallmoney – tinyint float – real)
Strings (Character: char – varchar – text Unicode character: nchar – nvarchar - ntext)
Data and Time (Time (12:35:29. 1234567), Date (2007-05-08), Smalldatetime (2007-05-08 12:35:00),
Datetime (2007-05-08 12:35:29.123) etc.)
Other (cursor, table, timestamp and more)
https://docs.microsoft.com/en-us/sql/t-sql/data-types/numeric-types?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/t-sql/data-types/date-and-time-types?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/t-sql/data-types/string-and-binary-types?view=sql-server-ver15
CONFIDENTIAL 14
Selecting and Filtering data
Numeric
Date and time
date – time – datetime – datetime2
int – bigint – bit – decimal –
– datetimeoffset - smalldatetime
smallint – money – smallmoney –
tinyint
Binary strings
float – real binary– varbinary - image
Strings
Character: Other
char – varchar – text cursor – timestamp – table
Unicode character: …and more
nchar – nvarchar - ntext
CONFIDENTIAL 15
Selecting and Filtering data
Char vs varchar:
Type1 – 3
Type2 - 5
CONFIDENTIAL 16
Selecting and Filtering data
✓ Use char when the sizes of the column data entries are consistent.
✓ Use varchar when the sizes of the column data entries vary considerably.
✓ Use varchar(max) when the sizes of the column data entries vary considerably, and the string length
might exceed 8,000 bytes.
✓ If using a lower version of the SQL Server Database Engine, consider using the
Unicode nchar or nvarchar data types to minimize character conversion issues.
CONFIDENTIAL 17
Selecting and Filtering data
--result
--@date @datetime
------------ -----------------------
--2025-12-10 2025-12-10 00:00:00.000
CONFIDENTIAL 18
Selecting and Filtering data
CONFIDENTIAL 19
Selecting and Filtering data
Almost always, a SELECT query returns a dataset as a relation, and this is an important feature that should
not be forgotten, since you can perform the same actions on a result set as on a regular table.
CityID CityName
Select CityID,CityName From Cities 1 Kyiv
where CountryID = 1 2 Lviv
3 Odessa
4 Dnipro
CONFIDENTIAL 20
Selecting and Filtering data
Instead of specifying the specific names of the fields that you want to select, you can specify an
abbreviation:
CountryID CountryName
Select * From Countries
1 Ukraine
2 Belarus
3 Spain
4 USA
CONFIDENTIAL 21
Selecting and Filtering data
Using DISTINCT
In some cases, you may have duplication in the result set, for example, if you modify the previous
statement:
CONFIDENTIAL 22
Selecting and Filtering data
CountryName
Select DISTINCT
case CountryID 1 Ukraine
when 1 then ‘Ukraine' 2 Belarus
… 3 Spain
CONFIDENTIAL 23
Selecting and Filtering data
Data tables can contain many records, millions of records. It is very rare to select all records with a regular
SELECT.
In most cases, you will select data following some condition. The WHERE clause allows you to implement
such a selection:
CONFIDENTIAL 24
Selecting and Filtering data
✓ Arithmetic +, -, *, /, %
✓ Comparing =, >, <, <> (not equal to), ! (not), >= , <=
✓ Logical AND, OR, NOT
✓ Concatenation +
SELECT EmployeeKey, LastName FROM DimEmployee
WHERE (EmployeeKey <= 500) AND (LastName LIKE '%Smi%’) AND (FirstName
LIKE '%A%’);
CONFIDENTIAL 25
Selecting and Filtering data
CONFIDENTIAL 26
Selecting and Filtering data
Matching comparison, more, less:
CONFIDENTIAL 27
Selecting and Filtering data
Range entry (BETWEEN):
CONFIDENTIAL 28
Selecting and Filtering data
Entering a specific set (IN):
CONFIDENTIAL 29
Selecting and Filtering data
Pattern matching (LIKE):
CONFIDENTIAL 30
Selecting and Filtering data
Like - % , _ , escape
CONFIDENTIAL 31
Selecting and Filtering data
NULL values:
✓ Comparing NULL to any other value (as well as NULL) will return UNKNOWN
✓ You can get records containing NULL using the IS [NOT] NULL operator
CONFIDENTIAL 32
Selecting and Filtering data
Checking for NULL
CONFIDENTIAL 33
Selecting and Filtering data
Sorting ORDER BY:
The result set can be sorted before further use. This is done using the ORDER BY operator:
CONFIDENTIAL 34
Selecting and Filtering data
Using ORDER BY:
By default, data is sorted in ascending order:
Select DepartmentName,DateOfCreation from Departments
order by DepartmentName
DepartmentName DateOfCreation
% Store 2011-05-01
A Store 2011-02-01
B Store 2011-01-11
HR 2011-01-01
CONFIDENTIAL 35
Grouping and Aggregating data
Functions
AVG() STDEVP()
MIN() VAR()
MAX() STDEV()
COUNT() SUM()
GROUPING() VARP()
CHECKSUM_AGG() COUNT_BIG()
CONFIDENTIAL 36
Grouping and Aggregating data
Aggregate functions
In the first column, the average price for the Price column is calculated, in the second, the quantity of all
goods:
SELECT
AVG(pr.Price), SUM(pr.Count) FROM Products AS pr
81.6666 7
CONFIDENTIAL 37
Grouping and Aggregating data
Result sets can be grouped by some criterion
CONFIDENTIAL 38
Grouping and Aggregating data
Grouping by multiple parameters
CONFIDENTIAL 39
Grouping and Aggregating data
Grouping with filtering
Count the number of offices of all types in selected cities with more than 2 offices:
SELECT …
GROUP BY dp.DepartmentName, ct.CityName
HAVING COUNT(dp.DepartmentName) > 1
CONFIDENTIAL 40
Grouping and Aggregating data
Having vs where
SELECT ProductID
FROM SalesOrderDetail
WHERE UnitPrice < 25.00
GROUP BY ProductID
HAVING AVG(OrderQty) > 5
ORDER BY ProductID;
CONFIDENTIAL 41
Grouping and Aggregating data
Having vs where
CONFIDENTIAL 42
Grouping and Aggregating data
Having vs where
CONFIDENTIAL 43
Joining
CONFIDENTIAL 44
Joining
Cartesian product
We got the Cartesian product - a combination of all records from the left and right tables. This set must be
correctly connected (filtered).
CONFIDENTIAL 45
Joining
Joining by comparison
The easiest way to do a query like this is to use the WHERE clause:
Name Name
Mike Production
John TC
Paul Maintenance
Jessie Production
There are 4 records in total, since 2 employees have a DepartmentID value of NULL.
Filtering the set manually is an outdated approach.
CONFIDENTIAL 46
Joining
Joins
Instead of manual filtering, you should use one of the built-in join types:
OUTER JOIN
FULL, LEFT, RIGHT
INNER JOIN
CROSS JOIN
SELF JOIN
CONFIDENTIAL 47
Joining
FULL LEFT JOIN (LEFT JOIN)
Returns possible combinations from the right table for each record on the left (including the NULL value)
that satisfy the selection condition:
SELECT Empl.Name, Depts.Name from Empl
LEFT JOIN Depts ON Empl.DepartmentID = Depts.ID
In the resulting set, we can see even those employees who do not have a specified department:
Name Name
Mike Production
John TC
Eric NULL
Paul Maintenance
Jessie Production
Jonny NULL
CONFIDENTIAL 48
Joining
FULL RIGHT JOIN (RIGHT JOIN)
Returns all possible combinations from the left table for each record on the right (including the NULL value)
that satisfy the selection condition
SELECT Empl.Name, Depts.Name from Empl
RIGHT JOIN Depts ON Empl.DepartmentID = Depts.ID
We have a “Secret” department, to which no employee is assigned, and now we see it:
Name Name
Mike Production
Jessie Production
John TC
Paul Maintenance
NULL Secret
CONFIDENTIAL 49
Joining
FULL OUTER JOIN (FULL JOIN)
Name Name
Mike Production
John TC
Eric NULL
Paul Maintenance
Jessie Production
Jonny NULL
NULL Secret
CONFIDENTIAL 50
Joining
INNER JOIN (JOIN)
Name Name
Mike Production
John TC
Paul Maintenance
Jessie Production
CONFIDENTIAL 51
Joining
CROSS JOIN
CONFIDENTIAL 52
Joining
SELF JOIN
CONFIDENTIAL 53
Subqueries
Queries can contain other queries inside. Such nested queries named subqueries.
This query will return all items with price above the average one:
CONFIDENTIAL 54
Subqueries
You can also use different conditions for filtering in form of subqueries.
This query will return all items with position ID which exist in the list of positions with ID above 1:
CONFIDENTIAL 55
Subqueries
Select dp.DepartmentName,
(Select CityName from Cities where CityID = dp.CityID)
from Departments as dp
CONFIDENTIAL 56
Subqueries
This query finds the prices of all mountain bike products, their average price, and the difference between the
price of each mountain bike and the average price.:
CONFIDENTIAL 57
Subqueries
The following query finds the names of all products that are in the Wheels subcategory:
CONFIDENTIAL 58
Correlated Subqueries
Many queries can be evaluated by executing the subquery once and substituting the resulting value or
values into the WHERE clause of the outer query. In queries that include a correlated subquery (also known
as a repeating subquery), the subquery depends on the outer query for its values. This means that the
subquery is executed repeatedly, once for each row that might be selected by the outer query.
CONFIDENTIAL 59
Correlated Subqueries
The subquery in this statement cannot be evaluated independently of the outer query. It needs a value for
Employee.BusinessEntityID, but this value changes as SQL Server examines different rows in Employee.
This query retrieves one instance of each employee's first and last name for which the bonus in the
SalesPerson table is 5000 and for which the employee identification numbers match in the Employee and
SalesPerson tables:
CONFIDENTIAL 60
Correlated Subqueries
CONFIDENTIAL 61
CRUD
CONFIDENTIAL 62
CRUD
In addition to reading data, very often you have to insert new data, update existing records, as well as
delete data.
✓ INSERT
✓ UPDATE
✓ DELETE
CONFIDENTIAL 63
CRUD
CONFIDENTIAL 64
CRUD
Statement INSERT
INSERT INTO Products (Name, Price, Count)
VALUES ('ProductN','12,00’,5)
-- or
INSERT INTO Products
VALUES ('ProductN','12,00',5)
TABLE Products (
ID int IDENTITY(1,1) NOT NULL,
Name nvarchar(50) NOT NULL,
CONFIDENTIAL 65
CRUD
CONFIDENTIAL 66
CRUD
Statement DELETE
To delete one row:
DELETE FROM OldProducts WHERE ID='1'
To clear table:
DELETE FROM OldProducts
CONFIDENTIAL 67
CRUD
CONFIDENTIAL 68
CRUD
Statement UPDATE
Update the name of the department “HR”, replacing it with “Human Resources” :
UPDATE Departments
SET
DepartmentName = 'Human Resources'
WHERE DepartmentName = 'HR'
CONFIDENTIAL 69
CRUD
UPDATE HumanResources.Employee
SET VacationHours = VacationHours + 8
CONFIDENTIAL 70
Indexes
Indexes are special lookup tables that the database search engine can use to speed up data retrieval.
n index in a database is very similar to an index in the back of a book.
For example, if you want to reference all pages in a book that discusses a certain topic, you first refer to
the index, which lists all the topics alphabetically and are then referred to one or more specific page
numbers.
When there are thousands of records in a table, retrieving information will take a long time. Therefore
indexes are created on columns which are accessed frequently, so that the information can be retrieved
quickly.
An index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with
the UPDATE and the INSERT statements. Indexes can be created or dropped with no effect on the data.
Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify
the table and which column or columns to index, and to indicate whether the index is in an ascending or
descending order.
CONFIDENTIAL 71
Indexes
A clustered index alters the way that the rows are physically
stored. When you create a clustered index on a column (or a
number of columns), the SQL server sorts the table’s rows by
that column(s).
As a result, there can be only one clustered index on a table or
view.
CONFIDENTIAL 72
Indexes
Creating index
Creating non-clustered index on the primary key:
CREATE TABLE Names(
idName int IDENTITY(1,1),
vcName varchar(50),
CONSTRAINT PK_guid PRIMARY KEY NONCLUSTERED (idName)
)
Creating index
Creating clustered index on the not key attribute:
CREATE CLUSTERED INDEX I_CL_vcName
ON TestTable(vcName)
CONFIDENTIAL 74
User-defined functions
Like functions in programming languages, SQL Server user-defined functions are routines that accept
parameters, perform an action, such as a complex calculation, and return the result of that action as a
value. The return value can either be a single scalar value or a result set.
CONFIDENTIAL 75
User-defined functions
CONFIDENTIAL 76
User-defined functions
CONFIDENTIAL 78
User-defined functions
CONFIDENTIAL 79
User-defined functions
-- Look at continuation
CONFIDENTIAL 80
User-defined functions
BEGIN
-- CTE name and columns
WITH EMP_cte(EmployeeID, OrganizationNode, FirstName, LastName, JobTitle,
RecursionLevel)
AS (
SELECT ...
)
-- copy the required columns to the result of the function
INSERT @retFindReports
SELECT EmployeeID, FirstName, LastName, JobTitle, RecursionLevel
FROM EMP_cte
RETURN
END;
GO
SELECT EmployeeID, FirstName, LastName, JobTitle, RecursionLevel FROM
ufn_FindReports(1);
CONFIDENTIAL 81
Stored procedures
Stored Procedure in SQL Server can be defined as the set of logically group of SQL statement which are
grouped to perform a specific task. The main benefit of using a stored procedure is that it increases the
performance of the database
CONFIDENTIAL 82
Stored procedures
CONFIDENTIAL 83
Stored procedures
CONFIDENTIAL 84
Stored procedures
CONFIDENTIAL 85
Stored procedures
CONFIDENTIAL 86
User-defined functions vs Stored procedures
CONFIDENTIAL 87
Triggers
DML triggers is a special type of stored procedure that automatically takes effect when a data
manipulation language (DML) event takes place that affects the table or view defined in the trigger.
DML events include INSERT, UPDATE, or DELETE statements. DML triggers can be used to enforce business
rules and data integrity, query other tables, and include complex Transact-SQL statements. The trigger and
the statement that fires it are treated as a single transaction, which can be rolled back from within the
trigger. If a severe error is detected (for example, insufficient disk space), the entire transaction
automatically rolls back.
DML triggers are similar to constraints in that they can enforce entity integrity or domain integrity. In
general, entity integrity should always be enforced at the lowest level by indexes that are part of PRIMARY
KEY and UNIQUE constraints or are created independently of constraints. Domain integrity should be
enforced through CHECK constraints, and referential integrity (RI) should be enforced through FOREIGN
KEY constraints. DML triggers are most useful when the features supported by constraints cannot meet
the functional needs of the application.
CONFIDENTIAL 88
Triggers
There are three action query types that you use in SQL which are INSERT, UPDATE and DELETE. So, there
are three types of triggers and hybrids that come from mixing and matching the events and timings that
fire them.
After Triggers
These triggers are executed after an action such as Insert, Update or Delete is performed.
Instead of Triggers
These triggers are executed instead of any of the Insert, Update or Delete operations. For example, let’s
say you write an Instead of Trigger for Delete operation, then whenever a Delete is performed the Trigger
will be executed first and if the Trigger deletes record then only the record will be deleted.
CONFIDENTIAL 89
Triggers
Trigger to the Goods table for insertion. Rolls back an insert transaction if the item ID is greater than 100:
CREATE TRIGGER Trigger_insert_1
ON Goods
FOR INSERT
AS
IF @@ROWCOUNT=1
BEGIN
SET NOCOUNT ON
DECLARE @id INT, @name VARCHAR(20)
SELECT @id=ID, @name=Name FROM inserted
if @id > 100
BEGIN
ROLLBACK TRAN
PRINT 'good ''' + @name + ''' can''t be insert'
END
END
CONFIDENTIAL 90
Triggers
Trigger to the Goods table for insertion. Rolls back an insert transaction if the item ID is greater than 100:
CONFIDENTIAL 91
Triggers
-- Look at continuation
CONFIDENTIAL 92
Triggers
if @Operation = 'unknown'
SET @Operation = 'update'
• https://www.w3schools.com/sql/
• https://docs.microsoft.com/en-us/sql/?view=sql-server-ver15
CONFIDENTIAL 94
Q&A
CONFIDENTIAL
CONFIDENTIAL 32