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.
Explain and apply
Distinguish SQL queries from Power Query M and trace a workbook-table filter.
Work it out
Express the same class-10A filter in SQL and compare its two-row result with the preserved M example.
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
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.
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.
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.
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.
| roll_no | name | class | city |
|---|---|---|---|
| 1 | Aarav | 10A | Pune |
| 2 | Diya | 10A | Chennai |
| 3 | Sneha | 9B | Mumbai |
- 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:
- Go to Data → Get & Transform Data → Get Data → From Workbook
- Select the Excel file
- Choose the
studentssheet or named range - 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.
| roll_no | name | class | city |
|---|---|---|---|
| 1 | Aarav | 10A | Pune |
| 2 | Diya | 10A | Chennai |
| 3 | Sneha | 9B | Mumbai |
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.
| roll_no | name | class | city |
|---|---|---|---|
| 1 | Aarav | 10A | Pune |
| 2 | Diya | 10A | Chennai |
| 3 | Sneha | 9B | Mumbai |
SQL · portable concepts; preserved examples identify their engine
Alternatively, use Microsoft Query to run SQL:
- Go to Data → Get Data → From Other Sources → From Microsoft Query
- Choose "Excel Files" as the data source
- Select your workbook
- Write SQL like:
SELECT name, city FROM [students$] WHERE class = '9B';
Step 5: Connect to External SQL Database via Excel
- Go to Data → Get Data → From Database → From MySQL/SQL Server
- Enter server details, authentication, and database name
- 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.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
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.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
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;| name | class | city |
|---|---|---|
| Aarav | 10A | Pune |
| Sneha | 10A | Pune |
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
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.