Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2013
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

View Poll Results: All Caps for SQL Syntax?
Yes 19 48.72%
No 5 12.82%
A mix of both 15 38.46%
Voters: 39. You may not vote on this poll

Reply
 
Thread Tools
Old 12-01-2012, 10:08 PM   #1
N4g4rok
Senior Member
 
N4g4rok's Avatar
 
Join Date: Sep 2011
Location: Northwest Arkansas, USA
Posts: 284
Default All Caps for SQL Syntax?

I'm curious, do you prefer tot type your SQL in all caps? Lately i've been correcting myself and doing so.
__________________
Primary: Hades
> AMD Phenom II X4 970 @ 3.9 Ghz + Antec Kuhler 620
> ASUS M4A79XTD EVO
> XFX Radeon R7950 Black Edition
> 128GB Samsung 830 SSD + 2TB Hitachi Deskstar HDD
> 8GB G.SKILL Ripjaws DDR3 RAM @ 1600 Mhz
> NZXT Hades Case

Home Server: Charon
> AMD Sempron 145 @ 2.8 Ghz + Stock cooler
> GigaByte GA-M68MT-S2
> 2TB Seagate Barracuda Green HDD + 2TB Samsung Spinpoint HDD
> 4GB Crucial DDR3 RAM @ 1333
> NZXT Hush Case
N4g4rok is offline   Reply With Quote
Old 12-02-2012, 10:42 AM   #2
KLin
Lifer
 
KLin's Avatar
 
Join Date: Feb 2000
Location: Phreaznaux
Posts: 28,610
Default

I usually capitalize t-sql keywords (SELECT, FROM, WHERE, GROUP BY, HAVING) etc. etc. and use pascal casing for field names and table names.
__________________
'L_'
KLin is online now   Reply With Quote
Old 12-02-2012, 10:44 AM   #3
Nothinman
Elite Member
 
Nothinman's Avatar
 
Join Date: Sep 2001
Posts: 30,672
Default

I'm with KLin, keywords get capitalized while the tables, columns, etc get case according to how they were created/inserted.
__________________
http://www.debian.org
Nothinman is offline   Reply With Quote
Old 12-02-2012, 11:33 AM   #4
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 11,742
Default

Quote:
Originally Posted by KLin View Post
I usually capitalize t-sql keywords (SELECT, FROM, WHERE, GROUP BY, HAVING) etc. etc. and use pascal casing for field names and table names.
This.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is offline   Reply With Quote
Old 12-02-2012, 02:30 PM   #5
Leros
Lifer
 
Leros's Avatar
 
Join Date: Jul 2004
Location: Austin, TX
Posts: 21,369
Default

Agree with the above. Not that it really matters, but it is nice to be consistent throughout the codebase.
Leros is offline   Reply With Quote
Old 12-02-2012, 06:02 PM   #6
Dratickon
Junior Member
 
Join Date: May 2012
Posts: 21
Default

Quote:
Originally Posted by KLin View Post
I usually capitalize t-sql keywords (SELECT, FROM, WHERE, GROUP BY, HAVING) etc. etc. and use pascal casing for field names and table names.
This is also what I tend to do. I work with a lot of legacy code/queries that were written without any standardized coding style, so I've learned that the little things like this make a difference and add up over time.
Dratickon is offline   Reply With Quote
Old 12-03-2012, 11:10 PM   #7
clamum
Lifer
 
clamum's Avatar
 
Join Date: Feb 2003
Posts: 21,481
Default

Quote:
Originally Posted by KLin View Post
I usually capitalize t-sql keywords (SELECT, FROM, WHERE, GROUP BY, HAVING) etc. etc. and use pascal casing for field names and table names.
++
__________________
\ Battlefield 4 Stats /\ Heat /
clamum is online now   Reply With Quote
Old 12-04-2012, 04:32 PM   #8
KentState
Diamond Member
 
KentState's Avatar
 
Join Date: Oct 2001
Location: Atlanta, GA
Posts: 5,519
Default

