0% found this document useful (0 votes)
3 views

Sql2

The document provides an overview of SQL basics, focusing on various types of JOIN operations, including INNER, LEFT, RIGHT, and FULL OUTER JOINs, along with their syntax and examples. It also covers additional SQL concepts such as SELF JOIN, CROSS JOIN, EQUI JOIN, NON EQUI JOIN, and NATURAL JOIN. The agenda includes topics like set operations and aggregate functions, aimed at enhancing understanding of SQL for software engineers.

Uploaded by

and311003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Sql2

The document provides an overview of SQL basics, focusing on various types of JOIN operations, including INNER, LEFT, RIGHT, and FULL OUTER JOINs, along with their syntax and examples. It also covers additional SQL concepts such as SELF JOIN, CROSS JOIN, EQUI JOIN, NON EQUI JOIN, and NATURAL JOIN. The agenda includes topics like set operations and aggregate functions, aimed at enhancing understanding of SQL for software engineers.

Uploaded by

and311003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

SQL Basics

Deep Dive Into SQL

Zlata Chernysh
Junior Software Engineer
Tetiana Lampiha
Software Engineer
CONFIDENTIAL | © 2021 EPAM Systems, Inc.
Recommendations

1 M U T E YO U R M I C

2 R A I S E H A N D A S K Q U E ST IO NS

3 D U R AT I O N : 3 H O U R S

4 C O FFEE BRE AK: 15 MINUT ES

5 Q & A A FT E R E A C H MO D ULE

6 T E A M S Q U E S TIO N S C H A NN EL

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 2


Z L ATA C H E R N Y S H

Junior S of tw are Engineer

• EPAM BI Lab 2020 Participant


• EPAM Career Path: Production Project -> DWBI Software
Engineer

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 3


TETIANA LAMPIHA

S of tw are Engineer

• EPAM BI Lab 2019 Participant


• EPAM Career Path: Production Project -> DWBI Software
Engineer

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 4


AGENDA
1 SQL JOINS

2 S E T O P E R AT I O N S

3 A G G R E G AT E F U N C T I O N S

4 G R O U P B Y S T AT E M E N T

5 C A S E S T AT E M E N T

6 S U B Q U ER IES

7 CTE

8 D AT A T Y P E S

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


JOINS

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


JOINS

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
Here are the different types of the JOINs in SQL:
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN

table 1 table 2 table 1 table 2 table 1 table 2 table 1 table 2

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 7


INNER JOIN Keyword

The INNER JOIN keyword selects records that have matching values in
both tables.
INNER JOIN Syntax: JOIN =
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Note: The INNER JOIN keyword selects all rows from both tables as INNER JOIN
long as there is a match between the columns. If there are records in
the “table1” table that do not have matches in “table2", these records
will not be shown!
table 1 table 2
In case inner joins involve more than two tables, it doesn’t matter in
which order the tables appear in the query. The performance and
output of the query would be the same.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 8


INNER JOIN Example
SELECT c.CompanyName, Country, OrderDate, ShipName, Discount, ProductName
FROM dbo.Customers c
The following SQL statement selects companies with their INNER JOIN dbo.Orders o
countries, all order dates within this company, ship name, ON c.CustomerID = o.CustomerID
discount and product name which was bought: INNER JOIN dbo.Shippers s
ON o.ShipVia = s.ShipperID
INNER JOIN dbo.[Order Details] od
ON o.OrderID = od.OrderID
INNER JOIN dbo.Products p
ON od.ProductID = p.ProductID

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 9


LEFT JOIN Keyword

The LEFT JOIN keyword returns all records from the left table (table1),
and the matching records from the right table (table2). The result is
NULL from the right side, if there is no match. LEFT =
LEFT JOIN Syntax:
JOIN
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
LEFT JOIN
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
Note: The LEFT JOIN keyword returns all records from the left table
(table1), even if there are no matches in the right table (table2). table 1 table 2

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 10


RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all records from the right table (table2),
and the matched records from the left table (table1). The result is NULL
from the left side, when there is no match. RIGHT
=
JOIN
RIGHT JOIN Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

RIGHT JOIN
Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
Note: The RIGHT JOIN keyword returns all records from the right table
(table2), even if there are no matches in the left table (table1). table 1 table 2

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 11


LEFT JOIN and RIGHT JOIN Examples
The following SQL statement will select all customers, and any orders
they might have:
SELECT c.CompanyName, Country, OrderDate, ShipName
FROM dbo.Customers c
LEFT JOIN dbo.Orders o
ON c.CustomerID = o.CustomerID;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 12


LEFT JOIN and RIGHT JOIN Examples
The following SQL statement will select all customers, and any orders The following SQL statement will return all customers, and any orders
they might have: they might have:
SELECT c.CompanyName, Country, OrderDate, ShipName SELECT OrderDate, ShipName, c.CompanyName, Country
FROM dbo.Customers c FROM dbo.Orders o
LEFT JOIN dbo.Orders o RIGHT JOIN dbo.Customers c
ON c.CustomerID = o.CustomerID; ON c.CustomerID = o.CustomerID;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 13


FULL JOIN Keyword

The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or right (table2) table records.
Note: FULL OUTER JOIN can potentially return very large result-sets!
FULL =
Tip: FULL OUTER JOIN and FULL JOIN are the same.
JOIN
FULL OUTER JOIN Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name FULL OUTER JOIN
WHERE condition;

Note: The FULL OUTER JOIN keyword returns all matching records from both tables
table 1 table 2
whether the other table matches or not. So, if there are rows in table1 that do not
have matches in table2 or if there are rows in table2 that do not have matches in
table1, those rows will be listed as well.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 14


FULL JOIN

S O M E D A TA B A S E S D O N O T S U P P O R T F U L L O U T E R J O I N ( F O R E X A M P L E , M Y S Q L ) . I N T H E S E C A S E S ,
F U L L O U T E R J O I N S C O U L D B E O B TA I N E D B Y U S I N G U N I O N O F L E F T O U T E R J O I N
AND RIGHT OUTER JOIN.
The following query syntax will return the same result as FULL JOIN:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name

UNION

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 15


FULL JOIN Example

The following SQL statement selects all customers and suppliers:


SELECT C.ContactName, C.Country AS CustomerCountry,
S.Country AS SupplierCountry, S.CompanyName
FROM Customers C
FULL JOIN Suppliers S
ON C.Country = S.Country
ORDER BY C.Country, S.Country

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 16


Types of JOINs

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 17


SELF JOIN

A self JOIN is a regular join, but the table is joined with itself (which is
also called Unary relationships), especially when the table has a FOREIGN
KEY which references its own PRIMARY KEY. To join a table itself means
that each row of the table is combined with itself and with every other
row of the table.

Self JOIN Syntax:

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
T1 and T2 are different table aliases for the same table.

NOTE: You can use INNER JOIN syntax!

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 18


SELF JOIN

A self JOIN is a regular join, but the table is joined with itself (which is The self join can be viewed as a join of two copies of the same table. The
also called Unary relationships), especially when the table has a FOREIGN table is not actually copied, but SQL performs the command as though it
KEY which references its own PRIMARY KEY. To join a table itself means were.
that each row of the table is combined with itself and with every other
The syntax of the command for joining a table to itself is almost same as
row of the table.
that for joining two different tables. To distinguish the column names
Self JOIN Syntax: from one another, aliases for the actual the table name are used, since
both the tables have the same name. Table name aliases are defined in
SELECT column_name(s)
the FROM clause of the SELECT statement.
FROM table1 T1, table1 T2
WHERE condition;
T1 and T2 are different table aliases for the same table.

NOTE: You can use INNER JOIN syntax!

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 19


