Professional Documents
Culture Documents
2021 02 03 Class Presentation - SQL FROM
2021 02 03 Class Presentation - SQL FROM
2021 02 03 Class Presentation - SQL FROM
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
SQL QUERIES – CLAUSES IN THE SELECT STATEMENT
SELECT [DISTINCT] [TOP] … <select_list>
FROM … JOIN ON
WRITTEN ORDER
Store your sql code and a snapshot of the result in a Word document.
FROM CLAUSE
The columns used to join the tables are normally primary and foreign keys,
but they don’t have to be. They can even be columns with no relationships
between them at all.
Therefor, do not assume that SQL Server will automatically join tables using
their primary and foreign keys. Your query still has to specify the
columns to be used for joining the tables.
-- 19 820 rows
SELECT Sales.SalesOrderHeader.SalesOrderID
FROM Sales.SalesOrderHeader
-- 31 465 rows
SELECT Sales.Customer.CustomerID
FROM Sales.Customer
JOIN Sales.SalesOrderHeader
ON Sales.Customer.CustomerID = Sales.SalesOrderHeader.CustomerID
-- 31 465 rows
SELECT Sales.Customer.CustomerID
FROM Sales.Customer
JOIN Sales.SalesOrderHeader
ON Sales.Customer.TerritoryID = Sales.SalesOrderHeader.TerritoryID
-- 13 000 000+ rows
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
1-1 JOIN ON
SELECT *
FROM dbo.Customers A
INNER JOIN dbo.Orders B
ON A.customer_id = B.customer_id
1-1 JOIN ON
Note that each table has an alias (A and B), which can be used to
refer to the tables in other clauses of the query.
input.
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS INNER JOIN (The INNER keyword is optional)
This is the simplest and most common join. This query will return all of
the records in the left table (table A) that have a matching record in the
right table (table B).
SELECT *
1-1 JOIN ON
FROM dbo.Customers A
JOIN dbo.Orders B
ON A.customer_id = B.customer_id
ORDERS
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS LEFT OUTER JOIN (The OUTER keyword is optional)
This query will return all of the records in the left table (table A) regardless
if any of those records have a match in the right table (table B). It will also
return any matching records from the right table.
SELECT *
1-1 JOIN ON
FROM dbo.Customers A
LEFT JOIN dbo.Orders B
ON A.customer_id = B.customer_id
ORDERS
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS RIGHT OUTER JOIN (The OUTER keyword is optional)
This query will return all of the records in the right table (table B)
regardless if any of those records have a match in the left table (table
A). It will also return any matching records from the left table.
SELECT *
FROM dbo.Customers A
1-1 JOIN ON
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS FULL OUTER JOIN
This Join can be referred to as a FULL OUTER JOIN or a FULL JOIN.
This query will return all of the records from both tables, joining
records from the left table (table A) that match records from the
right table (table B).
SELECT *
1-1 JOIN ON
FROM dbo.Customers A
FULL OUTER JOIN dbo.Orders B
ON A.customer_id = B.customer_id
ORDERS
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS LEFT EXCLUDING JOIN
This query will return all of the records in the left table
(table A) that do not match any records in the right
table (table B).
1-1 JOIN ON
SELECT *
FROM dbo.Customers A
LEFT JOIN dbo.Orders B
ORDERS ON A.customer_id = B.customer_id
WHERE B.customer_id IS NULL
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS RIGHT EXCLUDING JOIN
This query will return all of the records in the right table (table B) that
do not match any records in the left table (table A).
SELECT *
FROM dbo.Customers A
1-1 JOIN ON
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS OUTER EXCLUDING JOIN
This query will return all of the records in the left table (table A)
and all of the records in the right table (table B) that do not match.
1-1 JOIN ON
SELECT *
FROM dbo.Customers A
ORDERS FULL OUTER JOIN dbo.Orders B
ON A.customer_id = B.customer_id
WHERE A.customer_id IS NULL OR
B.customer_id IS NULL
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
PRACTICAL
Visit https://www.wiseowl.co.uk/sql/exercises/standard and do the exercises
on Basic joins and More exotic joins.
Store your sql code and a snapshot of the results in a Word document.
1-1 JOIN ON
RESULT
RESULT
empid 2013 2014 2015
------ --------- ---------- ---------
9 9894.52 26310.39 41103.17
3 18223.96 108026.17 76562.75
6 16642.61 43126.38 14144.16
7 15232.16 60471.19 48864.89
Obviously, if you only retrieve records from a single table, the WHERE
clause will be your only change to filter out individual records.
ORDERS
SELECT *
FROM dbo.Customers A
INNER JOIN dbo.Orders B
ON A.customer_id = B.customer_id
WHERE B.amount > 100
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
2 THE WHERE CLAUSE
When multiple operators appear in the same expression, SQL Server
evaluates them based on operator precedence rules. The following list
2 THE WHERE CLAUSE
RESULT
RESULT
The % (percent) wildcard
The percent sign represents a string of any
size, including an empty string. For example,
the following query returns customers
where the name starts with M:
SELECT * RESULT
FROM dbo.Customers
WHERE customer_name LIKE N'M%'
SELECT * RESULT
FROM dbo.Customers
WHERE customer_name LIKE N'_e%'
SELECT * RESULT
FROM dbo.Customers
WHERE customer_name LIKE N'[CGK]%';
SELECT * RESULT
FROM dbo.Customers
WHERE customer_name LIKE N'[A-E]%';
-- Output 2020-02-12
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
4 THE HAVING CLAUSE
(5) SELECT (5-2) [DISTINCT] (7) [TOP] … (5-1) <select_list>
(1) FROM … (1-1) JOIN ON
4 THE HAVING CLAUSE
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
5-1 THE SELECT LIST
(5) SELECT (5-2) [DISTINCT] (7) [TOP] … (5-1) <select_list>
(1) FROM … (1-1) JOIN ON
(1-2) APPLY
5-1 THE SELECT LIST
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
5-2 THE SELECT DISTINCT CLAUSE
(5) SELECT (5-2) [DISTINCT] (7) [TOP] … (5-1) <select_list>
5-2 THE DISTINCT CLAUSE
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
6 THE ORDER BY CLAUSE
(5) SELECT (5-2) [DISTINCT] (7) [TOP] … (5-1) <select_list>
(1) FROM … (1-1) JOIN ON
6 THE ORDER BY CLAUSE
SELECT *
FROM dbo.Customers A
INNER JOIN dbo.Orders B
ON A.customer_id = B.customer_id
ORDER BY A.customer_name DESC, B.amount ASC
RESULT
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
CUSTOMERS
7 OFFSET, FETCH
Perform the same query as in the
previous example, but skip the first 3
7 OFFSET, FETCH
RESULT
2 0 2 02 0C2T0U CTTr U
a i nTirnagi nSi nogl u St iool nust i o| nAsl l | RAi gl l hRt si gRhet s eRr ev e
s edr v| ecdt u| t rcat u
i nt irnagi n. ai nc g. z. a c . z a
Thank You!