The Problem – SFWGHO

When writing T-SQL, Microsoft’s take on the SQL database language, the commands have to come in a certain order. This is:

– SELECT (selecting data)

– FROM (stating which table to get data from)

– WHERE (any filtering conditions)

– GROUP BY (specifying any aggregation to perform)

– HAVING (any filtering to perform on aggregated data)

– ORDER BY (which order to display the final rows in)

It is vital that you put these commands in the right order, otherwise you’ll get a syntax error. The acronym for the commands is SFWGHO, so it’s really just a question of finding a way to remember this series of letters.

Solutions

When he first met this problem, the author of this article trawled the Internet for acronyms. I found one site which was running a competition, with one entrant:

Slippery Fish Will Garnish Hungry Orcas

Surely there must be better acronyms than that! A little reflection came up with the following ideas:

Six Fat Wives Gross Husbands Out

Sweaty Feet Will Give Horrible Odours

Out of these, the second seems better (it’s certainly in better taste!). So here is our recommendation for how to remember the order of commands in SQL:

SWEATY or SELECT

FEET or FROM

WILL or WHERE

GIVE or GROUP BY

HORRIBLE or HAVING

ODOURS or ORDER BY

So now there’s no excuse for getting the order of SQL commands wrong, we just need to find a way to remember to put commas in the right place all the time…

Leave a Reply

Your email address will not be published. Required fields are marked *