📜 ⬆️ ⬇️

Formatting Long SQL Queries

Entered recently in a local offline holivor on the topic of formatting long SQL queries.

Actually, the whole holivor comes down to the fact that it is more convenient to read - INNER JOIN BEFORE the table, or AFTER it, and also - AND - before or after the condition is announced.

Two options and a question for habrovchanam under the cut:
')

Option 1:


SELECT
t.field,
t.field1,
...
FROM
table t INNER JOIN
table1 t1 ON t1.id = t.id INNER JOIN
table2 t2 ON t2.id=t1.id
WHERE
t.value = 'foo' AND
t1.value = 'bar'
GROUP BY t.field
ORDER BY t.field


Option 2:


SELECT
t.field,
t.field1,
...
FROM table t
INNER JOIN table1 t1
ON t1.id=t.id
INNER JOIN table2 t2
ON t2.id=t1.id
WHERE t.value='foo'
AND t1.value='bar'
GROUP BY t.field
ORDER BY t.field


We can not find standards for formatting requests - for programming - full, by request - no. And still argue.

What do you think, what option do you use? First or second? And maybe the third?

Source: https://habr.com/ru/post/46068/


All Articles