Selecting data from a table based on value in another table (SQL server)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I am using the northwind example database to brush up my SQL server skills.



I want to select all the products ordered by the customer with the customer id 'Folko'.



My idea is to join the Orders table with the OrderDetails table on OrderID and then join the Products table with the OrderDetails table on ProductID.
Then insert the WHERE clause (i.e. where Orders.CustomerID = 'FOLKO')



Here is the query:



select Products.ProductName from [Order Details] 
inner join Orders on [Order Details].OrderID = Orders.OrderID
inner join Products on [Order Details].ProductID = Products.ProductID
where Orders.CustomerID = 'FOLKO'


I have also managed to get the same information with the following query:



select Products.ProductName from [Order Details], Orders, Products
where Products.ProductID = [Order Details].ProductID
and Orders.OrderID = [Order Details].OrderID
and Orders.CustomerID = 'FOLKO'


I am getting the result that I want but I wonder if these are the preferred ways (or if one or the other is better) or if it should be done in a different way altogether?







share|improve this question





















  • You should avoid old style joins (second).
    – paparazzo
    Jan 16 at 19:24
















up vote
0
down vote

favorite












I am using the northwind example database to brush up my SQL server skills.



I want to select all the products ordered by the customer with the customer id 'Folko'.



My idea is to join the Orders table with the OrderDetails table on OrderID and then join the Products table with the OrderDetails table on ProductID.
Then insert the WHERE clause (i.e. where Orders.CustomerID = 'FOLKO')



Here is the query:



select Products.ProductName from [Order Details] 
inner join Orders on [Order Details].OrderID = Orders.OrderID
inner join Products on [Order Details].ProductID = Products.ProductID
where Orders.CustomerID = 'FOLKO'


I have also managed to get the same information with the following query:



select Products.ProductName from [Order Details], Orders, Products
where Products.ProductID = [Order Details].ProductID
and Orders.OrderID = [Order Details].OrderID
and Orders.CustomerID = 'FOLKO'


I am getting the result that I want but I wonder if these are the preferred ways (or if one or the other is better) or if it should be done in a different way altogether?







share|improve this question





















  • You should avoid old style joins (second).
    – paparazzo
    Jan 16 at 19:24












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am using the northwind example database to brush up my SQL server skills.



I want to select all the products ordered by the customer with the customer id 'Folko'.



My idea is to join the Orders table with the OrderDetails table on OrderID and then join the Products table with the OrderDetails table on ProductID.
Then insert the WHERE clause (i.e. where Orders.CustomerID = 'FOLKO')



Here is the query:



select Products.ProductName from [Order Details] 
inner join Orders on [Order Details].OrderID = Orders.OrderID
inner join Products on [Order Details].ProductID = Products.ProductID
where Orders.CustomerID = 'FOLKO'


I have also managed to get the same information with the following query:



select Products.ProductName from [Order Details], Orders, Products
where Products.ProductID = [Order Details].ProductID
and Orders.OrderID = [Order Details].OrderID
and Orders.CustomerID = 'FOLKO'


I am getting the result that I want but I wonder if these are the preferred ways (or if one or the other is better) or if it should be done in a different way altogether?







share|improve this question













I am using the northwind example database to brush up my SQL server skills.



I want to select all the products ordered by the customer with the customer id 'Folko'.



My idea is to join the Orders table with the OrderDetails table on OrderID and then join the Products table with the OrderDetails table on ProductID.
Then insert the WHERE clause (i.e. where Orders.CustomerID = 'FOLKO')



Here is the query:



select Products.ProductName from [Order Details] 
inner join Orders on [Order Details].OrderID = Orders.OrderID
inner join Products on [Order Details].ProductID = Products.ProductID
where Orders.CustomerID = 'FOLKO'


I have also managed to get the same information with the following query:



select Products.ProductName from [Order Details], Orders, Products
where Products.ProductID = [Order Details].ProductID
and Orders.OrderID = [Order Details].OrderID
and Orders.CustomerID = 'FOLKO'


I am getting the result that I want but I wonder if these are the preferred ways (or if one or the other is better) or if it should be done in a different way altogether?









share|improve this question












share|improve this question




share|improve this question








edited Jan 16 at 19:25









Sam Onela

5,88461545




5,88461545









asked Jan 16 at 18:20









karra

62




62











  • You should avoid old style joins (second).
    – paparazzo
    Jan 16 at 19:24
















  • You should avoid old style joins (second).
    – paparazzo
    Jan 16 at 19:24















You should avoid old style joins (second).
– paparazzo
Jan 16 at 19:24




You should avoid old style joins (second).
– paparazzo
Jan 16 at 19:24










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The first query, i.e. using explicit JOINs, is generally the preferred way, and has benefits like:



  • verbosity

  • easier to read and maintain

  • less likely to have accidental cross joins

For more precise and detailed answers, see the answers to this question on SO (bearing in mind it is tagged mysql, it still is relevant for sql-server). Also read this related blog post by Baron Schwartz.






share|improve this answer























  • Perfect! Thank you for the help and useful links!
    – karra
    Jan 16 at 19:33










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185245%2fselecting-data-from-a-table-based-on-value-in-another-table-sql-server%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The first query, i.e. using explicit JOINs, is generally the preferred way, and has benefits like:



  • verbosity

  • easier to read and maintain

  • less likely to have accidental cross joins

For more precise and detailed answers, see the answers to this question on SO (bearing in mind it is tagged mysql, it still is relevant for sql-server). Also read this related blog post by Baron Schwartz.






share|improve this answer























  • Perfect! Thank you for the help and useful links!
    – karra
    Jan 16 at 19:33














up vote
1
down vote



accepted










The first query, i.e. using explicit JOINs, is generally the preferred way, and has benefits like:



  • verbosity

  • easier to read and maintain

  • less likely to have accidental cross joins

For more precise and detailed answers, see the answers to this question on SO (bearing in mind it is tagged mysql, it still is relevant for sql-server). Also read this related blog post by Baron Schwartz.






share|improve this answer























  • Perfect! Thank you for the help and useful links!
    – karra
    Jan 16 at 19:33












up vote
1
down vote



accepted







up vote
1
down vote



accepted






The first query, i.e. using explicit JOINs, is generally the preferred way, and has benefits like:



  • verbosity

  • easier to read and maintain

  • less likely to have accidental cross joins

For more precise and detailed answers, see the answers to this question on SO (bearing in mind it is tagged mysql, it still is relevant for sql-server). Also read this related blog post by Baron Schwartz.






share|improve this answer















The first query, i.e. using explicit JOINs, is generally the preferred way, and has benefits like:



  • verbosity

  • easier to read and maintain

  • less likely to have accidental cross joins

For more precise and detailed answers, see the answers to this question on SO (bearing in mind it is tagged mysql, it still is relevant for sql-server). Also read this related blog post by Baron Schwartz.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jan 16 at 19:29


























answered Jan 16 at 19:24









Sam Onela

5,88461545




5,88461545











  • Perfect! Thank you for the help and useful links!
    – karra
    Jan 16 at 19:33
















  • Perfect! Thank you for the help and useful links!
    – karra
    Jan 16 at 19:33















Perfect! Thank you for the help and useful links!
– karra
Jan 16 at 19:33




Perfect! Thank you for the help and useful links!
– karra
Jan 16 at 19:33












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185245%2fselecting-data-from-a-table-based-on-value-in-another-table-sql-server%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?