It is rather easy to recognize a start of an SQL-Statement, because very few words are used. Mostly Select, Update, Insert or Delete and very few other words.
The problem is to recognize the end of the Statement. Are there semicola (";") at the end?
The rest is as easy as shown by holli.