SELF JOIN

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 20


SELF JOIN

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 21


SELF JOIN

SELECT a.emp_id AS Emp_ID,


a.emp_name AS Employee_Name,
b.emp_id AS Supervisor_ID,
b.emp_name AS Supervisor_Name
FROM employee a, employee b
WHERE a.emp_supv = b.emp_id;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 22


SELF JOIN

SELECT a.emp_id AS Emp_ID,


a.emp_name AS Employee_Name,
b.emp_id AS Supervisor_ID,
b.emp_name AS Supervisor_Name
FROM employee a, employee b
WHERE a.emp_supv = b.emp_id;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 23


SELF JOIN Example

The following query checks whether there are any customers with
the same cities and countries in the table. CustomerID is a primary
key, and it is unique.
SELECT A.ContactName, B.ContactName,
B.City, B.Country
FROM Customers A JOIN Customers B
ON A.CustomerID <> B.CustomerID
AND A.City = B.City
AND A.Country = B.Country
ORDER BY A.Country

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 24


CROSS JOIN

The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table if no WHERE
clause is used along with CROSS JOIN. This kind of result is called as Cartesian Product.

If WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN.
An alternative way of achieving the same result is to use column names separated by commas after
SELECT and mentioning the table names involved, after a FROM clause.

CROSS JOIN Syntax:


SELECT column_name(s)
FROM table1
CROSS JOIN table2
OR:

SELECT column_name(s) CROSS =


FROM table1 JOIN
JOIN table2
OR:
SELECT column_name(s)
FROM table1, table2

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 25


EQUI and NON EQUI JOINS

EQUI JOIN performs a JOIN against equality or matching


column(s) values of the associated tables. An equal sign (=) is
used as comparison operator in the where clause to refer
equality.
You may also perform EQUI JOIN by using JOIN keyword
followed by ON keyword and then specifying names of the
columns along with their associated tables to check equality.

Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name = table2.column_name;

Or:
SELECT * FROM table1 JOIN table2
ON table1.column_name = table2.column_name;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 26


EQUI and NON EQUI JOINS

EQUI JOIN performs a JOIN against equality or matching The SQL NON EQUI JOIN uses comparison operator instead of
column(s) values of the associated tables. An equal sign (=) is the equal sign like >, <, >=, <= along with conditions.
used as comparison operator in the where clause to refer
equality. Syntax:
You may also perform EQUI JOIN by using JOIN keyword SELECT * FROM table_name1, table_name2
followed by ON keyword and then specifying names of the WHERE table_name1.column [> | < | >= | <= ]
columns along with their associated tables to check equality. table_name2.column;

Syntax:
SELECT column_list
FROM table1, table2....
WHERE table1.column_name = table2.column_name;

Or:
SELECT * FROM table1 JOIN table2
ON table1.column_name = table2.column_name;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 27


NATURAL JOIN

N O T E : S Q L S E R V E R D O E S N O T S U P P O R T N AT U R A L J O I N S !

NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that, columns with the same name of associated
tables will appear once only.

Guidelines:
• The associated tables have one or more pairs of identically named columns.
• The columns must be of the same data type.
• Don’t use ON clause in a natural join.

NATURAL JOIN Syntax:


SELECT * FROM table1 NATURAL JOIN table2;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 28


NATURAL JOIN

SELECT * FROM foods NATURAL JOIN company;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 29


SEMI-JOIN AND ANTI-JOIN

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 30


S ET Operations

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


SET Operations

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 32


UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT statements.
• Each SELECT statement within UNION must have the same number of columns.
• The columns must also have similar data types.
• The columns in each SELECT statement must also be in the same order.
UNION Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Note: The column names in the result-set are usually equal to the column names in the first SELECT statement in the UNION.

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


UNION Operator Example

