SQL Square Brackets []

 

SQL Square Brackets []


🔹 Introduction

In Salesforce Marketing Cloud Engagement (SFMC), SQL is based on SQL Server (T-SQL).

Square brackets [] are used to escape identifiers such as:

  • Column names
  • Data Extension names

This ensures SQL interprets them exactly as written.



 Why [] Is Important

SQL normally expects:

  • No spaces
  • No special characters
  • No reserved keywords

But in SFMC, real-world data often includes:

  • Email Address
  • Order-Amount
  • Group

👉 Square brackets solve this by telling SQL:

“Use this exact name.”


📌 Use Cases of []


1️⃣ Column Names with Spaces

SELECT [First Name], [Email Address]
FROM Contacts

 

Original DE


Resultened DE 



2️⃣ Reserved Keywords

SELECT [Date], [Group]
FROM Orders



Original DE





Final Result


 


3️⃣ Special Characters

SELECT [Order-Amount], [Discount%]
FROM SalesData

 






4️⃣ Data Extension Names

SELECT *
FROM [Customer Data 2025]




 




5️⃣ Aliasing

SELECT
EmailAddress AS [
Customer Email],
FirstName AS [Customer Name]
FROM Contacts








⚙️ Practical SFMC Setup (Hands-On Practice)


📂 Data Extension 1: Customer Orders

Field NameType
Email AddressEmailAddress
Order DateDate
Order-AmountDecimal
Discount%Decimal
GroupText

📂 Data Extension 2: Contacts

Field NameType
Email AddressEmailAddress
First NameText

👉 These DEs are designed to test:

  • Spaces ✔
  • Special characters ✔
  • Reserved keywords ✔

🚀 Real-Time Query Example

SELECT
[Email Address],
COUNT(*) AS [Total Orders],
MAX([Order Date]) AS [Last Order Date]
FROM [Customer Orders]
WHERE [Order Date] >= DATEADD(day, -30, GETDATE())
GROUP BY [Email Address]





 

📌 What this does:

1. FROM [Customer Orders]

👉 Starting point

  • This tells SQL:

“Take data from the Customer Orders Data Extension”


2. WHERE [Order Date] >= DATEADD(day, -30, GETDATE())

👉 Filtering logic (VERY IMPORTANT)

  • GETDATE() → today’s date
  • DATEADD(day, -30, GETDATE()) → 30 days back

👉 So this means:

“Only take orders from the last 30 days”


3. SELECT [Email Address]

👉 This is your grouping key

  • You are saying:

“I want results per customer (email)”


4. COUNT(*) AS [Total Orders]

👉 Aggregation

  • COUNT(*) = count number of rows (orders)

👉 Meaning:

“How many orders each customer placed”


5. MAX([Order Date]) AS [Last Order Date]

👉 Another aggregation

  • MAX() = latest value

👉 Meaning:

“What is the most recent order date for each customer”


 6. GROUP BY [Email Address]

👉 The most important line

  • This tells SQL:

“Group all rows by each email”


🧠 Golden Rule

👉 Source DE = input data
👉 Target DE = output result


⚠️ Common Mistakes

  • Missing brackets
  • Using reserved keywords without []
  • Incorrect joins
  • Trying to store aggregated data in same DE

✅ Best Practices

  • Avoid spaces in field names (if possible)
  • Always use [] when unsure
  • Keep naming consistent

🎯 Interview Answer

Square brackets [] in SFMC SQL are used to escape identifiers such as column names or Data Extensions that contain spaces, special characters, or reserved keywords, ensuring queries execute correctly.


🔥 Key Takeaway

👉 [] is critical for preventing SQL errors
👉 It ensures reliable and accurate queries

Comments

Popular posts from this blog

A/B Testing in Salesforce Marketing Cloud (Step-by-Step Guide)

📧 Creating & Sending a Test Email in Salesforce Marketing Cloud (SFMC)

Creating a Personalized Email in Salesforce Marketing Cloud