-1

I have 4 tables in SQL Server 2012. This is my diagram:

enter image description here

I have this query:

SELECT 
    pc.Product_ID, d.Dept_ID, c.Category_ID, sc.SubCategory_ID 
FROM 
    dbo.ProductConfiguration pc
INNER JOIN 
    dbo.SubCategory sc ON sc.SubCategory_ID = pc.SubCategory_ID
INNER JOIN 
    dbo.Category c ON c.Category_ID = sc.Category_ID
INNER JOIN 
    dbo.Department d ON d.Dept_ID = c.Dept_ID
WHERE 
    pc.Product_ID = 459218

What is the best way, (INNER, LEFT, RIGHT) to get columns values? I need be careful with performance

Thanks a lot

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ericardezp
  • 29
  • 1
  • 6
  • 2
    They have different meanings; which one to choose depends on how the data is located and your need. Look at this link https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins – Fact Mar 16 '18 at 00:06
  • [what-is-the-difference-between-inner-join-and-outer-join](https://stackoverflow.com/questions/38549/what-is-the-difference-between-inner-join-and-outer-join) OR [Whats the difference between inner join, left join, right join, and full join](https://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join) -- Between these two posts, you have over 2.8 million views. This question appears to be too broad or should be closed as duplicate. – Brien Foss Mar 16 '18 at 00:10
  • Thanks for all comments – ericardezp Mar 16 '18 at 15:49

3 Answers3

0

This has nothing to do with performance.

I suppose every product belongs to a subcategory which belongs to a category which belongs to a department. So you inner join all the tables; this is the normal thing to do.

If one of the entities were not mandatory, but optional, say there are categories that don't belong to a department 1, but you still wanted to show the row, then you'd outer join the table. You'd use a left outer join on departments. We never use right outer joins, because these are supposed to be harder to read, and they do essentially the same as left joins.

1) i.e. category.dept_id coud be null

Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73
0

your query best variant is LEFT JOIN, from its do not loses your information

0

I would recommend to you, use execution plans when you need to update the performance. using execution plan you can understand the behaviors. it will grate help for future. Thank You!