Anyone who has ever debugged a 200-line SELECT without indentation knows: SQL formatting is more than a matter of taste. Readable code isn’t just easier to understand — with the right editor tools, it’s also much faster to refactor.
In this article:
- Why SQL has its own style problem
- Box Selection: editing multiple lines at once
- Search & Replace as a formatting tool
Prerequisite: SSMS serves as the example editor; the principles apply to any editor with box selection (Azure Data Studio, VS Code, DataGrip, DBeaver, …).
Why SQL Needs Its Own Style
Visual Studio and VS Code format C#, Python, and JavaScript largely automatically — for SQL, that’s only partially true. SQL Server Management Studio (SSMS) has barely any auto-formatting, and established style conventions from other languages don’t transfer well. The reason lies in the paradigm: a single SQL statement can span hundreds of lines, while software code typically consists of many short instructions. Readability in SQL therefore has to be created consciously — manually or with editor tools that make exactly that easier.
Formatting Is Learning
Anyone who manually formats a 100-line SELECT can’t avoid actually reading the statement. Indenting JOIN clauses forces you to build a mental model of the relationships between tables. Aligning aliases means tracking where each column comes from. Setting parentheses means working through the logic precisely. The result isn’t just readable code — it’s a better model of the data in the writer’s head.
Anyone who clicks a statement together in a designer or pieces one together from a heap of copy-pasted snippets ends up with a working statement and no understanding. In a world where Copilot and Cursor write SQL ready-made, this comprehension gap becomes a risk: technically correct SQL that nevertheless fails to answer the actual business question, because the writer never really looked at the data. SQL is closer to data modeling than to classical programming — and modeling requires understanding, not typing. Manual formatting is a discipline that prevents exactly that — readability is only the visible by-product.
Aesthetics
When a SQL statement is structured, its logic becomes visible — that’s the aesthetic this article means. What we call beauty, regularity, and harmony in functional code isn’t anything esoteric: these are the qualities structured code has, and a heap of pasted snippets doesn’t.
The following SQL statement is admittedly not particularly meaningful, but it is valid and runs without problems against the AdventureWorksDW2017 database:
SELECT[DimDate].ProductKey,C.EnglishProductName,DueDateKey,FullDateAlternateKey,DimDate.CustomerKey,B.[LastName]FROM[dbo].FactInternetSales[DimDate],dbo.[DimDate]D,dbo.[DimCustomer]B,dbo.DimProduct[C]WHERE[DueDateKey]=[DateKey]AND[DimDate].[ProductKey]=[C].[ProductKey]AND[DimDate].CustomerKey=B.CustomerKey
Even though aesthetics is subjective, the beauty, regularities, harmony — and certainly the meaningfulness — of this statement may all be questioned.
The same statement, somewhat reformatted and rewritten, looks more aesthetic than the previous one:
SELECT
T01.[ProductKey]
,T04.[EnglishProductName]
,T01.[DueDateKey]
,T02.[FullDateAlternateKey]
,T01.[CustomerKey]
,T03.[LastName]
FROM
[dbo].[FactInternetSales] T01
LEFT JOIN [dbo].[DimDate] T02
ON
T01.[DueDateKey] = T02.[DateKey]
LEFT JOIN [dbo].[DimCustomer] T03
ON
T01.[CustomerKey] = T03.[CustomerKey]
LEFT JOIN [dbo].[DimProduct] T04
ON
T01.[ProductKey] = T04.[ProductKey];
In any case, this version is more readable and easier to understand. Individual components of the statement can be identified by their position alone. The term aesthetics may seem far-fetched here in a technical sense, but many requirements for structured code can be summarised under it.
Functionality
The headline of this article emphasises the functionality of aesthetics. It focuses less on the function of SQL commands themselves and more on powerful editing features in SSMS — and other editors — that let you structure and format SQL statements quickly. The two power features here are Box Selection and Search & Replace, combined with a willingness to use the keyboard rather than the mouse.
Box Selection
Box Selection (called Column Selection in SSMS and Visual Studio) is such a powerful feature in many editors that I find it hard to understand how few developers know or use it. It lets you select and edit text block-wise — instead of the line-based default.
For line-by-line text selection, you press the Shift key. The shortcut Shift+Right Arrow selects text character by character starting at the cursor; the selection can span multiple lines:

For block-wise selection, you combine the Shift key with the ALT key. In the example below, ALT+Shift+Down Arrow extends the cursor over multiple lines starting at the first occurrence of alias T01, then ALT+Shift+Right Arrow selects a block across all aliases.

Box selection thus lets you extend the cursor across multiple lines and select a rectangular block anywhere in the editor. Anything you type is applied to all lines that the cursor spans. The text below was not duplicated by Copy & Paste — it was typed once after extending the cursor over five lines (entering the text in all five at once):

