The Practical SQL Guide I wish I had when I started Data Analysis
By: Jeet Chaudhuri
Tags: Data Science, Data, Programming, Sql, Data Analysis

Photo by Aron Visuals on Unsplash

SQL has become a common skill requirement across industries and job profiles. Companies demand that their data analysts, data scientists and product managers are at least be familiar with SQL. This is because SQL remains the language of data. So, in order to be data-driven, people need to know how to access and analyze data.

When I looked for resources to learn SQL for my role as an Analyst and prepare for interviews, all the websites had a very standard set of ‘Employee — Salary’ tables with very basic queries. While I did understand the basics of SQL well from such examples, it missed on which concepts were most important in a practical setting where we usually work with bigger queries. So I decided to summarize the key tips which I wish I had emphasized on when beginning my journey.

Know your basic query structure

This is the most basic and important tip. No matter how complex the problem, you should have the basic structure in mind and breakdown every part of your query into this structure. Even when I work with multiple tables and joins it always help to keep the basic structure in mind.

Basic SQL Query Structure

Know your Ranks and Partitions

In my years as an analyst and giving several interviews RANK/SUM OVER (PARTITION BY ____ ORDER BY) is one of the most used functions. This is used to rank rows based on particular value and partitioned by other attribute or find rolling value of aggregate functions.

It is important to always use row_number over rank/dense_rank when you need unique ranks for all your rows. (The difference between 3 types on rank functions is a common interview question. Here is a good summary for reference.)

These functions can help us in multiple problems for example :

Finding the latest entry in a particular filter —

Finding cumulative sums —

Removing duplicate rows from a table-

and many more problems which I might have not even encountered yet.

Working with Time and Strings

The most common filters I have worked with usually are based on time and strings. Here are some important functions to know for manipulating time

Important date functions

For string, you should be familiar with all the conditions used in LIKE functions to identify patterns. You can refer to a comprehensive list, but to summarize % is used to denote any number of characters while _ is used to denote a single character. [] can be used in SQL Server to check for multiple characters. Eg. Find names in a Column name where 2nd character has a vowel and last character is ‘t’ —

SELECT name from Table WHERE lower(name) LIKE '_[aeiou]%t'

One extra tip, if you export results as .csv always remember to remove commas in all the string column to ensure proper formatting of the file. Use regexp_replace(column_name, ’,’ , ’’) as column_name to remove commas in the column.

Get comfortable with Joins and use proper formatting

Unfortunately you would almost never work with just one table, and most queries will have many sub-queries and joins. So you must always breakdown your given problem into simpler queries and then approach the problem. Basic knowledge of joins is very important in this case. (Also a very common interview question).

The most common join I use is the left join, which is we need to keep all the data from the first table whether or not we get a match in the second table. Let’s see this in the example in the next section.

Also when working with multiple tables, code formatting with proper indentations is the most important. Always keep this in mind while writing any code — if I give this to a 3rd person how easy would it be to comprehend.

Use Cases for filtered counts and sums output

You can always use case when to filter your output, like you use count if in Excel.

(Also another common interview question is the difference between count(*) it returns the number of rows null and non-null vs count(1) Will count all the rows since the expression “1” evaluates to non-null for every row vs count(column_name) Counts rows with non null value for given column)

Let’s see all this in action in the following query where we will try to find out the top Uber drivers in Amsterdam who had the highest cancellations and see their login hours and completed rides. We’ll join the login hours table which has the details of all the logged in drivers with the bookings table which has all the bookings with their status as ‘completed successfully’ or ‘completed early’ or ‘cancelled by customer’ or ‘cancelled by driver’.

I hope this guide helps you in becoming more confident in writing your SQL queries and tackle next problems you face either as an Analyst or even as an interviewee.

Thanks for reading.