SQL with Excel — Query Your School Data Without Leaving Excel

SQL and Excel Reports

The office keeps a workbook class list and also receives data from a central SQL database. It needs to know where filtering occurs and which language each tool accepts.

Excel can import and transform database data through supported connectors. Power Query transformations use the M language; they are not automatically SQL statements executed by a workbook.

  1. Explain and apply

    Distinguish SQL queries from Power Query M and trace a workbook-table filter.

  2. Work it out

    Express the same class-10A filter in SQL and compare its two-row result with the preserved M example.

  3. Avoid mistakes

    The retained Advanced Editor example is M, not SQL-like syntax that can accept arbitrary SELECT statements.

SQL and Excel Reports: the request and its result

  1. School need

    The office keeps a workbook class list and also receives data from a central SQL database. It needs to know where filtering occurs and which language each tool accepts.

  2. Database operation

    Excel can import and transform database data through supported connectors. Power Query transformations use the M language; they are not automatically SQL statements executed by a workbook.

  3. Check the outcome

    Both operations can describe a class filter, but SQL runs on a database while the shown Table.SelectRows transformation is M. Their input fixtures are separately identified.

A conceptual model. The query examples below show the actual syntax, input data and results.

Understand what the database does

A workbook connector reads sheets or named tables into Power Query. Table.SelectRows expresses a filter in M. A database connector may support native SQL and query folding, but availability depends on the source, connector and platform. A legacy ODBC query against a sheet uses that driver’s workbook naming convention.

Related concepts: Select Data and Connect with JDBC and ODBC.

Why Use SQL with Excel?

  • Query multiple sheets like tables
  • Run SELECT, WHERE, JOIN, and GROUP BY inside Excel
  • Build dynamic dashboards without VBA
  • Connect external databases (MySQL, Access) via Excel

Step 1: Prepare Your Excel Sheet

Create a worksheet named students with these four headers and three rows. The first row contains field names rather than a student record.

Workbook input: students sheet
roll_nonameclasscity
1Aarav10APune
2Diya10AChennai
3Sneha9BMumbai
  • Save the file as an .xlsx workbook.
  • Use consistent column headers and one student record per row.
  • Keep the sheet name students, or create a named table and adapt the connector reference accordingly.

Step 2: Load Data Using Power Query

For Excel 2016 or later:

  1. Go to Data → Get & Transform Data → Get Data → From Workbook
  2. Select the Excel file
  3. Choose the students sheet or named range
  4. Click Transform Data to open Power Query Editor

Step 3: Run SQL Queries on Excel Table

This section uses the workbook input below. The M filter reads the named students sheet; the ODBC example uses that driver’s sheet notation.

Workbook input: students sheet
roll_nonameclasscity
1Aarav10APune
2Diya10AChennai
3Sneha9BMumbai

Power Query M

This example is Power Query M, not SQL. The Advanced Editor accepts M expressions such as Table.SelectRows. It does not turn a workbook into a server that accepts arbitrary SELECT statements.

Open Home → Advanced Editor to inspect or modify the M transformation below.

Example: Select students from class 10A


let
    Source = Excel.Workbook(File.Contents("C:\Users\You\Documents\school.xlsx"), null, true),
    Students_Sheet = Source{[Item="students",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Students_Sheet, [PromoteAllScalars=true]),
    #"Filtered Rows" = Table.SelectRows(#"Promoted Headers", each ([class] = "10A"))
in
    #"Filtered Rows"
roll_no | name   | class | city
--------+--------+-------+---------
1       | Aarav  | 10A   | Pune
2       | Diya   | 10A   | Chennai

Step 4: Using Microsoft Query (Legacy Method)

This section uses the workbook input below. The M filter reads the named students sheet; the ODBC example uses that driver’s sheet notation.

Workbook input: students sheet
roll_nonameclasscity
1Aarav10APune
2Diya10AChennai
3Sneha9BMumbai

SQL · portable concepts; preserved examples identify their engine

Alternatively, use Microsoft Query to run SQL:

  1. Go to Data → Get Data → From Other Sources → From Microsoft Query
  2. Choose "Excel Files" as the data source
  3. Select your workbook
  4. Write SQL like:
SELECT name, city FROM [students$] WHERE class = '9B';

Step 5: Connect to External SQL Database via Excel

  1. Go to Data → Get Data → From Database → From MySQL/SQL Server
  2. Enter server details, authentication, and database name
  3. Select the table and load or transform with Power Query

This is useful when your student data is stored in a centralized MySQL database on a server.

Real-World School Use Cases

  • Generate attendance reports from Excel with SQL filters
  • Create pivot-style summaries using GROUP BY
  • Merge student and marks sheets using JOIN-like operations
  • Feed SQL-transformed Excel tables into Power BI

Tips and Best Practices

Query discussed in this section:

SELECT name, city FROM [students$] WHERE class = '9B';
  • Name your tables/ranges clearly
  • Avoid merged cells — SQL hates them
  • Use Power Query instead of formulas for better scalability
  • Save a backup before applying SQL transformations

Important differences and mistakes

The retained Advanced Editor example is M, not SQL-like syntax that can accept arbitrary SELECT statements. Connector menus differ across Excel versions and platforms. Protect refresh credentials, preserve clear headers and verify refresh results before replacing a report.

Try it with the school data

Express the same class-10A filter in SQL and compare its two-row result with the preserved M example.

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

Worked answer and reasoning

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

SQL · this practice variation runs with SQLite 3; the preserved examples retain their stated dialect

SELECT name, class, city FROM practice_students WHERE class = '10A' ORDER BY roll_no;
Expected query result
nameclasscity
Aarav10APune
Sneha10APune

Both operations can describe a class filter, but SQL runs on a database while the shown Table.SelectRows transformation is M. Their input fixtures are separately identified.

Check your understanding

Which language is Table.SelectRows in the Power Query Advanced Editor?
Choose one answer. You can try again.

Answer and explanation

Power Query M.

Both operations can describe a class filter, but SQL runs on a database while the shown Table.SelectRows transformation is M. Their input fixtures are separately identified.

Use the idea independently

Excel can import and transform database data through supported connectors. Power Query transformations use the M language; they are not automatically SQL statements executed by a workbook.

Both operations can describe a class filter, but SQL runs on a database while the shown Table.SelectRows transformation is M. Their input fixtures are separately identified.

Continue your understanding with Select Data and Connect with JDBC and ODBC.

Source for the engine-specific behavior: Microsoft Power Query documentation.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Connecting to SQL Databases via JDBC/ODBC — Access Your Data Securely from Applications →