Box selection also works with the mouse. Starting from the current cursor position, you can drag a rectangle by holding ALT+Shift together with the left mouse button.


Damit die Blockauswahl wirklich Zeit spart, muss das SQL-Statement strukturiert gegliedert For box selection to really save time, the SQL statement must be cleanly structured and precisely indented.
Box selection takes some getting used to. But once you’ve come to appreciate it, you won’t want to do without it.
Note
- Box selection only works in editor regions that contain line breaks.
- It is most efficient when tabs are inserted as spaces. See also Editor Options in SSMS.
- Block-wise selection is supported by many editors and even Microsoft Word: SQL Server Management Studio, Microsoft Visual Studio, Notepad++, etc.
- The implementation of this power feature can vary considerably between editors.
- In SQL Server Management Studio and Microsoft Visual Studio the feature is implemented identically. Notepad++ also provides box selection via
ALT+Shift, but its implementation differs from the Microsoft products and (in my view) is not as intuitive or comfortable.
Search & Replace
The Search & Replace feature is probably familiar to everyone and available in SSMS via Ctrl+H. Originally intended for replacing individual text fragments, it turns out to be another power feature for SQL formatting.
For example: SQL Server supports either double quotes (") or square brackets ([ ]) as delimiters for object names. Delimiters are not strictly required, but they improve readability.
The two SELECT statements below differ only in whether their field names are delimited.
Without delimiters:
SELECT
T01.object_id
,T01.name
,T01.column_id
,T01.system_type_id
,T01.user_type_id
,T01.max_length
,T01.precision
FROM
sys.columns T01
With delimiters (square brackets):
SELECT
T01.[object_id]
,T01.[name]
,T01.[column_id]
,T01.[system_type_id]
,T01.[user_type_id]
,T01.[max_length]
,T01.[precision]
FROM
[sys].[columns] T01
Adding delimiters to large statements may seem time-consuming. Inserting the closing brackets, in particular, looks tricky because their position differs from line to line.
In practice, the task takes only a few moments.
The brackets can be inserted before and after each field name with box selection, as shown below:

Then, with Search & Replace, the closing brackets can be moved left until there are no spaces between the last character of each field name and the bracket. Replace <space>] with ]<space> repeatedly until no occurrence of <space>] remains.

Summary
The more structured and formatted SQL code is, the more readable and maintainable it becomes. SSMS provides two powerful features — Box Selection and Search & Replace — that let you structure and format SQL code quickly and efficiently. The result may or may not be perceived as aesthetic. Aesthetics here is rather an incidental attribute. The term was used in this article as a provocative buzzword.
When SSMS Hits Its Limits: DataGrip
For occasional SQL editing with box selection and Search & Replace, SSMS is a solid editor. But anyone who regularly refactors larger statements will find a much more powerful tool in DataGrip by JetBrains: real multi-cursor at arbitrary positions, built-in auto-format for T-SQL and Postgres, intelligent column and alias refactorings. In direct comparison, DataGrip is also significantly more capable than Visual Studio Code with SQL extensions.
Since October 2025, DataGrip is free for non-commercial use (JetBrains announcement) — one less barrier to giving it a try.
FAQ
Why should I format SQL manually at all — auto-formatters do that, don’t they?
Auto-formatters (sqlfluff, pgFormatter, format-on-save buttons in editors) produce readable layout, but they don’t replace the act of formatting. Manually indenting, aligning aliases, and placing parentheses forces you to read the statement in full and build a mental model of the data and its relationships along the way. Auto-formatters are useful as a final polish, but anyone who never engages with the structure of a statement while writing it learns nothing about the underlying data.
How do I activate box selection in SSMS?
Hold down ALT+Shift and either drag with the arrow keys (Down Arrow/Right Arrow) or use the left mouse button to draw a rectangle. Anything you type then appears in all marked lines simultaneously.
Does box selection also work in other editors?
Yes — in Visual Studio (identical to SSMS), Azure Data Studio, VS Code, DataGrip, DBeaver, and even Microsoft Word. The ALT+Shift shortcut with arrow keys or mouse is the same everywhere. Notepad++ supports it too, though with a slightly different implementation.
What is the difference between box selection and multi-cursor?
Both let you edit multiple places at once. Box selection drags a rectangle — all selected columns across rows below one another. The multi-cursor (Ctrl+Click in VS Code, DataGrip, and Azure Data Studio; limited support in SSMS) places multiple independent cursors at arbitrary positions. For formatting-style refactoring of SQL columns, box selection is usually more intuitive; for irregular multi-edits, multi-cursor is more flexible.
How do I replace text across many SQL lines at once?
Ctrl+H opens Search & Replace. For more complex patterns — for example, replacing all CONVERT( calls with TRY_CONVERT( — enable the regex option in the search dialog. For repetitive, identical edits within the same block of lines, box selection is usually faster.