question

jowens0520 avatar image
jowens0520 asked

How to optimize the following SQL Query

I have been given the assignment to figure out what is wrong with the SQL Query below and how to optimize it. Here is what I have so far: 1) Incorrect syntax near the keyword 'FROM'. This means there is a trailing comma in the select list. 2) The Alias 'C' is not set in the query 3) Order by column ordinal is a bad practice I should avoid too except in adhoc querying 4) Instead of <> null, it should be is not null Anything else? SELECT C.ContactID, C.FirstName, C.LastName, SP.SalesPersonID, FROM Person.Contact A JOIN Sales.SalesPerson SP LEFT JOIN Sales.SalesTerritory ST ON ST.TerritoryID = SP.TerritoryID WHERE SP.SalesPersonNM <> NULL ORDER BY 1,2,3,4,5
querysql query
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
David Wimbush avatar image
David Wimbush answered
I reckon you've nailed most of it. The only other thing I'd question is that there's no join condition for the join between Person.Contact and Sales.SalesPerson. That will join every contact to every sales person, which doesn't sound likely.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jowens0520 avatar image jowens0520 commented ·
That's a good point
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.