SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
Using SQL in Excel
1. How To Use SQL In Excel
The MySQL For Excel Add-inn
Author: Victor Cocis
Contact: victorc@web.de
2. Introduction
Processing large amount of data, like in-place update, or wildcard-based
filtering, is hard to manage using Excel’s tools, but with a little help of SQL
can be done easy
With the MySQL for Excel Add-in you can do this in 3 simple steps:
Export data from Excel to a local MySQL Server
Use a MySQL client to process the data
Import results back to Excel
3. Needed Tools
Visual Studio 2010 Tools for Office
Runtime. Download it from Microsoft
The MySQL for Excel Add-in (free).
Download it from Oracle
A standalone MySQL Server. This
example uses mini-server, portable,
download it from Sourceforge
A MySQL client. Recommendation
HeidiSQL, portable, download it from
HeidiSQL
4. Installation
The Visual Studio Runtime
and the MySQL for Excel
Add-in are coming as
Windows Installers
After running the
installation, the add-in will
appear in the „Data“ Tab of
Excel.
Mini-Server and HeidiSQL
have portable versions, just
unzip them in a folder of
choice
6. Connecting All
Together
Start the Mini-server by clicking
the mysql_start.bat file from the
installation folder. A command
prompt will inform you if the
server is running
Connect HeidiSQL and create a
database (These steps are
necessary only once)
Start HeidiSQL from its
installation folder
(heidisql.exe)
In the right bottom corner
click New -> Session in root
folder
7. Connecting All Together
The session manager will create
a session called „Unnamed“,
rename it to e.g. „mysession“
Set following connection
parameters
Hostname: „localhost“ or
„127.0.0.1“
User: root
Password: root
Port: 3311
Database: blank
8. Connecting All
Together
In the screen that opens
right-click the session
name („mysession“ in this
example) the Create new -
> Database and enter a
database name, e. g.
„mydatabase“ in the form
that opens. You will then
see the new database on
the right panel.
*Note: information_schema and
mysql are system databases
9. Connecting All Together
Connect Excel tot he database
(These steps are necessary only
once)
Open a blank excel workbook
and choose Data from the menu.
You will see the MySQL for Excel
panel on the left, as described
in Installation
Click on New Connection and
enter the same parameters as
described in slide #7 then click
OK
10. Connecting All Together
In the panel double-click the connection to open it, then the database to connect
to
11. Practical
Examples
For the examples we will use
A Price and Stock List
Name Category Price Stock
Ballantines Finest Scotch 58.90 120
Bowmore 12 Years Single Malt 48.90 111
Canadian Club Canadian 50.90 114
Canadian Club 12 Years Canadian 43.90 108
Chivas Regal 12 Years Scotch 32.90 no stock
Chivas Regal 18 Years Scotch 32.90 120
Crown Royal Canadian 37.90 102
Crown Royal Reserve Canadian 33.90 108
Dalmore Scotch Single Malt 35.90 101
Dewars 12 Years Scotch 35.90 118
Dewars White Label Scotch 35.90 118
Glenfiddich 12 Years Single Malt 38.90 114
Glenfiddich 15 Years Single Malt 35.90 108
Glenfiddich 18 Years Single Malt 37.90 no stock
Glenlivet 12 Years Single Malt 39.90 no stock
Glenlivet 15 Years Single Malt 48.90 115
Glenlivet 18 Years Single Malt 45.90 115
J and B Scotch 57.90 114
Jack Daniels Bourbon 36.90 104
Jameson Irish 40.90 103
Jim Beam Bourbon 40.90 116
Johnny Walker Black Label Scotch 33.90 110
Johnny Walker Blue Label Scotch 56.90 100
Johnny Walker Red Label Scotch 52.90 115
Old Grand Dad Bourbon 59.90 104
Sir Edwards Scotch 34.90 no stock
Teachers Scotch 58.90 110
Tullamore Dew Irish 33.90 115
VAT 69 Scotch 37.90 104
12. Practical
Examples
An Incoming Deliver List
*Note: You can download the lists as
Excel files from
https://www.mediafire.com/file/h8qu8
0uztmgc70f/Whyskey_Sample.xlsx/file
Name Delivered
Ballantines Finest 40
Bowmore 12 Years 50
Canadian Club 40
Chivas Regal 12 Years 60
J and B 55
Jack Daniels 40
Jameson 50
Jim Beam 50
Johnny Walker Black Label 60
Old Grand Dad 30
Sir Edwards 40
Teachers 60
Tullamore Dew 60
VAT 69 50
13. Practical Examples – Export Data And
Create Table
Mark the columns you wish to
export and in the MySQL For Excel
panel click „Export Excel Data To
New Table “
15. Practical Examples – Export Data And
Create Table
Click ok in next screen and in
the panel you will see the wizard
has created the table.
16. Practical Examples – Export Data And
Create Table
Examine the table in HeidiSQL
(click refresh in the upper left
corner of Heidi if the table is not
yet visible)
The wizard has added a
numerical id. First Excel table row
has been used as header, to name
the fields. Column containing strings
have been formatted as varchar
with the maximum length of
contained string. The „Stock“
column is also varchar, as it contains
„no stock“ on some positions.
17. Practical Examples - Update and
Regularize Column
To make the „Stock“ column
numeric, let’s first replace „no
stock“ with 0 (zero). Click the
„Query“ tab in Heidi and run
following query:
UPDATE whiskey SET Stock=0
WHERE Stock="no stock";
18. Practical Examples - Update and
Regularize Column
Click the „Data“ tab
to check modified data
19. Practical Examples - Update and
Regularize Column
Back tot he „Table“
tab, double click the
„Datatype“ oft he Stock
field and choose „INT“
from the dropdown and
then „Save“
20. Practical Examples -
Update and
Regularize Column
Back to Excel, click „Import
MySQL Data“ to update the
changes. You can do this over
existing data, or in a new
Worksheet, or even a new file
The add-in has also created a
connection between the Excel
table and the database. Just
clicking „Refresh“ in the „Data“
tab of Excel and changes in the
database will be reflected in
Excel
21. Practical Examples -
Mirror Back Data
Changes in Excel
Unfortunately changes in Excel
won’t be updated in the database.
„Edit MySQL Data“ provides a way
to change MySQL data, but is a bit
tricky to use. An easy way to do
this:
In Heidi SQL, right click the
table, then choose “Empty
table(s)”. This will delete data, but
preserve the table structure
Back in Excel, select the
columns, then select the table in
the right pane and click “Append
Excel Data to Table”
22. Practical Examples - Mirror Back Data
Changes in Excel
On the next screen
check the box “First
Row Contains Column
Names” and verify that
columns are correctly
mapped. Then click
“Append”
23. Practical Examples - Mirror Back Data
Changes in Excel
Refresh the table in
Heidi to verify data is
correct
24. Practical Examples
– Save Filters
In this example, the whiskey
shop orders weekly drinks where
the stock is below 110 bottles.
In Heidi right click your
database (“mydatabase”) then
choose “If you use certain filters
regularly, you can store them as
views in the database and use
them by just a click Create new”
-> “View”
25. Practical Examples
– Save Filters
Give it a name, e.g. “Order”
and write your SELECT
statement:
SELECT * FROM whiskey WHERE
Stock<110;
then save it.
26. Practical Examples – Save Filters
Back to Excel right click in the panel with data objects and choose
“Refresh Database Objects” to display the view
27. Practical Examples
– Save Filters
Now choose where you want to
display the data, it might be same
sheet, a different one or even a
new file.
Click the “order” view, then
click “Import MySQL Data”
Next time you need the filter,
just update your data as described
in Mirror Back Data Changes in
Excel and click “Refresh” in the
data tab*
*Note: We observed cases when the
view didn’t refresh after a table
update. We recommend to manually
refresh the view from the MySQL client
28. Practical Examples
– Save Filters
The most help is, of course,
in case of complex filters, with
multiple conditions, which you
don’t have to build each time
with Excel filter wizard.
You can also use operations or
features not present in Excel.
For example, the supplier of
the whiskey shop has an offer for
whiskey labeled older than 12
years. Hard to filter such names
in Excel, but very simple using
regular expression in SQL:
SELECT * FROM whiskey WHERE
Name REGEXP "1[3-9] Years";
29. Practical Examples – In-Place Editing
This requires in Excel at least writing a formula and dragging it for all values,
then override the original values. Much simpler with MySQL for Excel.
A simple one: increase all prices by 10%:
Run following query in Heidi:
UPDATE whiskey SET Price=Price*1.1;
then in Excel click “Refresh” in the “Data” Tab or import data to another
sheet or file.
30. Practical Examples
– In-Place Editing
A more complex one: a new delivery is
here, update the stocks. In Excel this needs
some lookups and IF’s and overriding the
original values.
With SQL first export the delivery data as
described in Export Data and Create Table, to
fill data in a “delivery” table
As names are identical, we will use that
field to match the 2 tables. Run following
query in Heidi:
UPDATE whiskey LEFT JOIN delivery ON
whiskey.Name=delivery.Name SET
Stock=Stock+Delivery;
then in Excel click the “whiskey” sheet
and click “Refresh” in the “Data” Tab or import
data to another sheet or file
31. Practical Examples – Delete Rows Based on
Filter Criteria
This is another task not straight forward in Excel but quick to do with SQL.
To delete all products wit zero stock after delivery (because supplier won’t
deliver them anymore), run following query in Heidi:
DELETE FROM whiskey WHERE Stock=0;
then in Excel click “Refresh” in the “Data” Tab or import data to another
sheet or file.
32. Notes
You can use a MySQL client of choice, functionalities described here are
common to most database clients
You can use the actual version of the MySQL Server as a local host installation
Some processes described will show informative or warning pop-ups, these
were ignored in the text, just click “Ok” or “Continue” when they show up
All SQL SELECT statements select all the fields just for readability, use a list
of fields of your need with SQL