Aliases — Learning SQL Journal — Part 1

I came across a Salesforce SQL Interview question from StrataScratch, at first I thought it was a pretty straightforward, but this question taught me a few things, and I want to share them with you.

Here is the preview of the dataset:

Basically, for each carmaker (ie Ford) we want to find out if they produced more (or less) product names (Mustang, Focus, Explorer, etc.) the next year.

My first approach is to select company_name (ie Ford), in year 2020 using the following SQL query:

I thought I would half way done, once I have the 2019 query and join the two tables. I realized I have to figure out how to subtract the 2019 count column from the 2020 count column.

Solution:

To solve this problem, I have to give an alias to the count columns (C1 and C2). We then can easily use them to subtract C1 column from C2 column on the first SELECT statement.

Conclusion:

When I first started learning about SQL, I thought the sole purpose of using an alias in a SQL query is similar to using alias when importing modules in Python (import pandas as pd): to avoid having to type “pandas” everytime we want to call it. Though this is true, aliases are used so we can use them in a higher level query.

My name is Johan Halim, I am a SharpestMinds Data Science mentee. I have experience in Accounting, and I am currently looking to transition into a Data Analyst role. If you are recruiting for a Data Analyst role, please reach out to me on my LinkedIn.

Thank you.

--

--

--

Los Angeles, CA / Data Analyst / SQL and Python

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Intermediate Certificates on Localhost for Mac

Airbyte or Meltano — and why I use neither of them

How to build the perfect Docker image — Part I — Keep it lightweight

Advancing a Web Platform & Product: Assessment Training

Why Spring boot, a variant of Java, is so Famous?

Optimising OBS on a 2020 MacBook Pro

Custom dialog in Flutter

How to write custom tamper scripts for sqlmap

Some of the payloads sent by sqlmap. Note the presence of AND keyword.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Johan H

Johan H

Los Angeles, CA / Data Analyst / SQL and Python

More from Medium

Sea salt vs Table salt: What’s the right pick for you?

Why One should learn Java : Object Oriented: In Java, everything is an Object.

Connected Data: Delivering Better Project Outcomes

ChefWorthy Do’s & Don’ts