Running SQL Queries in Excel

%3CLINGO-SUB%20id%3D%22lingo-sub-1824842%22%20slang%3D%22en-US%22%3ERunning%20SQL%20Queries%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1824842%22%20slang%3D%22en-US%22%3E%3CP%3EI've%20used%20ODBC%20queries%20in%20Excel%20before%20but%20it%20was%20always%20with%20stored%20procedures%20on%20the%20server.%20Now%20I'd%20like%20to%20be%20able%20to%20define%20freeform%20SQL%20queries%20in%20Excel%20that%20pull%20data%20from%20a%20MS%20SQL%20Server.%20I%20have%20been%20playing%20around%20for%2030%20mins%20or%20so%20and%20Googling%20how%20to%20do%20it%20but%20can't%20seem%20to%20find%20anything.%20How%20do%20I%20build%20and%20debug%20SQL%20queries%20in%20Excel%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1824842%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1825534%22%20slang%3D%22en-US%22%3ERe%3A%20Running%20SQL%20Queries%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1825534%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F848109%22%20target%3D%22_blank%22%3E%40Dave7%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20best%20way%20to%20pull%20data%20from%20a%20SQL%20Server%20is%20to%20use%20Power%20Query%20in%20Excel%2C%20have%20you%20come%20across%20that%20yet%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20897px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F229692i9C579FBF1DC1A175%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1827252%22%20slang%3D%22en-US%22%3ERe%3A%20Running%20SQL%20Queries%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1827252%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F9832%22%20target%3D%22_blank%22%3E%40Wyn%20Hopkins%3C%2FA%3E.%26nbsp%3BI%20should%20have%20said%20that%20yes%2C%20I%20got%20into%20the%20Power%20Query%20but%20couldn't%20figure%20out%20how%20to%20use%20it%20to%20write%20plain%20SQL.%20The%20Advanced%20Editor%20it%20has%20seems%20to%20use%20a%20different%20language%20(PowerQuery%2C%20apparently).%20Is%20it%20just%20not%20possible%20to%20write%20plain%20SQL%3F%20If%20that's%20the%20case%2C%20I'm%20sure%20I%20can%20figure%20out%20PowerQuery%20though%20if%20you%20have%20a%20good%20primer%20I'm%20all%20ears.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I've used ODBC queries in Excel before but it was always with stored procedures on the server. Now I'd like to be able to define freeform SQL queries in Excel that pull data from a MS SQL Server. I have been playing around for 30 mins or so and Googling how to do it but can't seem to find anything. How do I build and debug SQL queries in Excel?

 

Thanks!

4 Replies
Highlighted

Hi @Dave7 

 

The best way to pull data from a SQL Server is to use Power Query in Excel, have you come across that yet?

 

image.png

Highlighted

Thanks, @Wyn Hopkins. I should have said that yes, I got into the Power Query but couldn't figure out how to use it to write plain SQL. The Advanced Editor it has seems to use a different language (PowerQuery, apparently). Is it just not possible to write plain SQL? If that's the case, I'm sure I can figure out PowerQuery though if you have a good primer I'm all ears. 

Highlighted

@Dave7 

Power Query is not the tool for generating SQL query, you may only embed already prepared SQL query into the connector to SQL database. There are pros and cons, but mostly the latest. If only performance for relatively complex queries since SQL query runs on server site, but even in this case it's better to create view directly on the SQL server and connect to it.

 

In Power Query major part of tasks could be done with user interface without coding on M-script (internal Power Query language). Query to SQL database could be generated from UI as well.

Highlighted

Hi @Dave7 

 

When connecting to a SQL database you do have the option of writing plain SQL here...

image.png

 

If you use the Power Query interface to do transformations then if that transformation can be converted into SQL it will be done automatically in the background...  You can often (but not always) see this happening by Right Clicking on a step and viewing the Native Query:

 

image.png

 

Note:

You may well get better performance by using the Power Query steps and allowing it to convert and "fold" much of the query back to the SQL Server.

 

Writing your own SQL query from scratch breaks "query folding" and the data processing will be carried out on your computer.

 

e.g. Filtering just for this year's records - if done via applying a filter in Power Query will push that filtering back to be performed quicker by the Server.