Back to articles list Articles Cookbook
8 minutes read

5 Tiny SQL Tricks That Make You Look Like a Pro

Want your SQL to look cleaner and feel more professional? These five small but powerful tricks will help you write queries that are easier to read, debug, and share.

You’ve written a few SQL queries, joined some tables, maybe even built a small report. But your queries still feel a bit… clunky.

The truth is, the difference between “getting the job done” and looking like a pro often comes down to a handful of small habits — the kind that make your code easier to read, maintain, and share with others.

In this article, we’ll share five tiny SQL tricks that can instantly level up your queries. They’re simple, practical, and will make you look far more confident with SQL.

If you want to keep improving beyond quick tips, check out our SQL Practice track. It’s packed with real-life exercises designed to help you move past theory and write cleaner, more effective SQL — exactly the kind of skills these tricks build on.

Let’s get into the five habits that can make your SQL feel more polished and professional.

1. Indent Your Code!

It sounds boring. It feels obvious. But good indentation is the first thing that separates a sloppy query from a professional one.

When your SQL is properly indented, it’s easier to read, debug, and understand — not just for your teammates, but for you when you revisit the query weeks later. Poorly formatted SQL often leads to mistakes, missed conditions, or hours wasted scanning a wall of text.

Here’s what we often see from beginners:

SELECT name, email, total FROM customers JOIN orders ON customers.id=orders.customer_id WHERE total>100;

Now look at the same query with consistent indentation:

SELECT name,
       email,
       total
FROM customers
JOIN orders ON customers.id = orders.customer_id
WHERE total > 100;

Same query, completely different experience. You can instantly spot the selected columns, the join, and the filter conditions. Adding or removing a column or condition is no longer a headache.

A few line breaks and spaces make your query clear and professional-looking. It’s a small habit that pays off every time you write SQL.

2. Leading Comma

I’ll be honest: I don’t love this style. But I can’t deny how useful it is.

A leading comma means you start each new column in your SELECT list with a comma instead of ending the previous line with it. The main benefit? It’s much easier to comment out columns without breaking the query.

Here’s the usual way:

SELECT name,
       email,
       total
FROM customers;

Here’s the leading comma style:

SELECT name
     , email
     , total
FROM customers;

If you want to comment out email, you just do:

SELECT name
     -- , email
     , total
FROM customers;

No need to worry about removing commas or fixing syntax errors.

You’ll often see seasoned SQL professionals use this style — not because it looks nicer, but because it saves time when editing or debugging queries.

3. Start Your Filters with WHERE 1 = 1

This trick is in the same spirit as the leading comma. It doesn’t change what your query does, but it makes editing and debugging your filters much easier.

The idea is to start your WHERE clause with a condition that’s always true (1 = 1). That way, every real condition starts with AND, and you can comment them out without breaking the query.

Here’s the usual style:

SELECT *
FROM orders
WHERE order_date >= '2024-01-01'
  AND status = 'Shipped';

Here’s the WHERE 1 = 1 approach:

SELECT *
FROM orders
WHERE 1 = 1
  AND order_date >= '2024-01-01'
  AND status = 'Shipped';

Now, if you need to test or debug without the status filter, you can just comment it out:

SELECT *
FROM orders
WHERE 1 = 1
  AND order_date >= '2024-01-01'
  -- AND status = 'Shipped';

No syntax errors, no fiddling with ANDs. It’s a small trick that makes your query more flexible while you’re building or testing it.

4. Use CASE

This is the SQL construction that many beginners don’t know about — and it’s a game-changer. Think of CASE as SQL’s version of an IF statement. It lets you create your own categories or labels directly in the query, without exporting data to Excel or writing extra scripts.

Here’s a simple example:

SELECT customer_name,
       CASE
           WHEN points >= 100 THEN 'Gold'
           WHEN points >= 50  THEN 'Silver'
           ELSE 'Bronze'
       END AS loyalty_tier
FROM customers;

What this does:
 For every row in the customers table, SQL checks the value in the points column.

  • If points is 100 or more, it returns 'Gold'.
  • If it’s between 50 and 99, it returns 'Silver'.
  • Otherwise, it returns 'Bronze'.

The result is a new column (loyalty_tier) that assigns each customer to a category based on their points.

How it works:

  • CASE starts the expression.
  • Each WHEN defines a condition.
  • THEN defines the value returned if that condition is true.
  • ELSE is optional — it’s used when none of the conditions match.
  • END closes the expression.

