Inserting a query into an Excel worksheet lets you pull live data from external sources like databases, web services, or other files without manually copying and pasting. This keeps your data current and reduces errors. You’ll need Excel 2016 or later (including Microsoft 365) and a valid data source with permissions to access it.

What You'll Need

  • Microsoft Excel 2016 or later (including Microsoft 365)
  • Access to a data source: a database (SQL Server, Access), a web page with tabular data, a CSV or text file, or an OData feed
  • Network or file system permissions to reach the data source
  • For web queries: a stable internet connection and a publicly accessible URL
  • For database queries: connection details (server name, database name, credentials if required)

Step-by-Step

  1. Open your Excel workbook where you want the query results to appear. Make sure there’s enough empty space below and to the right of your cursor to avoid overwriting existing data.
  2. Go to the Data tab on the Ribbon. Look for the Get & Transform Data group. Click on Get Data.
  3. A dropdown menu will appear with options like From Database, From Web, From File, and more. Choose the type of data source you’re connecting to. For example, select From Web if pulling from a webpage, or From SQL Server if connecting to a database.
  4. Enter the connection details in the dialog box that opens. For a web query, paste the full URL (including https://). For a database, enter the server name and database name. Click OK.
  5. Excel will preview the data. In the Navigator window, you’ll see tables or lists available from the source. Select the table or query you want to import. You can click Transform Data if you need to clean or reshape the data before loading.
  6. If you clicked Transform Data, the Power Query Editor opens. Use the ribbon tools to remove columns, filter rows, change data types, or merge queries. When done, click Close & Load at the top left.
  7. If you did not transform the data, click Load in the Navigator window. Excel will insert the query results into a new worksheet by default, starting at cell A1.
  8. After the data loads, Excel creates a query connection. You’ll see a small icon (usually a lightning bolt or database symbol) in the worksheet’s status bar or in the Queries & Connections pane (View > Queries & Connections).
  9. To refresh the data later, right-click any cell in the query table and select Refresh, or go to Data > Refresh All. Excel will reconnect to the source and update the table with new data.

Common Mistakes

  • Trying to use a query on Excel 2013 or earlier — these versions lack the Get & Transform Data tools. Upgrade to Excel 2016 or later.
  • Using a URL that requires login credentials without providing them — web queries only work with public data unless you configure authentication in Power Query.
  • Overwriting existing data by loading the query into a cell that already contains values. Always leave blank space or choose to load into a new worksheet.
  • Forgetting to save the workbook after creating a query, which can cause the connection to break if you close Excel without saving.
  • Using a dynamic URL (like one with session tokens or timestamps) that changes with every visit — these won’t work for scheduled refreshes.
  • Assuming Excel can read any file format — while it handles CSV, TXT, JSON, and Excel files well, proprietary formats like .accdb (Access) require the correct drivers installed on your system.

Tips

  • Use the Power Query Editor to clean data before loading — remove headers from the middle of a table, split columns by delimiters, or convert text to dates. This saves time later.
  • Name your queries in the Queries & Connections pane (right-click > Rename) to easily identify them later, especially if you have multiple queries in one workbook.
  • Use Parameters to make queries reusable. For example, create a parameter for a date range or a product ID; then reference it in your query instead of hardcoding values.
  • For frequent updates, set the query to refresh automatically on open or at intervals. Go to Data > Queries & Connections, right-click the query, select Properties, then check Refresh every X minutes or Refresh when opening the file.
  • When importing from a website, inspect the page source to find the exact table ID or structure. Sometimes data is hidden in JavaScript; Excel can’t extract it unless it’s in HTML tables.
  • Keep a backup of your workbook before running complex queries. A malformed query can sometimes corrupt the data model or cause slow performance.

Troubleshooting

  • If the query fails to connect, check your internet connection or file path. Try opening the source data directly in a browser or file explorer to confirm it’s accessible.
  • If you see an error about "authentication failed," ensure your credentials are correct. For databases, try using Windows Authentication instead of SQL Server Authentication if possible.
  • If the query loads but shows no data, check if the source has changed — the table name might be different, or the webpage structure was updated. Re-run the query from the Navigator window to see updated options.
  • If Excel crashes or freezes during query loading, reduce the amount of data being imported. Use filters in Power Query to load only necessary rows or columns.
  • If the Refresh button is grayed out, the workbook may be in manual calculation mode. Go to Formulas > Calculation Options and select Automatic.
  • If you get a "data source error: file in use" message, close any other programs accessing the file (like another Excel instance or a shared network drive).
  • If the query works on one computer but not another, install the latest Microsoft OLE DB drivers or Microsoft Access Database Engine on the new machine.

Frequently Asked Questions

Can I insert a query into Excel Online?

No. Excel Online does not support the Get & Transform Data feature. You must use the desktop version of Excel (2016 or later) to create or edit queries. You can view and refresh existing queries in Excel Online, but not create new ones.

Why does my query take so long to load?

Large datasets, slow internet, or complex transformations in Power Query can slow loading. Reduce the number of rows or columns imported, or apply filters early in the query to limit data volume.

Can I combine data from multiple sources in one query?

Yes. Use Power Query to append tables (stack them vertically) or merge tables (join them horizontally by a common column). You can combine data from Excel files, web pages, and databases in one query.

Do queries update automatically when I share the file?

Yes, if the recipient has Excel 2016 or later and has access to the original data source. If they don’t have network access or permissions, they’ll see old data or errors.

Is there a limit to how much data a query can pull?

Excel can handle over a million rows, but performance degrades with very large datasets. For datasets larger than 1 million rows, consider using Power BI or a database instead.

Can I edit the SQL code in an Excel query?

Yes. If you’re using a database source like SQL Server, click Advanced Editor in Power Query to view and edit the M code or SQL statement directly. Be careful — incorrect syntax can break the query.