• Sloth Bytes
  • Posts
  • 🦄 5 SQL Tips That will Save Your Life

🦄 5 SQL Tips That will Save Your Life

Sponsored by

Hello friends!

Welcome to this week’s Sloth Bytes. I hope you had a great week 😁 

Learn AI in 5 minutes a day

This is the easiest way for a busy person wanting to learn AI in as little time as possible:

  1. Sign up for The Rundown AI newsletter

  2. They send you 5-minute email updates on the latest AI news and how to use it

  3. You learn how to become 2x more productive by leveraging AI

5 SQL Tips That will Save Your Life

(Ok a little dramatic, but I’m a YouTuber.)

I said last week that I’ll do a SQL one 😁 

When I first learned SQL, I thought:
"This is easy. Just SELECT some stuff and vibe."

And it was easy for a while until:

  1. I had to learn how JOINs worked

  2. I started doing more advanced queries where it reached a point it was so unreadable even I couldn’t tell what it did… and I wrote it an hour ago

So today I’m dropping a few byte-sized SQL tips that saved my sanity and made me feel like I finally knew what I was doing.

1. Always use LIMIT while exploring

SELECT * FROM users LIMIT 10;

Why: When you're querying a big table and you're just trying to ā€œsee what’s in there,ā€ you don’t want to accidentally return millions of rows.

You will crash your tab, your editor, and possibly your will to live.

Use LIMIT 10 (or even 5) while testing.

Bonus tip: Use ORDER BY created_at DESC in there to see the most recent entries. You'll look like you know what you’re doing.

2. Be explicit with your JOINs

-- šŸ”„ Terrifying
SELECT * FROM users, orders;

-- āœ… Professional
SELECT * FROM users 
JOIN orders ON users.id = orders.user_id;

Why: That first query creates a Cartesian product or if you’re fancy a CROSS JOIN, meaning every row in users is combined with every row in orders. If you have 10,000 users and 20,000 orders... well, that’s 200 million rows. Congrats, you just created a performance nightmare.

Always use ON conditions when joining. It’s a best practice and Your future self will thank you.

3. Use Aliases for Table and Column Names

-- Without aliases
SELECT employees.employee_name, departments.department_name
FROM employees
JOIN departments ON employees.department_id = departments.department_id;

-- With aliases
SELECT emp.employee_name, dept.department_name
FROM employees AS emp
JOIN departments AS dept ON emp.department_id = dept.department_id;

Why: Makes your SQL code more readable and reduce the amount of typing required when working with long tables and column names.

4. Never run DELETE or UPDATE without a WHERE

-- 😵 Bye-bye data
DELETE FROM users;

-- 😌 Peace of mind
DELETE FROM users WHERE id = 123;

Why: I don’t care how confident you feel. Never EVER delete or update without a WHERE clause.

Pro tip: Run a SELECT first with the same WHERE condition.
If it returns what you expect, then run your DELETE or UPDATE.

SELECT * FROM users WHERE id = 123;
-- Looks good?
DELETE FROM users WHERE id = 123;

5. Select ONLY What You Need

-- Bad Practice
SELECT * FROM users;

-- āœ… Better
SELECT user_id, first_name, last_name FROM users;

Why: SELECT * is fine when you're exploring but don’t do it for real queries.

You’ll have these issues:

  • Slower performance (especially on wide tables)

  • Wasted memory and bandwidth

  • Accidental exposure of sensitive columns (hello, user_password)

  • Fragile queries that break when the schema changes

Be intentional.
Only grab the columns you need . It makes your code faster, clearer, and safer.

Think of it like going to the grocery store:
Do you take everything off the shelf just in case, or do you grab what you actually came for?

Exactly.

If I’m wrong about any of these, please let me know. SQL is not my strongest skill….

Thanks for the feedback as always :)

Open Source Email: Zero

I’m bringing this back. No idea why I stopped. I think I got lazy…

What is Zero?

Zero is a cool open source alternative to email and the founders Adam and Nizzy have been cooking.

They’ve also started adding AI features to email:

  • AI that can answer questions about your inbox

  • Mass unsubscribe

  • Auto-labeling

  • Auto-summarization

The crazy thing is this is still in OPEN BETA. Yeah… lots of potential here.

Make sure to reply to their tweet here by saying ā€œThe sloth sent meā€

 

Thanks to everyone who submitted!

No Yelling

Create a function that transforms sentences ending with multiple question marks ? or exclamation marks ! into a sentence only ending with one without changing punctuation in the middle of the sentences.

Examples

noYelling("What went wrong?????????")
output = "What went wrong?"

noYelling("Oh my goodness!!!")
output = "Oh my goodness!"

noYelling("I just!!! can!!! not!!! believe!!! it!!!")
output = "I just!!! can!!! not!!! believe!!! it!"
# Only change repeating punctuation at the end of the sentence.

noYelling("Oh my goodness!")
output = "Oh my goodness!"
# Do not change sentences where there exists only one or zero exclamation marks/question marks.

Notes

  • Only change ending punctuation - keep the exclamation marks or question marks in the middle of the sentence the same (see third example).

  • Don't worry about mixed punctuation (no cases that end in something like ?!??!).

  • Keep sentences that do not have question/exclamation marks the same.

How To Submit Answers

Reply with

  • A link to your solution (github, twitter, personal blog, portfolio, replit, etc)

  • or if you’re on the web version leave a comment!

  • If you want to be mentioned here, I’d prefer if you sent a GitHub link or Replit!

New video coming out soon!

I think this one will be about programming mistakes every beginner makes.

That’s all from me!

Have a great week, be safe, make good choices, and have fun coding.

If I made a mistake or you have any questions, feel free to comment below or reply to the email!

See you all next week.

What'd you think of today's email?

Login or Subscribe to participate in polls.

Want to advertise in Sloth Bytes?

If your company is interested in reaching an audience of developers and programming enthusiasts, you may want to advertise with us here.

Reply

or to participate.