• I agree with most of the formatting comments, but my own style is slightly different. I like the following:

    SELECT a.Column1,

    a.Column2,

    CASE WHEN a.Column1 > 20

    THEN 'High'

    ELSE 'Low'

    END Rating,

    SUM(b.Column3)

    FROM Table1 a

    JOIN Table2 b

    ON a.Key1 = b.Key1

    AND a.Key2 = b.Key2

    WHERE a.Col2 > 10

    AND b.Col3 LIKE 'Hi there%'

    GROUP BY a.Column1,

    a.Column2

    ORDER BY a.Column1,

    a.Column2

    The advantages to me are:

    1. Easy to see start of the statement since all one block with no newlines between.

    2. Capitalized keywords make finding columns easier

    3. If GROUP BY and ORDER BY not included, all columns line up under first

    SELECT column.

    I personally find the preceding comma distracting (though I understand its utility for ease of editing).

    I also searched for a SQL formatter and couldn't find much. I did find a BASIC source for a simplistic SQL formatter, which I extended to support many more keywords. It will take a one-line SQL complex statement similar to what comes out of a GUI tool and format it as above. Email me if interested.

    vince.iacoboni@db.com