If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > w3schools alias example - can sb explain

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-02-11, 11:43
expired expired is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
w3schools alias example - can sb explain

Hi,

I came across a query at SQL Alias I can't understand. It says:

SELECT po.OrderID, p.LastName, p.FirstName
FROM Persons AS p,
Product_Orders AS po
WHERE p.LastName='Hansen' AND p.FirstName='Ola'

From what I see, there's no "JOIN" keyword used, but it's apparently going to operate on two separate tables. If it's going two retrieve data from "Product_Orders" tables, why isn't it mentioned after "WHERE" at all? So, how could "p.LastName=" and "p.FirstName" be related to "po.OrderID", how it knows what rows should be picked from "Product_Orders" table?
Reply With Quote
  #2 (permalink)  
Old 11-02-11, 12:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
[SIGH] yet another example of crappy sql from w3schools...

QUESTION: how it knows what rows should be picked from "Product_Orders" table?

ANSWER: by the absence of any filtering or join criteria, it picks all of them




does this make sense? no, but then, this example was about table aliases only

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-02-11, 13:35
expired expired is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Thanks, so the result will be like this, right?

OrderID|LastName|FirstName
1|Hansen|Ola
2|Hansen|Ola
3|Hansen|Ola
4|Hansen|Ola
5|Hansen|Ola
...
Reply With Quote
  #4 (permalink)  
Old 11-02-11, 13:52
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
To add to Rudy's comments, as there is no join between the Persons and Product_Orders table you will have "Cartesian Join". This is where for every row in one table will join with every row from a second table resulting in large amounts of data. In some cases this is desirable, in other cases not.

As for no JOIN the above statement could be rewritten as follows:

Code:
SELECT po.OrderID, p.LastName, p.FirstName
FROM Persons AS p
JOIN Product_Orders AS po
WHERE p.LastName='Hansen' AND p.FirstName='Ola'
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 11-02-11, 13:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by expired View Post
Thanks, so the result will be like this, right?
right


cartesian joins aren't necessarily bad if there are WHERE clause conditions

most optimizers are smart enough to push those conditions up into the data retrieval stage



did that make sense to anyone but me?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 11-02-11, 14:36
expired expired is offline
Registered User
 
Join Date: Nov 2011
Posts: 3
Ok thank you for answers
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On