I am given a list in MS Excel of 554 statistical units (called Primary Sampling Units or PSUs) from another department and have to query all 554 PSUs from a SQL Server 2008 Spatial database table (PSUs_2012) that contains over 6000 PSUs.

I want to avoid having to type each of the 554 PSU numbers in the WHERE clause and is wondering if there is a way that I can write a query that takes values from a specified column (e.g. PSU_Number) in the Excel/CSV table and return all the rows with the same PSU numbers in the PSUs_2012 database table. I can also import the (excel/csv) data into the same database but would still have the same problem of how I should go about querying the main table (PSUs_2012) using all the values in a column of another table. Note that PSU numbers are unique IDs and are in both data sets with identical column names.

Please note that I am new to databases and programming but willing to learn, fast. Solutions using PostgreSQL and SpatiaLite are also welcome.


A join should do the trick. Some variation of this query should work in your database of choice:

select PSUs_2012.PSUID, exceltable.PSUID 
from PSUs_2012  
join exceltable
on PSUs_2012.PSUID = exceltable.PSUID
  • Thanks it worked! @ user16094 your advise on the join type was very helpful also. Thanks. – Uazu Mar 13 '13 at 12:10

In SQL you can do it graphically either in a query window or view. You can add the tables and then drag one column to the other matching column. Your join syntax will populate for you. Then you can see what you are building and you can make other columns visible by checking the box. Run the query and check the results. If things check out ok save the query or view and reward yourself with a cup of coffee. If things don't look right change the syntax and run it again. Pay attention to the join type, you can right click on the join line in the window and set the join to right, left, etc. This is of course using SQL Server Management Studio (SSMS).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.