Bai 3
Bai 3
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
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