As I understand it, an 'An' should be used before words that sound like they start with a vowel.
This can lead to ambigityambiguity like 'SQL database'. This could either be written as 'an SQL databse'database' or 'a SQL database' depending upon whether you pronounce SQL as 'S-Q-L' or 'sequel'.