On Mon, Jul 28, 2014 at 3:10 PM, Drago, William @ MWG - NARDAEAST
<william.dr...@l-3com.com> wrote:
> All,
>
> Can someone tell me what the purpose of line 2 is in the following example? 
> It seems redundant to me since what is wanted from the Customers table is 
> specified on line 1.
>
>
> 1: SELECT Customers.CustomerName, Orders.OrderID
> 2: FROM Customers
> 3: INNER JOIN Orders
> 4: ON Customers.CustomerID=Orders.CustomerID
> 5: ORDER BY Customers.CustomerName;
>
> The above example was found here:
>
> http://www.w3schools.com/sql/sql_join_inner.asp
>
>
> Thanks,
> --
> Bill Drago
> Senior Engineer

Having read Igor response, I realize that I totally missed the intent
of your question. In line one, the "Customers." does _NOT_ necessarily
name a table! It could be a table name. Or it could be an alias name.
An alias name version of the above might be.

SELECT c.CustomerName, o.OrderID
FROM Customers AS c
INNER JOIN Orders as o
ON c.CustomerID=o.CustomerID
ORDER BY c.CustomerName;

One reason to do this is simply to shorten what needs to be typed in.
But another reason is when you refer to a single table (or view)
multiple times in a single SELECT. This is often called a "self join".
A bit on an explanation on this is here:
http://www.tutorialspoint.com/sql/sql-self-joins.htm

But basically an alias is needed in this case to distinguish which
instance of the table is being used for what. Example: You have a
table with names and ages. You want an age report. Who is older than
whom?

SELECT a.person as olderPerson, b.person as youngerPerson, a.age-b.age
as ageDifference
FROM people AS a
INNER JOIN people as b
where a.person=b.person and a.age > b.age;

You need the aliases "a" and "b" to compare the table with itself properly.


-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to