Just a cute pic of me
by Mike McKee
Data

Let's Talk About SQL

66 Days of Math and Programming -- Day 25

Today, I’m talking about something that should have been mentioned earlier, especially since I love data analytics. In fact, this topic hasn’t been mentioned once in my 66 Days of Math and Programming challenge so far.

It’s…

SQL.

It’s a data analyst’s best friend.

Yet I’ve waited so long to talk about it since I’ve had a crush on Python over the last month.

Anyway, let’s talk about SQL…

Today, I’m focusing on my favorite way to practice the language.

My preferred learning method (aside from doing projects) is doing LeetCode problems. If you’re in the Dorky World of Data and don’t know about LeetCode, we’ve got problems -- you’ve made my hit list.

Jokes aside, LeetCode is a great learning and practice tool for interview-style programming questions.

They’ve got every language out there, including SQL of course.

The boring way of doing LeetCode problems is solving the question and then moving on to the next one.

But that’s not what I do.

I like solving a problem and then redoing it by finding a new answer.

No two programmers or data analysts will have the exact same code. Most languages have flexibility, and you can solve problems using different methods.

Doing this helps me understand why what I’m writing works.

It’s magical.

Let me show you an example…

Leet Code problem i solved

The problem above tasks you with finding the confirmation rate for every user.

My initial thought was to use CTEs (Common Table Expressions) to write a long but simple query as you see below.

First SQL query using CTE

This one took a bit of thinking, but I’m not afraid of using CTEs. They may look daunting, but they’re a fundamental part of using SQL.

After solving the problem this way, with a solid runtime, if I may add, I looked for a new approach. One that didn’t use CTEs at all.

That’s when I came across this…

Second SQL query

This one took a while to think about.

And I’d be lying if I said I didn’t cheat by peeking at other solutions.

On my own, I don’t think I would have ever thought about putting a CASE statement inside a SUM function.

But I’m thrilled I looked at this problem from a different perspective.

That’s because a few problems later, I came across another one that required me to use a CASE statement inside a SUM function.

Some would call this a coincidence.

But I call it smart studying.