Executing SQL query inside Excel spreadsheet

Did you know that you can execute almost any SQL query directly in your Excel spreadsheet? This can be very useful for executing queries with GROUP BY and aggregates since they cannot be generated that easily. Let’s begin. I will use Excel 2013 but this method should work in other editions as well.

First, create any spreadsheet and save it somewhere. I will use the following:

Spreadsheet

Choose Data ribbon and click From Other Sources button. Choose From Microsoft Query:

Microsoft Query button

After clicking the button you will be able to choose data source. In fact, Microsoft Query is an external application (distributed with Office). Choose Excel Files:

Excel Files

Now select your file stored on the hard drive:

Choose file

Now you should be able to add tables to your workspace. Each sheet in spreadsheet represents separate table. Add table and hit Close.

Tables

Next, click SQL button placed just below the menu bar and insert any query you would like to execute:

Query

That’s all.