100% found this document useful (1 vote)
290 views

Style: SQL For Web Nerds Philip Greenspun

The document provides style guidelines for formatting SQL queries. It recommends: 1) Properly indenting code so the structure is clear and familiar to other programmers. 2) Putting each clause of a query on its own line if it does not fit on one line. 3) Putting the items in a clause on separate lines if they do not fit on the opening line of the clause. 4) Grouping columns for GROUP BY queries with the group identity columns first followed by aggregate columns computing functions for each group.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
290 views

Style: SQL For Web Nerds Philip Greenspun

The document provides style guidelines for formatting SQL queries. It recommends: 1) Properly indenting code so the structure is clear and familiar to other programmers. 2) Putting each clause of a query on its own line if it does not fit on one line. 3) Putting the items in a clause on separate lines if they do not fit on the opening line of the clause. 4) Grouping columns for GROUP BY queries with the group identity columns first followed by aggregate columns computing functions for each group.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/20/12

Sty le

Style
part of SQL for Web Nerds by Philip Greenspun

Here's a familiar simple example from the complex queries chapter:


slc ue_d eet sri, cut* a hwmn fo bor on() s o_ay rm bad weenteit (eet1fo bor_uhrzdmities hr o xss slc rm badatoie_ananr bmweebmue_d= a hr a.sri bor.sri)ad badue_d n psigtm +6 >ssaegopb ue_dodr otn_ie 0 ydt ru y sri re b hwmn ds; y o_ay ec

Doesn't seem so simple, eh? How about if we rewrite it:


slc ue_d cut* a hwmn eet sri, on() s o_ay fo bor rm bad weenteit (eet1fo hr o xss slc rm bor_uhrzdmitiesbm badatoie_ananr a weebmue_d=bor.sri) hr a.sri badue_d adpsigtm +6 >ssae n otn_ie 0 ydt gopb ue_d ru y sri odrb hwmn ds; re y o_ay ec

If your code isn't properly indented then you will never be able to debug it. How can we justify using the word "properly"? After all, the SQL parser doesn't take extra spaces or newlines into account. Software is indented properly when the structure of the software is revealed and when the indentation style is familiar to a community of programmers.

Rules for All Queries


philip.greenspun.com/sql/sty le.html

10/20/12

Sty le

If it fits on one line, it is okay to leave on one line:


slc ealfo uesweeue_d=3; eet mi rm sr hr sri 4

If it doesn't fit nicely on one line, give each clause a separate line:
slc * eet fo nw rm es weessae>eprto_ae hr ydt xiaindt adapoe_ ='' n prvdp t odrb rlaedt ds,ceto_aeds re y ees_ae ec raindt ec

If the stuff for a particular clause won't fit on one line, put a newline immediately after the keyword that opens the clause. Then indent the items underneath. Here's an example from the ArsDigita Community System's static .html page administration section. We're querying the s a i _ a e table, which holds a copy of any .html files in the Unix file system: ttcpgs
slc eet t_hrcut*,9999999999)a npgs oca(on()'9G9G9G9G9' s _ae, t_hrsmdm_o.elnt(aebd),9999999999)a nbts oca(u(bslbgteghpg_oy)'9G9G9G9G9' s _ye fo sai_ae; rm ttcpgs

In this query, there are two items in the select list, a count of all the rows and a sum of the bytes in the p g _ o ycolumn (of type CLOB, aebd hence the requirement to use d m _ o . e l n t rather than simply l n t ). We want Oracle to format these numbers with separation bslbgtegh egh characters between every three digits. For this, we have to use the t _ h rfunction and a mask of ' 9 G 9 G 9 G 9 G 9 '(the "G" tells oca 9999999999 Oracle to use the appropriate character depending on the country where it is installed, e.g., comma in the U.S. and period in Europe). Then we have to give the results correlation names so that they will be easy to use as Tcl variables. By the time we're done with all of this, it would be confusing to put both items on the same line. Here's another example, this time from the top-level comment administation page for the ArsDigita Community System. We're going to get back a single row with a count of each type of user-submitted comment:
slc eet cut* a nttl on() s _oa, smdcd(omn_ye'lentv_esetv'10)a natraieprpcie, u(eoecmettp,atraieprpcie,,) s _lentv_esetvs smdcd(omn_ye'aig,,) a nrtns u(eoecmettp,rtn'10) s _aig, smdcd(omn_ye'nnwrdqeto'10)a nuasee_usin, u(eoecmettp,uasee_usin,,) s _nnwrdqetos smdcd(omn_ye'rvt_esg_opg_uhr'10)a npiaemsae u(eoecmettp,piaemsaet_aeatos,,) s _rvt_esgs
philip.greenspun.com/sql/sty le.html 2/4

fo cmet rm omns

Notice the use of s m d c d to count the number of each type of comment. This gives us similar information to what we'd get from a u(eoe GROUP BY, but we get a sum total as well as category totals. Also, the numbers come out with the column names of our choice. Of course, this kind of query only works when you know in advance the possible values of c m e t t p . omn_ye The Oracle docs on DECODE for an explanation of the number formatting wizardry, see Oracle8 Server SQL Reference section on format conversion

Rules for GROUP BY queries


When you're doing a GROUP BY, put the columns that determine the group identity first in the select list. Put the aggregate columns that compute a function for that group afterwards:
slc lnsue_d frtnms ls_ae cutlnspg_d a nlns eet ik.sri, is_ae, atnm, on(ik.aei) s _ik fo lns ues rm ik, sr weelnsue_d=uesue_d hr ik.sri sr.sri gopb lnsue_d frtnms ls_ae ru y ik.sri, is_ae, atnm odrb nlnsds re y _ik ec

Next: procedural [email protected]

Reader's Comments
The where clause is in two lines in the example above though the text suggests one line: "If it doesn't fit nicely on one line, give each clause a separate line: select * from news where sysdate > expiration_date and approved_p = 't' order by release_date desc, creation_date desc" In this case, one line would be the better, of course.

10/20/12

Sty le

-- Peter Tury, June 12, 2002 Add a comment | Add a link

philip.greenspun.com/sql/sty le.html

4/4

You might also like