Sql server® 2012 t-sql fundamentals


Using Outer Joins in a Multi-Join Query



Download 10,93 Mb.
Pdf ko'rish
bet170/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   166   167   168   169   170   171   172   173   ...   443
Bog'liq
BookSQL

Using Outer Joins in a Multi-Join Query

Recall the discussion about all-at-once operations in Chapter 2, “Single-Table Queries.” The concept 

describes the fact that all expressions that appear in the same logical query processing phase are 

logically evaluated at the same point in time. However, this concept is not applicable to the process-

ing of table operators in the FROM phase. Table operators are logically evaluated from left to right. 

Re arranging the order in which outer joins are processed might result in different output, so you 

cannot rearrange them at will. 

Some interesting logical bugs have to do with the logical order in which outer joins are processed. 

For example, a common logical bug involving outer joins could be considered a variation of the bug 

in the previous section. Suppose that you write a multi-join query with an outer join between two 

tables, followed by an inner join with a third table. If the predicate in the inner join’s ON clause com-

pares an attribute from the nonpreserved side of the outer join and an attribute from the third table, 

all outer rows are filtered out. Remember that outer rows have NULL marks in the attributes from the 

nonpreserved side of the join, and comparing a NULL with anything yields UNKNOWN. UNKNOWN is 

filtered out by the ON filter. In other words, such a predicate would nullify the outer join, and logically 

it would be as if you specified an inner join. For example, consider the following query.

SELECT C.custid, O.orderid, OD.productid, OD.qty 

FROM Sales.Customers AS C 

  LEFT OUTER JOIN Sales.Orders AS O 

    ON C.custid = O.custid 

  JOIN Sales.OrderDetails AS OD 

    ON O.orderid = OD.orderid;

The first join is an outer join returning customers and their orders and also customers who did 

not place any orders. The outer rows representing customers with no orders have NULL marks in the 

order attributes. The second join matches order lines from the OrderDetails table with rows from the 

result of the first join, based on the predicate O.orderid = OD.orderid; however, in the rows represent-

ing customers with no orders, the O.orderid attribute is NULL. Therefore, the predicate evaluates to 

UNKNOWN, and those rows are filtered out. The output shown here in abbreviated form doesn’t 

contain the customers 22 and 57, the two customers who did not place orders.

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   166   167   168   169   170   171   172   173   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish