Comments (1)
I would tend to lay it out so that the GROUP
keyword is west of the river and BY
is east of the river. Having just reviewed similar queries in Celko's books again this is the style he uses as well. In fact I think I might add some information to the guide to document GROUP BY
.
So using some of your example code we could lay it out like so:
SELECT one,
two,
three,
five
FROM some_table
WHERE field = 'default'
GROUP BY 1, 2
I like this because GROUP
is really enough to spot that particular part of the statement when scanning to the west of the river as you read a query.
Now onto your other issue with UNION
or UNION ALL
; I tend to treat these as exceptions (like jokers in a pack of playing cards) as they're outside the query in question generally. I put them on a new line with clear whitespace surrounding them so it is quick and easy to see where the queries end and begin. I've used the shortened version of your code from above to illustrate this below:
(SELECT one,
two,
three,
five
FROM some_table
WHERE field = 'default'
GROUP BY 1, 2)
UNION ALL
(SELECT one,
two,
three,
five
FROM some_table
WHERE field = 'default'
GROUP BY 1, 2)
Celko does put them on their own line and he ignores the river for a UNION ALL
, but he dispenses with the extra surrounding whitespace lines I have included above. So for the above query he might write:
(SELECT one,
two,
three,
five
FROM some_table
WHERE field = 'default'
GROUP BY 1, 2)
UNION ALL
(SELECT one,
two,
three,
five
FROM some_table
WHERE field = 'default'
GROUP BY 1, 2)
I find the UNION ALL
a little jarring in this final example as it ignores the river without any leading or following whitespace to help negate the effect of non-conformity.
A further option would be to take the lead of the GROUP BY
we discussed earlier:
(SELECT one,
two,
three,
five
FROM some_table
WHERE field = 'default'
GROUP BY 1, 2)
UNION ALL
(SELECT one,
two,
three,
five
FROM some_table
WHERE field = 'default'
GROUP BY 1, 2)
This is probably the one that makes the most sense given that the keyword is really UNION
and ALL
is but a modifier to it.
I am thinking that adding a few, more complicated, SQL examples to the guide might help to cover off some of these more advanced usages and their optimal layout.
from sqlstyle.guide.
Related Issues (20)
- clicking link doesn't update fragment in URL HOT 3
- add link for OOP design vs. SQL HOT 3
- Grammer error in German translation HOT 1
- Audit columns HOT 1
- This guide is fantastic HOT 1
- Italian translation HOT 6
- CHECK pens_in_drawer violates "preferred formalisms" re. BETWEEN HOT 1
- Order of foreign key? HOT 1
- Backticks (backquotes?) in style. HOT 2
- Naming convention for boolean columns HOT 3
- Some additional uniform-suffixes for consideration HOT 1
- JOIN in Aliasing or correlations example not indented right HOT 1
- Possible indent typo on sqlstyle.guide website HOT 1
- ISO Date/Time Format HOT 2
- How are you aligning the first word on each line? HOT 2
- Ukrainian translation suggestion
- [just suggestion] from gitlab style (indentation 2 spaces) HOT 1
- Reconsider JOIN Style
- Mine
- https://sqlstyle.guide gives cloudflare's ssl exception HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlstyle.guide.