Basic SQL: Ranking Best Cities for Avocado Lovers
By: Amanda West
Tags: Avocado, Sql, Basic Sql, IBM, Data Science

I’ll be the first to admit, I’m basic. Avocado toast, pumpkin spice latté, Christmas-lights-in-July kind of basic. I have an unhealthy Mason-jars-to-food ratio. If Trader Joe’s and Target were heaven, I wouldn’t even be mad (especially the latter; my local one has a Starbucks).

But I’m also a master’s candidate in data science, and what the world didn’t teach me about SQL I decided to learn myself. I recently finished IBM’s SQL for Data Science Certificate and wanted to try getting my hands a little dirtier with it.

And then, the holy grail: avocado.csv

(1) Gathering the data

I took the easy road here and scoured Kaggle (in addition to Kaggle Competitions, they also have tons of clean datasets). Voilà, a treasure trove of avocado data by region, size, PLU code, and date.

(2) Create tables to be populated

Before we can load in data, we need to create an empty matrix to populate. Here are a few quirks with SQL regarding this:

One can create column names in mixed case or using reserved keywords (like Date), but we’ll need to reference them with apostrophes (‘Date’) in later use. It’s better to not name columns after reserved keywords.

Underscores, letters, and numbers are A.O.K.

SQL doesn’t like variable names that start with a number or contain any special characters (including spaces). Technically, one could use brackets [the date] , but it’s considered better practice to not have these things at all.

SQL likes column names to be in the first row ONLY

Common variables are int, decimal, date, char(x), and varchar(x). Int will truncate decimals, so I went with decimal.

The x in char() denotes the number of characters in every entry (so char(2) would be for a column with state names such as AZ, WI, TX). varchar(x) doesn’t denote character length by byte size, and one character can be more than one byte; pick a value a little larger than your longest string of characters for that row. I used varchar(50).

NOT NULL just specifies that the entries in a column must be filled in. An error will pop up if there isn’t anything.

Remembering these things from my IBM course, I made a few quick adjustments in Excel to help the data read easier (i.e. I deleted a few title rows, renamed some columns to start with letters instead of numbers, and wrote underscores in place of spaces).

Most importantly, the primary key needs to be a unique identifier, so I also added in a column that counted 1–18,249. For maximum laziness, you can just take my final version of the dataset here.

(3) Loading in the data

I’m going to use IBM db2 Cloud to upload the data and access it with SQL. If you’re following along, feel free to use any similar cloud service.

I’m not going to explain a lot about how I loaded the data in here; if you’re using a cloud service, there is no coding required to load in new data (besides initially creating the empty table).

Notes:

Be mindful of the date variable. You’ll need to correctly specify its format for SQL to understand it

Make sure to overwrite the data into the file, not append it!

If all goes well, then the service should give you a success message:

(4) Looking at the Data

One now has a clean dataset they can run in SQL. I’m going to look at the newest date available, March 25th 2018. I’m also going to drop regional data that is not a city; I don’t want to accidentally compare Houston to the entire Midwest!

Now we can throw some interesting questions at it right here from SQL, such as average price, volume, and specifying for only organic values;

So, what’s the best city for avocado lovers?

TLDR; Houston, Texas

Long Answer: it depends. If you only eat organic, perhaps the Rochester/Buffalo area ($1.03); if you’re a bang-for-your-buck kinda guy, then Houston or Phoenix is a steal at 56 and 59 cents a pop, respectively. If money is no object and you just want premium access to avocados galore, then I suppose you should try Los Angeles or Houston.

Because Houston ranked 1st in average price and 2nd for total volume, I think that unofficially crowns Houston as the winner. Next time you visit, buy some avocado toast for me! ❤

(5) Conclusion

SQL is great for communicating with a database to select, merge, delete, create, order, or update tables. You can narrow down what you’re looking for very easily with commands like avg(), max(), min(), count() and distinct and find simple summary statistics. That being said, I would probably prefer at this point to begin using Python pandas in tandem (or R) in order to perform anything more strenuous.

This was my very first data science blog ever, and I hope someone enjoys it. Next time I’ll go over working with SQL and pandas using Jupyter Notebooks.

If you need me, I’ll be in Houston!