Selecting data from a table based on value in another table (SQL server)
Clash 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?
sql comparative-review sql-server
add a comment |Â
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?
sql comparative-review sql-server
You should avoid old style joins (second).
â paparazzo
Jan 16 at 19:24
add a comment |Â
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?
sql comparative-review sql-server
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?
sql comparative-review sql-server
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
add a comment |Â
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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The first query, i.e. using explicit JOIN
s, 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.
Perfect! Thank you for the help and useful links!
â karra
Jan 16 at 19:33
add a comment |Â
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 JOIN
s, 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.
Perfect! Thank you for the help and useful links!
â karra
Jan 16 at 19:33
add a comment |Â
up vote
1
down vote
accepted
The first query, i.e. using explicit JOIN
s, 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.
Perfect! Thank you for the help and useful links!
â karra
Jan 16 at 19:33
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The first query, i.e. using explicit JOIN
s, 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.
The first query, i.e. using explicit JOIN
s, 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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You should avoid old style joins (second).
â paparazzo
Jan 16 at 19:24