The following SQL statement returns the cities (only distinct values) The following SQL statement returns the cities (duplicate values also)
from both the "Customers" and the "Suppliers" table: from both the "Customers" and the "Suppliers" table:
SELECT City FROM Customers SELECT City FROM Customers
UNION UNION ALL
SELECT City FROM Suppliers SELECT City FROM Suppliers
ORDER BY City; ORDER BY City;
Note: If some customers or suppliers have the same city, each city will
only be listed once, because UNION selects only distinct values.
Use UNION ALL to also select duplicate values!

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 34


INTERSECT Operator
The intersect operator returns only those records that are present in The following SQL statement returns the cities (only distinct values)
both SELECT statements. stored in both the "Customers" and the "Suppliers" table:
Note: The INTERSECT operator is not supported in some databases.
You can use the IN or EXIST IN clause for performing similar
SELECT City FROM Customers
operations.
INTERSECT
INTERSECT Syntax: SELECT City FROM Suppliers
ORDER BY City;
SELECT column_name(s) FROM table1
INTERSECT
SELECT column_name(s) FROM table2;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 35


EXCEPT, MINUS Operators
EXCEPT (MINUS) operator returns only those records that are The following SQL statement returns the cities (only distinct values)
present only in the first SELECT statement. from ONLY "Suppliers" table, which are NOT stored in "Customers"
Note: Most databases support EXCEPT operator. Oracle supports table:
MINUS operator. The EXCEPT/MINUS operator is not supported in
some databases SELECT City FROM Suppliers
EXCEPT Syntax: EXCEPT
SELECT column_name(s) FROM table1 SELECT City FROM Customers
EXCEPT ORDER BY City;
SELECT column_name(s) FROM table2;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 36


Joins VS Unions

In simple terms, joins combine data into new columns. If two tables Unions combine data into new rows. If two tables are “unioned”
are joined together, then the data from the first table is shown in one together, then the data from the first table is in one set of rows, and
set of columns alongside the second table’s column in the same row. the data from the second table in another set. The rows are in the
same result.
JOIN is applicable when the two involved relations have at least one
common attribute. UNION is applicable when the number of columns present in query
are same and the corresponding attributes has the same domain.
Number of columns selected from each table may not be same.
Number of columns selected from each table should be same.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 37


AGGREGATE FUNCTIONS

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


Aggregate Functions

In SQL, you can summarize/aggregate the data using aggregate functions. With these functions, you will be able to
answer questions like:
• What is the maximum value for the some_column_from_the_table? or
• What are the minimum values of some_column_from_the_table with respect to
another_column_from_the_table?
Aggregate functions include COUNT(), MIN(), MAX(), SUM() and AVG() functions.
An aggregate function performs a calculation on a set of values, and returns a single value. These functions do not
take NULL values into consideration.
N O T E : E X C E P T F O R C O U N T ( * ) , A G G R E G AT E F U N C T I O N S I G N O R E N U L L VA L U E S .

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 39


MIN() and MAX() Functions

The MIN() function returns the smallest value of the MIN() Example:
selected column.
The following SQL statement finds the price of the cheapest product:
The MAX() function returns the largest value of the SELECT MIN(UnitPrice) AS SmallestPrice
selected column.
FROM Products;
MIN() Syntax:
SELECT MIN(column_name) MAX() Example:
FROM table_name The following SQL statement finds the price of the most expensive
WHERE condition; product:
MAX() Syntax: SELECT MAX(UnitPrice) AS LargestPrice

SELECT MAX(column_name) FROM Products;

FROM table_name
WHERE condition;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 40


COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that matches a N O T E : N U L L VA LU E S A R E I G N O R E D !


specified criterion.
The AVG() function returns the average value of a numeric column.
COUNT() Syntax:
AVG() Syntax
SELECT COUNT(column_name)
SELECT AVG(column_name)
FROM table_name
FROM table_name
WHERE condition;
WHERE condition;
You can also use COUNT(*). It returns the amount of all rows in the
The SUM() function returns the total sum of a numeric column.
table:
SUM() Syntax
SELECT COUNT(*)
FROM table_name SELECT SUM(column_name)

WHERE condition; FROM table_name


WHERE condition;
To receive the number of distinct values in the column, use:
SELECT COUNT(DISTINCT column_name)
FROM table_name
WHERE condition;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 41


COUNT(), AVG() and SUM() Functions Examples

COUNT() examples AVG() Example


The following query returns the number of rows in “Products” table: The following SQL statement finds the average price of all products:
SELECT COUNT(*) AS rows_count FROM Products; SELECT AVG(UnitPrice) AS average_price FROM Products;

Amount of non-NULL values in FirstName column: The following SQL statement finds the sum of the "Quantity" fields in
SELECT COUNT(FirstName) AS first_name_count FROM Employees; the "Order Details" table:
SELECT SUM(Quantity) AS quantity_sum FROM [Order Details];

Amount of distinct non-NULL values in FirstName column:


SELECT COUNT(DISTINCT City) AS distinct_cities FROM
employees;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 42


GROUP BY

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 43


GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each co untry“ .
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
The GROUP BY clause is used with the SELECT statement to make a group of rows based on the values of a specific column or exp ression. The SQL
AGGREGATE functions can be used to get summary information for every group and in this case they are applied to an individual group.
GROUP BY Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 44


GROUP BY Example

The following SQL statement lists the number of orders sent by each shipper:
SELECT Shippers.CompanyName, Phone,
COUNT(Orders.OrderID) AS NumberOfOrders,
SUM(Freight) AS SumPayment,
AVG(Freight) AS AveragePayment
FROM Orders
LEFT JOIN Shippers
ON Orders.ShipVia = Shippers.ShipperID
GROUP BY CompanyName, Phone;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 45


HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
The WHERE clause is used to retrieve rows based on a certain condition, but it can not be applied to grouped result.
SQL HAVING clause specifies a search condition for a group or an aggregate. HAVING is usually used in a GROUP BY clause, but even if you are not
using GROUP BY clause, you can use HAVING to function like a WHERE clause.
HAVING Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 46


WHERE VS HAVING
WHERE HAVING

WHERE Clause is used to filter the records from the table based on the specified HAVING Clause is used to filter record from the groups based on the specified
condition condition.

WHERE Clause can be used without GROUP BY Clause HAVING Clause cannot be used without GROUP BY Clause

WHERE Clause cannot contain aggregate function HAVING Clause can contain aggregate function

WHERE Clause can be used with SELECT, UPDATE, DELETE statement. HAVING Clause can only be used with SELECT statement.

WHERE Clause is used before GROUP BY Clause HAVING Clause is used after GROUP BY Clause

WHERE Clause implements in row operations HAVING Clause implements in column operation

WHERE Clause is used with single row function like UPPER, LOWER etc. HAVING Clause is used with multiple row function like SUM, COUNT etc.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 47


HAVING Clause Example

The following SQL statement lists if the employees "Davolio" or "Fuller" have registered more than 25 orders:
SELECT Employees.LastName,
COUNT(Orders.OrderID) AS NumberOfOrders
FROM Orders
INNER JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID
WHERE LastName = 'Davolio' OR LastName = 'Fuller’
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 25;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 48


CASE Statement

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


CASE Statement

The CASE statement goes through conditions and returns a value when the first condition is met (like an IF-THEN-ELSE statement). So, once a
condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE cl ause.
If there is no ELSE part and no conditions are true, it returns NULL.
CASE expressions can be used in SELECT, INSERT, UPDATE, DELETE, functions.
Simple CASE: Checks one expression against multiple values. Allows only an equality check.
Searched CASE: Doesn’t have expression after the CASE statement. Allows evaluating complex expression in the WHEN clauses.
Syntax (searched CASE):
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 50


CASE Example

