Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Query Efficiency

Status
Not open for further replies.

RHelman

Programmer
Jan 11, 2012
3
US
Hello,
I have a query where I am essentially Summing the total cost of each order by customer. I have several tables: Customers, Orders, OrderedUpgrades, Upgrades; each with foreign keys linking them.

I have two SQL queries that work, my question is, which is more efficient and is it preferable to use one format over another? Essentially, one uses the INNER JOIN command while the other does not.

Query 1:
Code:
SELECT Customers.ID, Customers.FirstName, Customers.LastName, SUM(OrderedUpgrades.Quantity*Upgrades.Cost) As Total
FROM (((Customers INNER JOIN Orders ON Customers.ID = Orders.Customer) INNER JOIN OrderedUpgrades ON Orders.ID = OrderedUpgrades.Order) INNER JOIN Upgrades ON Upgrades.ID = OrderedUpgrades.Upgrade) GROUP BY Customers.ID, Customers.FirstName, Customers.LastName;

Query 2:
Code:
SELECT Customers.ID, Customers.FirstName, Customers.LastName, SUM(OrderedUpgrades.Quantity*Upgrades.Cost) As Total FROM Customers, Orders, OrderedUpgrades, Upgrades WHERE
Customers.ID = Orders.Customer AND Orders.ID = OrderedUpgrades.Order AND OrderedUpgrades.Upgrade = Upgrades.ID GROUP BY Customers.ID, Customers.FirstName, Customers.LastName;
 
IMHO, inner joins are better than filtered cartesian products.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top