Welcome back, darlings, to our SQL series—a charming little jaunt into the world of data that’s got us all feeling a bit like tech-savvy detectives. Last week, we had a cosy chat about what SQL actually is, the logical order of queries, and a few cheeky best practices to keep you on the straight and narrow.

Now, let’s up the ante a tad. This week, we’re diving headfirst into writing queries. Don’t worry, though—I’ll be with you every step of the way, like your favourite pair of well-worn trainers guiding you through the cobblestones of Covent Garden.
Joins vs. Unions: A Tale of Two Techniques

We’ve already had a flirtation with the SELECT and FROM functions, so our next logical step is understanding joins and unions. Think of these as tools to bring data together, each with its own personality.
Joins
Joins are like matchmaking for your tables. They combine columns from two datasets based on a shared key. Imagine combining your planner with your bestie’s to create one unified social calendar. For example:
INNER JOIN: Matches rows in both tables, like finding mutual plans for brunch.LEFT JOIN: Includes all your plans and sprinkles in theirs, if available.
Unions
Unions, on the other hand, are like stacking multiple guest lists into one. They merge rows vertically into a single result. A union ensures no duplicates unless you explicitly allow them (UNION ALL). Think "combining," not "matching."
WHERE There’s Data, There’s a Way
The WHERE clause is your filter. Like choosing only the fluffiest clouds for your Instagram feed, it ensures you only get the rows you care about. Need all employees in Sales? Easy:
SELECT *
FROM employees
WHERE department = 'Sales'Here's some very simple code
Add a touch of flair with LIKE, where % is your wildcard and _ your single-character stand-in. It’s pattern-matching with a sprinkle of intrigue:
SELECT *
FROM products
WHERE name
LIKE 'T_ddy'
;Here's an example of adding LIKE. This means the results brought back could be Teddy or Taddy? The choice is yours.
GROUP BY: Organised Chaos
The GROUP BY clause is for when you’re ready to get a bit fancy—aggregating data into digestible bites. For example, counting employees by department:
SELECT department
,COUNT(*) AS employee_count
FROM employees
GROUP BY department
;Here we're grouping by the departments in our store, very simple
HAVING: The Sophisticated Filter
Think of HAVING as WHERE for the refined set—it filters groups, not rows. Want only departments with more than 10 employees? Voilà:
SELECT department
,COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING
COUNT(*) > 10;Exactly the same, but this time we're asking to bring back only departments with more than 10 employees.
ORDER BY: A Touch of Elegance
Finally, we sort. ORDER BY gives your results a polished finish, like organising your bookshelf by colour (because aesthetics matter):
SELECT name
,salary
FROM employees
ORDER BY salary DESC
;Here we're asking to order the rows descending by salary
Pulling It All Together
Here’s how you turn all these building blocks into a show-stopping query. Picture this: you’ve got sales data, and you’re curious about products starting with "Pro," sold in 2025, grouped by region and product, with total sales over £10,000. Oh, and you want it sorted by top sellers. Easy peasy:
SELECT
region
,product
,SUM(sales_amount) AS total_sales
FROM
sales_data
WHERE
product LIKE 'Pro%'
AND YEAR(sale_date) = 2025
GROUP BY
region
,product
HAVING
SUM(sales_amount) > 10000
ORDER BY
total_sales DESC
;Combining all of the above into one query.
SQL as a Second Language
Learning SQL is a bit like mastering French or perfecting the art of the smoky eye—it takes patience, practice, and a pinch of determination. But trust me, with time, you’ll be reading and writing queries as fluently as you sip your Earl Grey.
If the syntax ever trips you up, here’s a fabulous resource to get you back on track: Snowflake SQL Documentation.
Until next time, keep querying like the data divas you are.
Song of the Day:
~S Xoxo

