0% found this document useful (0 votes)
29 views24 pages

Bai 3

This document provides an overview of advanced query concepts in SQL including joining multiple tables, grouping and aggregating data, and using different join types. It discusses inner joins, outer joins, cross joins, and self joins. It also covers built-in aggregate functions and how to use the GROUP BY clause to group query results and calculate aggregate values over groups. The logical order of operations when using the GROUP BY clause is also explained.

Uploaded by

Tom Vu
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)
29 views24 pages

Bai 3

This document provides an overview of advanced query concepts in SQL including joining multiple tables, grouping and aggregating data, and using different join types. It discusses inner joins, outer joins, cross joins, and self joins. It also covers built-in aggregate functions and how to use the GROUP BY clause to group query results and calculate aggregate values over groups. The logical order of operations when using the GROUP BY clause is also explained.

Uploaded by

Tom Vu
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/ 24

Module 3

Advance Query Data


Module Overview

Querying Multiple Tables


Understanding Joins
Querying with Inner Joins
Querying with Outer Joins
Querying with Cross Joins and Self Joins
Grouping and Aggregating Data
Using Aggregate Functions
Using the GROUP BY Clause
Filtering Groups with HAVING
Join Terminology: Cartesian Product
Characteristics of a Cartesian product
Output or intermediate result of FROM clause
Combine all possible combinations of two sets
In T-SQL queries, usually undesired
Special case: table of numbers Name Product
Davis Alice Mutton
Davis Crab Meat
Name Product Davis Ipoh Coffee
Davis Alice Mutton Funk Alice Mutton
Funk Crab Meat Funk Crab Meat
King Ipoh Coffee Funk Ipoh Coffee
King Alice Mutton
King Crab Meat
King Ipoh Coffee
Overview of Join Types
Join types in FROM clauses specify the
operations performed on the virtual table:
Join Type Description
Cross Combines all rows in both tables (creates
Cartesian product)
Inner Starts with Cartesian product; applies filter
to match rows between tables based on
predicate
Outer Starts with Cartesian product; all rows from
designated table preserved, matching rows
from other table retrieved. Additional
NULLs inserted as placeholders
T-SQL Syntax Choices
ANSI SQL-92
Tables joined by JOIN operator in FROM Clause
Preferred syntax
SELECT ...
FROM Table1 JOIN Table2
ON <on_predicate>

ANSI SQL-89
Tables joined by commas in FROM Clause
SELECT ...
FROM Table1, Table2
WHERE <where_predicate>
Inner Join Syntax
List tables in FROM Clause separated by JOIN operator
Table aliases preferred
Table order does not matter

FROM t1 JOIN t2
ON t1.column = t2.column

SELECT o.orderid,
o.orderdate,
od.productid,
od.unitprice,
od.qty
FROM Sales.Orders AS o
JOIN Sales.OrderDetails AS od
ON o.orderid = od.orderid;
Understanding Outer Joins
Returns all rows from one table and any matching rows from
second table
One tables rows are preserved
Designated with LEFT, RIGHT, FULL keyword
All rows from preserved table output to result set
Matches from other table retrieved
Additional rows added to results for non-matched rows
NULLs added in places where attributes do not match
Example: Return all customers and for those who have placed
orders, return order information. Customers without
matching orders will display NULL for order details.
Outer Join Syntax
Return all rows from first table, only matches from
second:
FROM t1 LEFT OUTER JOIN t2 ON
t1.col = t2.col

Return all rows from second table, only matches from


first:
FROM t1 RIGHT OUTER JOIN t2 ON
t1.col = t2.col

Return only rows from first table with no match in


second:
FROM t1 LEFT OUTER JOIN t2 ON
t1.col = t2.col
WHERE t2.col IS NULL
Outer Join Examples
All customers with order details if present:
SELECT c.custid, c.contactname, o.orderid,
o.orderdate
FROM Sales.Customers AS C
LEFT OUTER JOIN Sales.Orders AS O
ON c.custid = o.custid;

Customers that did not place orders:


SELECT c.custid, c.contactname, o.orderid,
o.orderdate
FROM Sales.Customers AS C LEFT OUTER JOIN
Sales.Orders AS O
ON c.custid = o.custid
WHERE o.orderid IS NULL;
Cross Join Syntax
No matching performed, no ON clause used
Return all rows from left table combined with
each row from right table (ANSI SQL-92
syntax):
SELECT ...
FROM t1 CROSS JOIN t2

Return all rows from left table combined with


each row from right table (ANSI SQL-89
syntax):
SELECT ...
FROM t1, t2
Understanding Self Joins
Why use self joins?
Compare rows in same table to each other

Create two instances of same table in FROM


clause
At least one alias required
Example: Return all employees and
the name of the employees manager
Self Join Examples
Return all employees with ID of employees
manager when a manager exists (inner join):
SELECT e.empid, e.lastname,
e.title, e.mgrid, m.lastname
FROM HR.Employees AS e
JOIN HR.Employees AS m
ON e.mgrid=m.empid ;

Return all employees with ID of manager


