SQL Join on One Column Contained in Another
SQL joins typically look to combine two tables into a set of columns that exist in both. Usually, the values match exactly, making the join straightforward. We can also use ILIKE to make the join case-insensitive and BETWEEEN if we have the date in one table and start date and end date in another.
Those don’t always get the job done on their own. Sometimes we need to be able to join one table to another if the value in the first table is contained in the value in the second. That’s probably clear as mud, so let me walk you through a practical example.
I typically use this technique when working with combined budgets. We typically don’t get our budgets down to the vendor level, we get one budget for a set of vendors, as seen below.
Our cost data, however, is broken out by vendor.
So how can we easily pull the cost data for each vendor in a given budget? Let’s backtrack for a second. How would we pull all budgets that contain the vendor ‘mediamath’?
We would leverage the wildcard symbol. Placing a wildcard at the start and end of the string finds all budget line items where the vendor’s column contains ‘mediamath’.
The difference for the join is that we have to apply the wildcard to the start and end of the vendor column in the cost data. We can do this by simply concatenating the wildcard to the start and end of the column name. This creates a dynamic string as opposed to the static ‘%mediamath%’ in the previous example.
Stay in touch
Subscribe to our newsletter
By clicking and subscribing, you agree to our Terms of Service and Privacy Policy
Now we see our budget line items with all the cost right there, just as we wanted. It’s a pretty simple trick once you look at it, but extremely effective when you need it.