Simple CASE Example:

SELECT DISTINCT OrderID,


CASE Discount
WHEN 0 THEN 'No’
ELSE 'Yes’
END AS Discount
FROM [Order Details];

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 51


CASE Example

Searched CASE Example. The following SQL goes through conditions


and returns a value when the first condition is met:
SELECT OrderID, Quantity,
CASE WHEN Quantity > 30
THEN 'The quantity is greater than 30’
WHEN Quantity = 30 THEN 'The quantity is 30’
ELSE 'The quantity is under 30’
END AS QuantityText
FROM [Order Details];

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 52


CASE Example

CASE in ORDER BY. The following SQL will order the


customers by City. However, if City is NULL, then order by
Country:
SELECT CompanyName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 53


SUBQUERIES

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 54


Subquery

A subquery is a SQL query nested inside a larger query.


A subquery may occur in :
• A SELECT clause
• A FROM clause
• A WHERE clause
The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery.
You can use the comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator, such as IN, ANY, or ALL.
A subquery is also called an inner query or inner select, while the statement containing a subquery is also called an outer q uery or outer select.
The inner query executes first before its parent query so that the results of an inner query can be passed to the outer query.
You can use a subquery to perform the following tasks:
• Compare an expression to the result of the query.
• Determine if an expression is included in the results of the query.
• Check whether the query selects any rows.

SU B QU ERI ES CA N`T B E U SED I N ORDER BY A ND GROU P BY CLA U SES.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 55


Types of Subqueries

By output:
• Single row subquery : Returns zero or one row.
• Multiple row subquery : Returns one or more rows.
• Multiple column subqueries : Returns one or more columns.
By connection to outer query:
• Correlated subqueries : Reference one or more columns in the outer SQL statement. The subquery is known as
a correlated subquery because the subquery is related to the outer SQL statement.
• Non-correlated (self-contained) subqueries: Independent of the outer query
Nested subqueries : Subqueries are placed within another subquery.

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 56


Subquery Examples

Subquery in select list (correlated subquery):

SELECT ProductName,
UnitPrice,
(SELECT MAX(Discount) FROM [Order Details] od
WHERE od.ProductID = p.ProductID) AS MaxDiscount
FROM Products p

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 57


Subquery Examples

Quantified predicate subquery with IN. This example generates a list Quantified predicate subquery with NOT IN. This example generates a
of ships that had orders from London customers: list of ships that had NO orders from London customers:
SELECT DISTINCT ShipName FROM Orders SELECT DISTINCT ShipName FROM Orders
WHERE CustomerID IN WHERE CustomerID NOT IN
(SELECT CustomerID FROM Customers WHERE City = 'London'); (SELECT CustomerID FROM Customers WHERE City = 'London');

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 58


Subquery Examples

Subquery in FROM (self-contained, table-valued) clause:


SELECT Country FROM (
SELECT COUNT(CustomerID) QuantityOfCustomers, Country
FROM Customers
GROUP BY Country
)c
WHERE QuantityOfCustomers >=10
ORDER BY 1 DESC;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 59


EXISTS Operator

The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.

EXISTS Syntax:

SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 60


EXIST Operator Examples

The following SQL statement returns TRUE and lists the


suppliers with a product price less than 20:
SELECT CompanyName
FROM Suppliers
WHERE EXISTS
(SELECT ProductName FROM Products WHERE
Products.SupplierID = Suppliers.supplierID AND UnitPrice< 20);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 61


EXIST Operator Examples

The following SQL statement returns TRUE and lists the The following SQL statement returns TRUE and lists the
suppliers with a product price less than 20: suppliers with a product price equal to 22:
SELECT CompanyName SELECT CompanyName
FROM Suppliers FROM Suppliers
WHERE EXISTS WHERE EXISTS
(SELECT ProductName FROM Products WHERE (SELECT ProductName FROM Products WHERE
Products.SupplierID = Suppliers.supplierID AND UnitPrice< 20); Products.SupplierID = Suppliers.supplierID AND UnitPrice = 22);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 62


ANY Operator

The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.
The ANY operator:
• returns a boolean value as a result
• returns TRUE if ANY of the subquery values meet the condition
ANY means that the condition will be true if the operation is true for any of the values in the range.
ANY Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 63


ALL Operator

The ALL operator:


• returns a boolean value as a result
• returns TRUE if ALL of the subquery values meet the condition
• is used with WHERE and HAVING statements
ALL means that the condition will be true only if the operation is true for all values in the range.
ALL Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 64


ANY and ALL Examples

The following SQL statement lists the ProductName if it


finds ANY records in the OrderDetails table has Quantity
equal to 10 (this will return TRUE because the Quantity
column has some values of 10):
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM [Order Details]
WHERE Quantity = 10);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 65


ANY and ALL Examples

The following SQL statement lists the ProductName if it The following SQL statement lists the ProductName if it
finds ANY records in the OrderDetails table has Quantity finds ALL records in the OrderDetails table don`t have
equal to 10 (this will return TRUE because the Quantity Quantity equal to 10 (this will return TRUE because the
column has some values of 10): Quantity column has some values of 10):
SELECT ProductName SELECT ProductName
FROM Products FROM Products
WHERE ProductID = ANY WHERE ProductID <> ALL
(SELECT ProductID (SELECT ProductID
FROM [Order Details] FROM [Order Details]
WHERE Quantity = 10); WHERE Quantity = 10);

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 66


