The Functional Aesthetics of SQL — Why Structured Code Is Faster to Edit

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, …).

Contents

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) offers only limited automatic formatting compared to specialized SQL editors, and SQL has no single, universally binding formatting style the way other languages do: plenty of conventions and style guides exist — which one applies depends on dialect, formatter, and team. On top of that comes 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. That is a thesis drawn from experience rather than a law of nature, but one that keeps proving itself in practice. 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 beautyregularity, 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:

  1: 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 beautyregularity, and harmony of this statement can be questioned — and certainly its meaningfulness, too.

The same statement, somewhat reformatted and rewritten, looks more aesthetic than the previous one:

  1: SELECT
  2:     T01.[ProductKey]
  3:    ,T04.[EnglishProductName]
  4:    ,T01.[DueDateKey]
  5:    ,T02.[FullDateAlternateKey]
  6:    ,T01.[CustomerKey]
  7:    ,T03.[LastName]
  8: FROM
  9:    [dbo].[FactInternetSales] T01
 10:    LEFT JOIN [dbo].[DimDate] T02
 11:    ON
 12:      T01.[DueDateKey] = T02.[DateKey]
 13:    LEFT JOIN [dbo].[DimCustomer] T03
 14:    ON
 15:      T01.[CustomerKey] = T03.[CustomerKey]
 16:    LEFT JOIN [dbo].[DimProduct] T04
 17:    ON
 18:      T01.[ProductKey] = T04.[ProductKey];

The second version is not just indented but also rewritten: the implicit comma joins of the first statement are replaced by explicit LEFT JOIN clauses, and the aliases follow a consistent positional scheme (T01T04). The result is more readable and easier to understand: individual components 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. If you prefer the keyboard over the mouse, you can produce a well-structured, cleanly formatted SQL statement in very little time.

Box Selection

Box Selection (called Column Selection in SSMS and Visual Studio) is available in many editors and such a powerful feature that it is always surprising how rarely it gets used in day-to-day development. 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:

Character-wise cursor selection in SSMS: Shift+Right Arrow extends the selection one character at a time across 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 the alias T01. Then ALT+Shift+Right Arrow selects a block across all aliases.

Block-wise selection in SSMS: ALT+Shift+Down Arrow and ALT+Shift+Right Arrow mark all T01 aliases vertically in a rectangle.

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 exactly once after extending the cursor over five lines — the entry appears in all five lines at once:

Box selection in SSMS: a single typed entry appears simultaneously across five lines, without copy and paste.

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.

Mouse box selection in SSMS: while ALT+Shift is held, the left mouse button drags a selection rectangle.

Result of mouse box selection: one line within the rectangle is highlighted.

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.
  • Box selection works on character columns: real tab characters break the rectangular selection behavior, so the editor should insert tabs 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.
  • SQL Server Management Studio is built on the Visual Studio shell, so the feature behaves the same in both products. Notepad++ also provides box selection via ALT+Shift, but its implementation differs from the Microsoft products and is less intuitive and 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 square brackets ([ ]) as delimiters for object names, as well as double quotes (") — the latter only with the QUOTED_IDENTIFIER option enabled, which is the default in SSMS. Delimiters are only mandatory for identifiers that contain, for instance, reserved words, spaces, or special characters. Many teams still use them consistently so that every identifier is unambiguously marked as such. Whether that improves readability or reads as visual noise is a matter of convention.

The two SELECT statements below differ only in whether their field names are delimited.

Without delimiters:

  1: SELECT
  2:     T01.object_id
  3:    ,T01.name
  4:    ,T01.column_id
  5:    ,T01.system_type_id
  6:    ,T01.user_type_id
  7:    ,T01.max_length
  8:    ,T01.precision
  9: FROM
 10:     sys.columns T01

With delimiters (square brackets):

  1: SELECT
  2:     T01.[object_id]
  3:    ,T01.[name]
  4:    ,T01.[column_id]
  5:    ,T01.[system_type_id]
  6:    ,T01.[user_type_id]
  7:    ,T01.[max_length]
  8:    ,T01.[precision]
  9: FROM
 10:     [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:

Box selection in action: opening and closing square brackets are inserted before and after all field names simultaneously.

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.

Result of Search & Replace: the closing square brackets are flush with the field names; the code is finally formatted.

Summary

The more consistently SQL code is structured and formatted, 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. For extensive SQL refactoring and database-specific navigation, DataGrip offers more built-in functionality than a typical VS Code setup with SQL extensions.

Since October 2025, DataGrip is free for non-commercial use (JetBrains announcement). That removes one 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 little 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 principle is the same everywhere, but shortcut and implementation details vary by editor, operating system, and configuration. In the Microsoft tools it is ALT+Shift with arrow keys or mouse, while Notepad++ uses the same combination with slightly different behavior.

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.