- The Query
- Posts
- 🤓 SQL best practices for writing clean code
🤓 SQL best practices for writing clean code
Read Time: 3 minutes
Morning crunchers! The Query here — the data newsletter that’s like a primary key for your data career. We help ensure a good relationship throughout your learning journey.
Here’s what we have for you today:
SQL best practices for writing clean code 🧹
A video game portfolio project dataset 🕹️
Another great SQL function for cleaning string data 🧵
A classic meme 🤝
def learn_data_analysis(👨💻):
1. Announcement — Ultimate Guide to Becoming a Data Analyst!
We’ve been working hard on an Ultimate Guide to Becoming a Data Analyst containing a roadmap and the best resources for learning.
Our goal with this guide is for it to be the only resource you need to learn to become a data analyst... because it links to the best resources that exist on the internet.
It will be free on our blog 🙂
If you have any resources that have really helped you learn, can you send them our way? We’d love to check them out and decide if we want to include them in this guide.
We’ll be releasing this guide at some point over the next few weeks!
2. SQL best practices for writing clean code 🧹
When I was learning SQL, I’d come back to a query I had written a month prior and it’d be really difficult to remember what the query does.
Writing clean code fixes this problem.
Here are a few things to keep in mind when it comes to clean SQL:
Style
Readability
Maintenance
Indentation
Comments
In this article, Martin Ganchev goes over each of these in depth.
Highly recommend!
select * from dataset-of-the-week
Do you like video games?
This week’s dataset of the week is the Global Video Game Sales by The Devastator on Kaggle.
Here are the columns you’re working with:
And here are some questions to get you started on a portfolio project:
What’s the relationship between video game expenditure and user satisfaction? How do consumers behave?
What are the most popular platform-genre combinations in the top 100 games? How can you use these insights to inform game development decisions?
class MiniLesson:
Cleaning Data with TRIM
This week we've got another data cleaning function for you!
Extra spaces before and after words in SQL columns can drive analysts crazy.
This can happen for a lot of reasons — a common one is when a user accidentally types a space before or after their email in an online form or survey.
There are often processes in place to ensure this doesn't happen, but that's not always the case.
I can't tell you how many times I've tried for hours to figure out what's wrong with my SQL query only to realize that one of the fields has a sneaky space in front of it!
To combat this you can use the TRIM function in SQL.
The TRIM function is a string manipulation function that removes leading and trailing spaces from a given string.
Let's consider an example where we have a table named “users” with user information that includes first names and email addresses with extra spaces.
We want to remove the extra spaces from the first_name and email columns.
We can use the TRIM() function in a query to achieve this:
Trimming the extra spaces allows us to do things like JOIN on those columns or use them in the WHERE clause appropriately.
As a data analyst, understanding how to use the TRIM() function in SQL is essential, as it allows you to clean and standardize text data within your queries.
By using the TRIM() function, you can ensure that your queries, joins, and comparisons work correctly and avoid issues caused by extra spaces in user-submitted data.
This, in turn, improves the accuracy and reliability of your data analysis.
import memes as 😂
That’s it for today.
Stay crunchin’ folks and see you next week!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
What'd you think of today's newsletter? |