C ommon Table Expression
(CTE)

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 67


CTE Definition and Syntax
A CTE (Common Table Expression) defines a temporary result set which you Syntax:
can then use in SELECT, INSERT, UPDATE, or DELETE statement.
WITH expression_name[(column_name [,...])]
AS
How to create a CTE: (CTE_definition)

• Initiate a CTE using “WITH” SQL_statement;

• Provide a name for the result soon-to-be defined query


Multiple CTE in one statement syntax:
• After assigning a name, follow with “AS”
WITH CTE1
• Specify column names (optional step)
AS (CTE_definition1),
• Define the query to produce the desired result set
CTE2
• If multiple CTEs are required, initiate each subsequent expression with a
AS (CTE_definition2)
comma and repeat steps 2-4.
SQL Stamenent;
• Reference the above-defined CTE(s) in a subsequent query

NOTE:
1. A SQL CTE HAS A NAME
2. CTE’S GONE WHEN THE SELECT IS DONE
3. YOU CAN’T REUSE IT
4. YOU CAN MAKE YOUR CODE MORE READABLE

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 68


CTE Examples
The basic usage of CTE and SELECT statement example Multiple CTEs in the same query
WITH Employee_CTE (EmployeeNumber, Title) WITH one (x)
AS AS (SELECT 1 AS x),
( Corres ponding
Col umns CTE two (y)
SELECT NationalIDNumber, JobTitle AS (SELECT x+1 AS y FROM one),
FROM HumanResources.Employee three (z)
) AS (SELECT y+1 AS z FROM two)
SELECT EmployeeNumber, Title SELECT one.x, two.y, three.z
Query
FROM Employee_CTE Using CTE FROM one
CROSS JOIN two
CROSS JOIN three;

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 69


Recursive CTE

A recursive CTE is a common table expression that references itself. Recursive CTE example that counts from 1 to 50:
WITH cte
The general syntax for a recursive CTE is: AS (
WITH cte_name (column1, column2, …) SELECT 1 AS n -- anchor member (Query definition)
AS UNION ALL
(
SELECT n + 1 -- recursive member (Query definition referencing CTE)
cte_query_definition -- Anchor member
FROM cte
UNION ALL
WHERE n < 50 -- termination check
cte_query_definition -- Recursive member; references cte_name.
) )

