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
2️⃣ Reserved Keywords
3️⃣ Special Characters
4️⃣ Data Extension Names
5️⃣ Aliasing
⚙️ Practical SFMC Setup (Hands-On Practice)
📂 Data Extension 1: Customer Orders
| Field Name | Type |
|---|---|
| Email Address | EmailAddress |
| Order Date | Date |
| Order-Amount | Decimal |
| Discount% | Decimal |
| Group | Text |
📂 Data Extension 2: Contacts
| Field Name | Type |
|---|---|
| Email Address | EmailAddress |
| First Name | Text |
👉 These DEs are designed to test:
- Spaces ✔
- Special characters ✔
- Reserved keywords ✔
🚀 Real-Time Query Example
📌 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
Post a Comment