Moving from spreadsheet thinking to SQL
In a spreadsheet the cell position carries meaning; in SQL the column name and the row content do. Once that shift lands, the way to split a file becomes obvious.
Rows are records, columns are fields
One row is one observation — an order, a session — and one column is one attribute of it. A sheet with title rows and subtotal rows breaks the rule. Keep subtotals out of the table and compute them with a query.
One kind of observation per table
When orders and customers share a sheet, customer details repeat on every row. Fixing a name means finding every row, and missing one splits the data. Splitting into an orders table and a customers table joined by a key removes the problem.
The formal name is normalisation. In practice two tables are enough to remove most duplication.
| Spreadsheet habit | In a SQL table | Why |
|---|---|---|
| subtotal rows | SUM in a query | separate data from calculation |
| merged cells | fill the value into each row | every row needs meaning |
| colour as status | a status column | colour cannot be queried |
| several tables on one sheet | separate tables joined by key | prevents duplication |
| two header rows | one header row | one name per column |
Wide versus long
Twelve month columns (wide) read well but query badly: comparing months means naming twelve columns. One date column and one amount column (long) handles every month with a single GROUP BY.
Reports are the opposite — wide is easier to read. Store long, display with a pivot or a chart.
-- wide: jan, feb, mar ...
-- converted to long:
SELECT month, SUM(amount) FROM sales_long GROUP BY month;
Formulas become queries
SUMIF is SUM with WHERE, COUNTIF is COUNT with WHERE, VLOOKUP is JOIN. A pivot table is GROUP BY on two columns, and a sorted unique list is SELECT DISTINCT. Translating one spreadsheet function at a time shows how little syntax is actually needed.
The order to migrate in
First decide what one row means. Keep only that observation's attributes as columns, and split other kinds of observation into their own tables. Then give each table a unique key and check that joining on it preserves the original row count.
Following that order prevents the multiplying join before it happens. Starting with one wide sheet instead lets duplicates accumulate until no one can tell which value is correct.
Questions
Can I load a sheet as it is?
If the sheet holds exactly one table, yes. Clean title rows, subtotals and merged cells first.
How do I join tables without a key?
A shared value such as a customer name works, but a single typo breaks the link. Create a unique id where possible.
Sources
Every query in this guide runs against the built-in sample data.
Open the lab