Capitalization is fine, but the biggest pet peeve is indents for me. A block of SQL code that I have to reformat to read is not pleasant. However, reserved T-SQL syntax is always in caps for me and objects match how the meta data.
KentState is offline   Reply With Quote
Old 12-04-2012, 06:32 PM   #9
N4g4rok
Senior Member
 
N4g4rok's Avatar
 
Join Date: Sep 2011
Location: Northwest Arkansas, USA
Posts: 284
Default

Quote:
Originally Posted by KentState View Post
the biggest pet peeve is indents for me.
What kind of indentation would you like to see in SQL code? I usually try to fit everything into C/C++ like blocks if i can. For example:

Code:
CREATE TABLE Persons
 (
   P_Id int primary key,
   LastName varchar(255),
   FirstName varchar(255),
   Address varchar(255),
   City varchar(255)
 );
__________________
Primary: Hades
> AMD Phenom II X4 970 @ 3.9 Ghz + Antec Kuhler 620
> ASUS M4A79XTD EVO
> XFX Radeon R7950 Black Edition
> 128GB Samsung 830 SSD + 2TB Hitachi Deskstar HDD
> 8GB G.SKILL Ripjaws DDR3 RAM @ 1600 Mhz
> NZXT Hades Case

Home Server: Charon
> AMD Sempron 145 @ 2.8 Ghz + Stock cooler
> GigaByte GA-M68MT-S2
> 2TB Seagate Barracuda Green HDD + 2TB Samsung Spinpoint HDD
> 4GB Crucial DDR3 RAM @ 1333
> NZXT Hush Case
N4g4rok is offline   Reply With Quote
Old 12-04-2012, 07:26 PM   #10
KentState
Diamond Member
 
KentState's Avatar
 
Join Date: Oct 2001
Location: Atlanta, GA
Posts: 5,519
Default

I hate seeing something like:
Code:
WITH cte (column1, column2)
AS (SELECT COLUMN1, COLUMN2 FROM TABLE)
SELECT
COLUMN1, COLUMN2,
COLUMN3, COLUMN4....
FROM A INNER JOIN B ON A. = B.
INNER JOIN C ON B. = C. INNER JOIN D ON 
A. = D. INNER JOIN cte ON ....
WHERE A.VALUE > B.VALUE AND (D.VALUE = '' OR C.VALUE = '')
AND C.VALUE = ''
GROUP BY COLUMN1, COLUMN2,
COLUMN3, COLUMN4....
I much prefer:
Code:
WITH cte (column1, column2)
AS
(
  SELECT COLUMN1, COLUMN2
  FROM TABLE
)
SELECT
   COLUMN1, 
   COLUMN2,
   COLUMN3, 
   COLUMN4....
FROM A 
  INNER JOIN B ON 
     A. = B.
  INNER JOIN C ON 
     B. = C. 
  INNER JOIN D ON 
     A. = D.
  INNER JOIN cte ON.....
WHERE A.VALUE > B.VALUE 
   AND (D.VALUE = '' OR C.VALUE = '')
   AND C.VALUE = ''
GROUP BY 
   COLUMN1, 
   COLUMN2,
   COLUMN3, 
   COLUMN4....
Just break it apart enough so I can read it. I've seen various indentation methods and as long as it's used consistently, I'm happy. There are some SQL beautifier tools that do a great job right in Management Studio.
KentState is offline   Reply With Quote
Old 12-04-2012, 08:07 PM   #11
AyashiKaibutsu
Diamond Member
 
AyashiKaibutsu's Avatar
 
Join Date: Jan 2004
Location: Maryland
Posts: 8,770
Default

Last office capped sql key words. This office we cap variables... At the end of the day, I don't really care I just write it to fit in with the code that's there, but I do like key words a little better.
__________________
The large print giveths and the small print taketh away.
AyashiKaibutsu is offline   Reply With Quote
Old 12-04-2012, 10:38 PM   #12
ringtail
Senior Member
 
ringtail's Avatar
 
Join Date: Mar 2012
Location: USA left coast
Posts: 698
Default

