Sql2
Sql2
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
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
S of tw are Engineer
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
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
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.
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
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
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.
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
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.
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
T1 and T2 are different table aliases for the same table.
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.
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
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.
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;
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;
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.
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.
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!
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.
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 .
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
FROM table_name
WHERE condition;
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];
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);
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;
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);
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.
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;
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;
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.
SELECT ProductName,
UnitPrice,
(SELECT MAX(Discount) FROM [Order Details] od
WHERE od.ProductID = p.ProductID) AS MaxDiscount
FROM Products p
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');
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);
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);
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 <=).
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);
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
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.
) )
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
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
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
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
sql_variant Stores up to 8,000 bytes of data of various data types, except text, ntext, and
timestamp