Is learning SQL in Snowflake a rite of passage, or just a new form of torture?
The past few days have been an emotional rollercoaster for us in DS48 and 47. For many, it’s our first time touching any kind of coding language. Some brave souls had never written a single line of code before this week. As for me, I’d dabbled briefly with Python during undergrad—like a summer fling I was never quite serious about. Aside from the odd calculated field in Tableau, coding and I had always been strangers.
So, you can imagine my panic walking into these sessions. And while there were plenty of moments when I wanted to hurl my laptop out the first-floor window, I have to admit—it wasn’t all bad. Not saying I’m the queen of SQL now, but I think I can navigate my way around a basic query without crying into my tea.
Still, why suffer in silence? Why not turn my misadventures into a new blog series? Let’s demystify the basics of SQL: from crafting queries to inserting, updating, and deleting data. We’ll explore creating databases, tables, views, and stored procedures—and, of course, help soothe the trauma of learning SQL (looking at you, Carl).
What Is SQL?
SQL, darling, stands for Structured Query Language. Think of it as having a polite conversation with a highly intelligent database. It’s the language you use to fetch, organize, or modify data. Essentially, SQL is how you turn a colossal mountain of information into something useful and, dare I say, chic.
The Basics of Writing SQL Code
When writing SQL, you have to follow a certain etiquette. At the very least, you must specify which columns you want and from which table. But here’s the twist: SQL demands you declare the columns first, then the table. It’s like saying, “Bring me columns 1 and 2 from table 1,” instead of, “Using table 1, show me columns 1 and 2.”
And while we’re here—Snowflake’s autocomplete only works after you tell it the table. So, my advice? Start every query with this:
SELECT *
FROM your_table_name
LIMIT 50;
Here's some very simple code
This little trick ensures you don’t accidentally retrieve millions of rows and crash your system. Once you’re happy with the result, you can finesse it with more functions.
Logical Order of an SQL Query
Here’s the proper sequence to write your query like a pro:
SELECT
– The columns you want to retrieve. Example: SELECT name, age
FROM
– The table your data lives in. Example: FROM users
JOIN
(optional) – Combine tables if needed. Example: JOIN orders ON users.id = orders.user_id
WHERE
– Filter rows. Example: WHERE age > 18
GROUP BY
(optional) – Group rows for aggregation. Example: GROUP BY country
HAVING
(optional) – Filter grouped data. Example: HAVING COUNT(*) > 5
ORDER BY
– Sort your results. Example: ORDER BY age DESC
LIMIT
– Control how many rows come back. Example: LIMIT 10
If you ever feel lost, Snowflake’s documentation is your North Star.
Best Practices for Writing SQL
Meaningful Names: Tables and columns should have names that make sense. No one wants to decipher cryptic codes like tbl_123
.
Comments Are Your Best Friend: Add notes to explain your thought process. Use --
or //
to comment out lines. Future you (and your teammates) will thank you.
Use Aliases: Shorten table or column names for simplicity.
Example:
SELECT u.name user_name
FROM users u
;
// OR
SELECT u.name AS "User Name"
FROM users AS "Users"
;
Here are the two ways you can Alias your names. If it needs to have a space use AS, if not just put a space and write your Alias there.
New Lines, Please:
Compare these:
Unreadable:
SELECT name,age FROM users WHERE age > 18 ORDER BY age;
Elegant:
SELECT
name
,age
FROM
users
WHERE
age > 18
ORDER BY
age
;
Always format your code neatly. It’s easier to debug and way less stressful to look at.
And there you have it. SQL isn’t just a tool—it’s an art form, one we’re all slowly mastering, tantrums and all. So, let’s grab our laptops, brew another cuppa, and keep coding. Because if SQL has taught me anything, it’s that a little structure can go a long way.
Song of the Day:
~S Xoxo