Skip to content
Advertisement

How to SUM values from 2 separate tables that share the same column name in SQL

I have 2 tables that have the exact same columns but different data. The columns are ‘name’, ‘gender’ and ‘count’. The first table is called names_2014 and the second names_2015. My goal is simply to find the top 5 most popular names amongst both these tables.

I know that to get the most popular names for one table is:

However, the closest I’ve gotten to my goal is:

I’ve tried many similar variations to this but none of them are successful. It seems that I need to combine both of the tables, and then SUM(count) and GROUP BY name but I guess I’m not combining the tables properly. Any help is much appreciated as I’ve spent hours on this and I feel like the solution is so close but I just can’t see it. I’m new to SQL and just trying to test my limits.

Advertisement

Answer

You may perform the aggregation on a subquery that unions the two tables as the following:

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