Skip to content
Advertisement

How to select rows in parent table based on a value in child table

Consider the following tables, what would be an efficient query to return 1 row for each order that has at least 1 child row with a specific warehouse code? I am using SQL Server 2016.

If I query for Warehouse 10:

If I query for Warehouse 20:

Advertisement

Answer

Assuming that when you say “When I query for Warehouse X” you are running individual queries for a single warehouse, just a simple INNER JOIN and a WHERE clause should do it:

If you want to have everything in one result set add ItemCode and Warehouse to the query and leave out the WHERE clause.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement