Run SQL Queries in Excel Like a Normal Formula
The xlwings add-in adds a sql worksheet function that takes a SQL query and one or more ranges and spills the result into the sheet. It uses the SQLite dialect and recalculates like any other formula when the data changes. It needs Python, xlwings and Excel on Windows.
Install xlwings and the add-in
You need Python with the xlwings package and the xlwings Excel add-in. The SQL function is built into the add-in, so there is nothing else to install. It works in Excel on Windows only.
Install the package from the command prompt or terminal:
pip install xlwings
Then install the add-in:
xlwings addin install
Close and reopen the workbook, and a new xlwings tab appears in the ribbon.

Write your first SQL formula
Type =sql( in a cell and pass two arguments: the query and the table range. The query can be typed straight into the formula or, easier to edit, kept in another cell that the formula points to. The full signature is =sql(query, table a, table b, ...).
In my example the formula points to cell A12 for the query and to the books table in A2:E10. While A12 is still empty, the formula shows an error. That is expected.

Inside the query, the first range is called a, the second b, and so on. Since xlwings 0.33.5 you can give a range your own name by putting the name in quotes before the range, for example "books", A2:E10. This query selects the ID and title of every German book, and the result spills into the cells below:

Group and join tables
GROUP BY, aggregate functions and joins work as in any SQLite query. Each table you use in the query must be passed to the formula as its own range, in the order a, b, c.
The example workbook on GitHub has every query below in row 12, highlighted in blue. This one counts the book titles per language:

Next, an inner join between the books table and the editors table. With only the books range in the formula, SQLite reports that there is no such table:

Add the editors range as a second argument, so books are a and editors are b, and the join returns each title with the editor’s name:

Joining all three tables works the same way: pass a third range for the authors. The ranges can also sit on different worksheets.

Limits
The formula only works where Python and the xlwings add-in are installed, and only in Excel on Windows. Anyone who opens the workbook without them sees errors instead of results, and Excel warns about links to external sources. Before you share the file, copy the results and paste them as values.

If your colleagues cannot install Python, look at the SQLOOKUP add-in for SQL in Excel instead. If you like formulas powered by Python in general, see how to create custom Excel formulas with Python.