(outer join). This will return NULL for the CEO:
SELECT e. empid, e.lastname,
e.title, m.mgrid
FROM HR.Employees AS e
LEFT OUTER JOIN HR.Employees AS m
ON e.mgrid=m.empid;
Working with Aggregate Functions
Aggregate functions:
Return a scalar value (with no column name)
Ignore NULLs except in COUNT(*)
Can be used in
SELECT, HAVING, and ORDER BY clauses
Frequently used with GROUP BY clause
SELECT AVG(unitprice) AS avg_price,
MIN(qty)AS min_qty,
MAX(discount) AS max_discount
FROM Sales.OrderDetails;

avg_price min_qty max_discount


--------- ------- ------------
26.2185 1 0.250
Built-In Aggregate Functions

Common Statistical Other

SUM STDEV CHECKSUM_AGG


MIN STDEVP GROUPING
MAX VAR GROUPING_ID
AVG VARP
COUNT
COUNT_BIG
Using DISTINCT with Aggregate Functions
Use DISTINCT with aggregate functions to
summarize only unique values
DISTINCT aggregates eliminate duplicate
values, not rows (unlike SELECT DISTINCT)
Compare (with partial results):
SELECT empid, YEAR(orderdate) AS orderyear,
COUNT(custid) AS all_custs,
COUNT(DISTINCT custid) AS unique_custs
FROM Sales.Orders
GROUP BY empid, YEAR(orderdate);

empid orderyear all_custs unique_custs


----------- ----------- ----------- ------------
1 2006 26 22
1 2007 55 40
1 2008 42 32
2 2006 16 15
Working with Aggregate Functions
Aggregate functions:
Return a scalar value (with no column name)
Ignore NULLs except in COUNT(*)
Can be used in
SELECT, HAVING, and ORDER BY clauses
Frequently used with GROUP BY clause
SELECT AVG(unitprice) AS avg_price,
MIN(qty)AS min_qty,
MAX(discount) AS max_discount
FROM Sales.OrderDetails;

avg_price min_qty max_discount


--------- ------- ------------
26.2185 1 0.250
Built-In Aggregate Functions

Common Statistical Other

SUM STDEV CHECKSUM_AGG


MIN STDEVP GROUPING
MAX VAR GROUPING_ID
AVG VARP
COUNT
COUNT_BIG
Using DISTINCT with Aggregate Functions
Use DISTINCT with aggregate functions to
summarize only unique values
DISTINCT aggregates eliminate duplicate
values, not rows (unlike SELECT DISTINCT)
Compare (with partial results):
SELECT empid, YEAR(orderdate) AS orderyear,
COUNT(custid) AS all_custs,
COUNT(DISTINCT custid) AS unique_custs
FROM Sales.Orders
GROUP BY empid, YEAR(orderdate);

empid orderyear all_custs unique_custs


----------- ----------- ----------- ------------
1 2006 26 22
1 2007 55 40
1 2008 42 32
2 2006 16 15
Using the GROUP BY Clause
GROUP BY creates groups for output rows, according
to a unique combination of values specified in the
GROUP BY clause
SELECT <select_list>
FROM <table_source>
WHERE <search_condition>
GROUP BY <group_by_list>;

GROUP BY calculates a summary value for aggregate


functions in subsequent phases
SELECT empid, COUNT(*) AS cnt
FROM Sales.Orders
GROUP BY empid;

Detail rows are lost after GROUP BY clause is


processed
GROUP BY Workflow
SELECT orderid, empid, custid
FROM Sales.Orders; orderid empid custid
10643 6 1
orderid empid custid
10692 4 1
10643 6 1
10926 4 2
10692 4 1
10625 3 2
10926 4 2
10625 3 2
WHERE custid IN(1,2)
10365 3 3
GROUP BY empid

empid COUNT(*)
6 1
4 2
3 1
GROUP BY and the Logical Order of
Operations
Logical Order Phase Comments
5 SELECT
1 FROM
2 WHERE
3 GROUP BY Creates groups
4 HAVING Operates on groups
6 ORDER BY

All columns in SELECT, HAVING, and ORDER BY must


appear in GROUP BY clause or be inputs to aggregate
expressions
Filtering Grouped Data Using the HAVING
Clause

HAVING clause provides a search condition


that each group must satisfy
HAVING clause is processed after GROUP BY
SELECT custid, COUNT(*) AS count_orders
FROM Sales.Orders
GROUP BY custid
HAVING COUNT(*) > 10;
Compare HAVING to WHERE
Using a COUNT(*) expression in HAVING clause is useful to
solve common business problems:
Show only customers that have placed more than one order:

SELECT c.custid, COUNT(*) AS cnt


FROM Sales.Customers AS c
JOIN Sales.Orders AS o ON c.custid = o.custid
GROUP BY c.custid
HAVING COUNT(*) > 1;

Show only products that appear on 10 or more orders:


SELECT p.productid, COUNT(*) AS cnt
FROM Production.Products AS p JOIN Sales.OrderDetails AS od ON
p.productid = od.productid
GROUP BY p.productid
HAVING COUNT(*) >= 10;
Summary

You might also like