-- Statement using the CTE SELECT n -- invocation (statement using CTE)


SELECT * FROM cte;
FROM cte_name

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 70


S QL S erver Data Types

CONFIDENTIAL | © 2021 EPAM Systems, Inc.


SQL Data Types

In SQL Server, each column, local variable, expression, and parameter has a related data type. A data type is an attribute
that specifies the type of data that the object can hold: integer data, character data, monetary data, date and time data,
binary strings, and so on.

Data types in SQL Server are organized into the following categories:

1) Exact numerics
2) Approximate numerics
3) Date and time
4) Character strings
5) Unicode character strings
6) Binary strings
7) Other data types

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 72


String Data Types

Data type Description Max size Storage


char(n) Fixed width character string 8,000 characters Defined width
varchar(n) Variable width character string 8,000 characters 2 bytes + number of chars

varchar(max) Variable width character string 1,073,741,824 characters 2 bytes + number of chars

text Variable width character string 2GB of text data 4 bytes + number of chars

nchar Fixed width Unicode string 4,000 characters Defined width x 2


nvarchar 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

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 73


Numeric Data Types

Data type Description Storage


bit Integer that can be 0, 1, or NULL
tinyint Allows whole numbers from 0 to 255 1 byte
smallint Allows whole numbers between -32,768 and 32,767 2 bytes
int Allows whole numbers between -2,147,483,648 and 2,147,483,647 4 bytes
bigint Allows whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 8 bytes
decimal(p,s) Fixed precision and scale numbers.Allows numbers from -10^38 +1 to 10^38 –1. 5-17 bytes
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the
decimal point). p must be a value from 1 to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p.
Default value is 0
numeric(p,s) Fixed precision and scale numbers.Allows numbers from -10^38 +1 to 10^38 –1. 5-17 bytes
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the
decimal point). p must be a value from 1 to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p.
Default value is 0
smallmoney Monetary data from -214,748.3648 to 214,748.3647 4 bytes
money Monetary data from -922,337,203,685,477.5808 to 922,337,203,685,477.5807 8 bytes
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308.The n parameter indicates whether the field should hold 4 or 8 4 or 8 bytes
bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value of n is 53.

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

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 74


Date and Time Data Types

Data type Description Storage

datetime From January 1, 1753 to December 31, 9999 with an accuracy of 3.33 milliseconds 8 bytes

datetime2 From January 1, 0001 to December 31, 9999 with an accuracy of 100 nanoseconds 6-8 bytes

smalldatetime From January 1, 1900 to June 6, 2079 with an accuracy of 1 minute 4 bytes

date Store a date only. From January 1, 0001 to December 31, 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

timestamp Stores a unique number that gets updated every time a row gets created or modified. The
timestamp value is based upon an internal clock and does not correspond to real time. Each table
may have only one timestamp variable

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 75


Date and Time Data Types Example

SELECT CONVERT (date, SYSDATETIMEOFFSET()) AS 'date',


CONVERT (time, SYSDATETIMEOFFSET()) AS 'time',
CONVERT (datetime , SYSDATETIMEOFFSET()) AS datetime,
CONVERT (datetime2, SYSDATETIMEOFFSET()) AS 'datetime2',
CONVERT (datetimeoffset , SYSDATETIMEOFFSET()) AS datetimeoffset,
CONVERT (smalldatetime , SYSDATETIMEOFFSET()) AS smalldatetime

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 76


Other Data Types

Data type Description

sql_variant Stores up to 8,000 bytes of data of various data types, except text, ntext, and
timestamp

uniqueidentifier Stores a globally unique identifier (GUID)

xml Stores XML formatted data. Maximum 2GB

cursor Stores a reference to a cursor used for database operations

table Stores a result-set for later processing

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 77


THANKS FOR ATTENDING!
QUESTIONS?

CONFIDENTIAL | © 2021 EPAM Systems, Inc. 78

You might also like