there's a formatting tool here that some may find useful
Instant SQL Formatter
ringtail is offline   Reply With Quote
Old 12-10-2012, 11:57 AM   #13
Snapshot1
Member
 
Join Date: Dec 2011
Location: Burlington, Ontario, Canada
Posts: 42
Default

If maintaining existing code I will try to conform to whatever convention is established.

In new code I tend to avoid capitalization but I am generally using SQL Server Management Studio which color-codes keywords, comments, etc. so they are easy to distinguish WITHOUT CAPITALIZATION which many of us have come to associate with shouting.

But careful / consistent indentation and suitable comments are both mandatory, and I apply these to both new and existing work without exception.

Snapshot1
__________________
i7-2600 ASUS P8Z68-PRO/GEN3 Corsair Vengeance LP 16GB DDR3 1600 Radeon HD 6870 1GB Corsair Force 3 120GB SSD WD Caviar Green 2TB HDD Corsair Obsidian 650D Seasonic X-760 Corsair 650D Enermax ETD-T60-TB Windows 7 Ultimate 64 bit SP1 Dell U2770
Snapshot1 is offline   Reply With Quote
Old 12-10-2012, 12:12 PM   #14
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 11,742
Default

I think the point is that, in case-insensitive syntaxes, if you don't adhere to a convention you end up with a mix of styles. When I started learning SQL using dblib the convention that MS inherited from Sybase was uppercase for reserved words. So I've stuck with that over the years.
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is offline   Reply With Quote
Old 12-12-2012, 09:26 PM   #15
MajorMullet
Senior Member
 
MajorMullet's Avatar
 
Join Date: Jul 2004
Posts: 783
Default

Quote:
Originally Posted by KLin View Post
I usually capitalize t-sql keywords (SELECT, FROM, WHERE, GROUP BY, HAVING) etc. etc. and use pascal casing for field names and table names.
Same here
MajorMullet is offline   Reply With Quote
Old 12-17-2012, 10:11 AM   #16
Evadman
Administrator Emeritus
Elite Member
 
Evadman's Avatar
 
Join Date: Feb 2001
Posts: 30,970
Default

Quote:
Originally Posted by KLin View Post
I usually capitalize t-sql keywords (SELECT, FROM, WHERE, GROUP BY, HAVING) etc. etc. and use pascal casing for field names and table names.
This, and it is what I teach everyone else that I work with.
__________________
Dan (Schmin) 1981-2003. Rest in Peace
I want to drinkify all my snacks. Chewing is for people living in the 19th century.
My Blog
Evadman is offline   Reply With Quote
Old 12-18-2012, 10:51 AM   #17
pauldun170
Golden Member
 
pauldun170's Avatar
 
Join Date: Sep 2011
Posts: 1,931
Default

We are an all caps shop here.
With rent-a-devs floating in and out the easiest and quickest way to enforce a convention is just make everything all caps when it comes to queries.
It is more than just readability\making the resident coder nazi's happy.

The performance boost from shared statements directly impacts application performance at least for repeated simple queries.


SELECT BITCHES FROM UP_IN_HERE WHERE UPPER(BOOTY) = 'BOUNCING'
is not the same as
SELECT Bitches FROM Up_in_here WHERE upper(BOOTY) = 'BOUNCING'
is not the same as
select bitches from up_in_here where upper(booty) = 'BOUNCING'

If there are lot of queries looking for bouncing booty...we don't want the RDBMS to have to go through every little song and dance routine to fetch the data again and again.

After years and years of having mixed skill sets\experience floating through the shop, we've found that one of the ways to ensure consistency and reduce headaches at least when it comes to sql is to hand them the naming convention cheat sheet and to rock the caps lock.

