SQL Statement indentation good practice [closed]

2019-01-21 05:06发布

What is the accepted practice for indenting SQL statements? For example, consider the following SQL statement:

SELECT column1, column2
FROM table1
WHERE column3 IN
(
SELECT TOP(1) column4
FROM table2
INNER JOIN table3
ON table2.column1 = table3.column1
)

How should this be indented? Many thanks.

25条回答
萌系小妹纸
2楼-- · 2019-01-21 05:52

This is my personal method. Depending on the length of the join condition I sometimes indent it on the line below.

SELECT
  column1,
  column2
FROM
  table1
WHERE
  column3 IN ( 
    SELECT TOP(1)
      column4
    FROM
      table2
      INNER JOIN table3 ON table2.column1 = table3.column1
  )


SELECT
  column1,
  column2
FROM
  table1
WHERE
  column3 IN ( 
    SELECT TOP(1)
      column4
    FROM
      table2
      INNER JOIN table3
        ON table2.column1 = table3.column1 -- for long ones
  )
查看更多
再贱就再见
3楼-- · 2019-01-21 05:55

What I usually do is,

print("SELECT column1, column2
       FROM table1
       WHERE column3 IN (SELECT TOP(1) column4
                         FROM table2 INNER JOIN 
                              table3 ON table2.column1 = table3.column1)");
查看更多
趁早两清
4楼-- · 2019-01-21 05:56

I don't know if there's a standard but I like to do it this way;

SELECT column1, column2
  FROM table1
WHERE column3 IN
(
    SELECT TOP(1) column4
      FROM table2
    INNER JOIN table3
      ON table2.column1 = table3.column1
)

because I can read and analyze the SQL better.

查看更多
叛逆
5楼-- · 2019-01-21 05:56
SELECT
    Column1,
    Column2
FROM
    Table1
WHERE
    Column3 IN
    (
        SELECT TOP (1)
            Column4
        FROM 
            Table2
        INNER JOIN 
            Table3
        ON
            Table2.Column1 = Table3.Column1
    )
查看更多
贼婆χ
6楼-- · 2019-01-21 05:58
SELECT column1, column2
FROM table
WHERE column3 IN (
    SELECT TOP(1) column4
    FROM table2
    INNER JOIN table3 ON table2.column1 = table3.column1
)

This is pretty short and easy to read. I'd make adjustments if there were more columns selected or more join conditions.

查看更多
放荡不羁爱自由
7楼-- · 2019-01-21 05:58

Yeah, this is pretty subjective...But here's my 2 cents:

SELECT
   Column1,
   Column2
FROM Table1
WHERE 
   Column3 IN (
      SELECT Column4
      FROM Table2
      JOIN Table3 ON
         Table2.Column1 = Table3.Column1
   )

But, really, I'd probably rewrite it without the IN:

SELECT
   Column1,
   Column2
FROM Table1
JOIN Table2 ON
   Table1.Column3 = Table2.Column4
JOIN Table3 ON
   Table2.Column1 = Table3.Column1

Basically, my rules are:

  • Capitalize Keywords
  • Columns go on individual lines, but SELECT modifiers (SELECT TOP 100, SELECT DISTINCT, etc.) or single columns (SELECT 1, SELECT Id, SELECT *, etc.) go on same line
  • Join conditions indented underneath JOIN clause
  • Use JOIN for INNER JOIN (since it's the common one), and fully specify others (LEFT OUTER JOIN, FULL OUTER JOIN, etc.)
  • Open parens on same line, close paren on separate line. If you have an alias, the alias goes with close paren.
查看更多
登录 后发表回答