QuestionQ77

VerifiedDisplaying Data from Multiple Tables

Examine the data in the CUSTOMERS table:

Question Image

You need to list every city with more than one customer, together with the customer details. Evaluate this query:

SQL>SELECT c1.custname, c1.city -  
FROM Customers c1 __________________ Customers c2  
ON (c1.city=c2.city AND c1.custname<>c2.custname);  

Which two JOIN options can fill the blank in the preceding query and produce the correct output?

Choose two
Explanation

An ON clause cannot be used together with the NATURAL JOIN syntax, so that option is invalid on syntactic grounds alone. The plain JOIN (equivalent to INNER JOIN) with the given ON predicate correctly restricts the result to customer rows that have at least one other customer sharing the same city, which is exactly the required output. Because the SELECT list draws only from c1, a LEFT OUTER JOIN would additionally retain every c1 row that lacks a c2 match — that is, every customer who is alone in their city — and display their real details as though they qualified, which is incorrect. A RIGHT OUTER JOIN, however, preserves unmatched rows from c2 instead, and since the selected columns come from c1, an unmatched right-side row surfaces with NULL values rather than presenting a lone customer's real data as a false match, so for the actual customer rows it returns the same qualifying set as the plain join.

Community Discussion

No comments yet. Be the first to start the discussion!