You can also use CASE in other parts of a query — not just in SELECT. For example, you can group data based on ranges:

SELECT
    CASE
        WHEN total < 100 THEN 'Small'
        WHEN total < 500 THEN 'Medium'
        ELSE 'Large'
    END AS order_size,
    COUNT(*) AS orders_count
FROM orders
GROUP BY
    CASE
        WHEN total < 100 THEN 'Small'
        WHEN total < 500 THEN 'Medium'
        ELSE 'Large'
    END;

Once you learn to use CASE, you stop relying on external tools for quick categorization — and that’s one of the skills that separates a beginner from someone who’s really comfortable with SQL.

For more ways to use CASE, check out our articles:

This single construction often marks the difference between simply knowing SQL syntax and actually thinking in SQL.

5. Use COALESCE

The COALESCE function is one of those surprisingly useful tools that many beginners overlook.
 It’s simple but powerful: it returns the first non-NULL value in a list of expressions.

This makes it perfect for handling missing data, providing fallback values, or adding meaningful defaults directly in your query.

Example 1: Fallback Values

If you have several phone number columns and want to show the first available one:

SELECT customer_id,
       COALESCE(phone_mobile, phone_home, phone_office) AS contact_number
FROM customers;

Here, SQL checks phone_mobile first. If it’s NULL, it tries phone_home. If that’s also NULL, it tries phone_office.  The query always returns the first non-NULL value — so you don’t have to write nested CASE statements.

Example 2: Meaningful Defaults

You can use COALESCE to replace NULLs with a default value that makes sense for your context.

For text fields:

SELECT COALESCE(middle_name, '') AS middle_name
FROM customers;

If middle_name is NULL, the query returns an empty string instead.

For numeric fields:

SELECT product_name,
       price - COALESCE(discount, 0) AS final_price
FROM products;

If discount is NULL, it’s treated as zero, so the calculation still works without errors.

Example 3: User-Friendly Notes

You can even use it to add descriptive placeholders:

SELECT order_id,
       COALESCE(notes, 'No notes') AS order_notes
FROM orders;

If a note is missing, the query displays 'No notes' instead of a blank or NULL.

COALESCE is one of those small but mighty functions that instantly makes your SQL more robust and user-friendly. Once you get used to it, you’ll find yourself using it everywhere to tidy up messy data.

Bonus Big Trick: Use CTE

CTEs (Common Table Expressions) aren’t exactly a beginner feature — but once you start using them, they change the way you write SQL.

A CTE lets you break a query into logical parts and give each part a name. This makes complex queries easier to read, debug, and maintain. Instead of stacking subqueries inside subqueries, you can build your logic step by step.

Here’s a simple example:

WITH high_value_orders AS (
    SELECT customer_id, total
    FROM orders
    WHERE total > 1000
)
SELECT customer_id,
       COUNT(*) AS big_orders
FROM high_value_orders
GROUP BY customer_id;

Here’s what’s happening:

  • The WITH clause defines a temporary result set called high_value_orders.
  • You can then refer to it in the main query as if it were a regular table.
  • This structure is much cleaner than writing a long nested query.

If you need to change the definition of high_value_orders, you can do it in one place.

If you’re ready to go beyond simple queries, check out our Recursive Queries course. It covers CTEs in depth — including advanced patterns like hierarchical queries and recursion.

CTEs are a big step toward writing SQL that’s not just correct but also elegant and maintainable.

Wrapping Up

The tricks we covered — from clean indentation to using CASE, COALESCE, and even CTEs — aren’t just clever shortcuts. They’re habits that make your SQL cleaner, easier to debug, and far more professional.

Once you pick them up, you’ll never want to go back to messy, hard-to-read queries.

If you’re serious about getting better at SQL, these tips are just the beginning. The best way to keep improving is to practice across different topics and real-world scenarios. That’s where our All Forever SQL Package comes in.

It’s the best value for money: you pay once and get lifetime access to all current and future courses on LearnSQL.com. You can start with practical tracks like SQL Practice to sharpen your skills, then move on to more advanced topics like Recursive Queries or Window Functions — all at your own pace.

Learning these small but powerful techniques is what turns SQL from a basic tool into a true professional skill.  Why stop at five tricks when you can keep adding to your toolkit for life?