There are always times where folks will check in a script\code with the convention they are used to but since its easy to spot we are pretty quick to identify the perp (who obviously doesn't listen or read the mandatory toilet reading when joining the department), implement a public shaming and place the code under public scrutiny.

HOWEVER, this doesn't apply to all of our scripts (procedures\functions etc etc where typical coding conventions are used).

In summary
1. All my SQL queries are shouted (caps lock rocked)
2. Strict naming convention policies.
3. Rules of the shop trump whatever standard your used to or comfortable with.
pauldun170 is offline   Reply With Quote
Old 01-04-2013, 11:00 PM   #18
Evadman
Administrator Emeritus
Elite Member
 
Evadman's Avatar
 
Join Date: Feb 2001
Posts: 30,970
Default

Quote:
Originally Posted by pauldun170 View Post
SELECT BITCHES FROM UP_IN_HERE WHERE UPPER(BOOTY) = 'BOUNCING'
1. All my SQL queries are shouted (caps lock rocked)
That is hard as hell to read. I wonder how much time is spent debugging vs using mixed case.
__________________
Dan (Schmin) 1981-2003. Rest in Peace
I want to drinkify all my snacks. Chewing is for people living in the 19th century.
My Blog
Evadman is offline   Reply With Quote
Old 01-05-2013, 01:29 AM   #19
Markbnj
Moderator
Programming
 
Markbnj's Avatar
 
Join Date: Sep 2005
Posts: 11,742
Default

Quote:
Originally Posted by pauldun170 View Post
SELECT BITCHES FROM UP_IN_HERE WHERE UPPER(BOOTY) = 'BOUNCING'
is not the same as
SELECT Bitches FROM Up_in_here WHERE upper(BOOTY) = 'BOUNCING'
is not the same as
select bitches from up_in_here where upper(booty) = 'BOUNCING'
I thought SQL was case insensitive. You're saying the engine treats those as different queries?
__________________
Everytime I try to tell you, the words just come out wrong

**
Some meaningless scribbling of no account

The 4th Realm

Arts and Letters Daily - Get some culture
Markbnj is offline   Reply With Quote
Old 01-05-2013, 03:41 AM   #20
beginner99
Platinum Member
 
Join Date: Jun 2009
Posts: 2,121
Default

Quote:
Originally Posted by Markbnj View Post
I thought SQL was case insensitive. You're saying the engine treats those as different queries?
maybe MySQL does but it also does a lot of other weird things. /bashing

I also have never heard of this...
beginner99 is offline   Reply With Quote
Old 01-07-2013, 02:10 PM   #21
Evadman
Administrator Emeritus
Elite Member
 
Evadman's Avatar
 
Join Date: Feb 2001
Posts: 30,970
Default

Quote:
Originally Posted by beginner99 View Post
maybe MySQL does but it also does a lot of other weird things. /bashing

I also have never heard of this...
If installed on a windows system, MySQL table names and databases are not case sensitive. If installed on some flavor of *ix, it IS case sensitive, and goes by the underlying OS rules. A database corresponds to a folder in the OS, and a table corresponds to a file. *ix OS's are case sensitive for folders and files, so that means MySQL is too on those OS's. Same thing with table aliases, and other things.

I can think of nothing to call this behavior besides absolutely retarded.
__________________
Dan (Schmin) 1981-2003. Rest in Peace
I want to drinkify all my snacks. Chewing is for people living in the 19th century.
My Blog
Evadman is offline   Reply With Quote
Old 01-07-2013, 02:55 PM   #22
Obsoleet
Platinum Member
 
Obsoleet's Avatar
 
Join Date: Oct 2007
Location: CHICAGO (South Loop)
Posts: 2,160
Default

Usually a mix, but if I use one or the other, I use all lowercase.
__________________
Intel C2Q 9450@3ghz | Intel X25-M G2 160GB | MSI Radeon 5870 (latest WHQLs)
Ubuntu 14.04 + Win7Pro | 8GB Mushkin XP2-6400 (4-4-4-12) | Lian Li PC-A05NB
Asus P5Q-E (P45 / ICH10R) | Corsair HX650 | Asus U3S6 | Asus VS278Q-P + 2x Dell P2210s
+ Samsung PN50B650 | External Seagate GoFlex 1.5TB USB 3.0 + External LiteOn IHES208 BR


Anandtech forums on trial: The corruption runs deep.
Obsoleet is offline   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 04:42 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.