1

Is there a name for this kind of SQL programming style?

--Courtesy of Markus Winand of modern-sql.com/ 
SELECT *
  FROM (SELECT cities.*
             , ROW_NUMBER() OVER(PARTITION BY country ORDER BY population DESC) rn
             , COUNT(*)     OVER(PARTITION BY country) ct_cities
          FROM cities
       ) t
 WHERE t.rn = 1
   AND t.ct_cities > 1

Source: db<>fiddle related to this topic: Oracle Idea: FIRST() and LAST() aggregate functions


It's as if there is a line between the SQL keywords (right-justified) and the criteria (left-justified).

enter image description here

Whereas in other SQL styles, the SQL keywords are left-justified.


I ask because that style seems easy to read and I want to find out more about it.

Edit; a related post: SQL Developer - Keep analytic function on single line when auto-formatting

1
  • That's how I've always formatted my SQL. I think I may have picked it up from Feuerstein like 25 years ago. I've never heard of anyone assigning a "name" to the style.
    – Paul W
    Commented Mar 17, 2023 at 3:10

2 Answers 2

2

GUI tools have/might have formatting options which allow you to set your own preferences. For example, in TOAD, it looks like this:

enter image description here

In Oracle SQL Developer:

enter image description here


Million years ago, when we were on Oracle 7.1 running on Digital Alpha server under OpenVMS operating system, there was only text editor (think of it as MS Windows Notepad) so you had to format code on your own - if you wanted. For example:

enter image description here

On the other hand, another developer did it this way (no formatting at all; you should see his complex SQL scripts):

enter image description here


Therefore, I wouldn't say that it is about formatting name, but whether code is - or is NOT - formatted.

2

A possible name for the programming style you are looking for would be vertical aligned.

However, there is no actual standard as to how the vertical aligned style would be implemented into whatever coding tool you are using, but is instead left up to the developers of the editors you are using.

Seeing as programming has evolved over time you will find that styles may have equally evolved and been adapted according to the requirements and/or complexity of the underlying programming languages.

Preferences of the companies implementing a certain style into their editors settings will certainly have an impact on how the available configuration options are applied to your code.

e.g. having a vertical alignment in your code may result in tabulators or spaces being added to your code and the amount of spaces may be configurable or not.

Different languages will have different coding conventions that are recommended, which may or may not include a programming style. Then again these programming styles are not standards, but essentially what a majority of developers find best.

I wouldn't put to much energy into finding the name of a specific programming style, but instead focus on what suits you (and possibly your team) best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.