This code is strange to me. Not like anything I have coded before. Can you explain what each line is doing.
It's probably best to move this discussion to Tom's wiki as suggested in his message above. But let me provide a small bit of a general overview. SQL may look strange because it's a non-procedural programming language. Many report writing systems are non-procedural programming languages. The idea is that you describe what happens to one record in a file and then that process is applied to every record in the file. You don't have to open or close the file, and you don't have to loop through the file. You might even say that RM's custom report feature is a non-procedural programming language.
There really aren't that many different SQL statements, but each possible statement has many, many options.The SELECT statement is the only query statement. For updating a file, there is UPDATE (changes an existing record), INSERT (adds a new record), and DELETE (deletes an existing record). There are other statements for defining new databases and defining new tables. Files are called databases, and a database consists of one or more tables. A table has rows and columns, just like a spreadsheet. The rows of the table are the records. The columns of the table are the data fields. The SQL statements are designed to operate on these rows and columns.
There are lots of SQL tutorials and help files on the Internet for free. I use them all the time. But I would still recommend the purchase at least one SQL book (like a real book, printed on paper). I'm not sure of exact titles, but the "Dummies" series or the "Made Simple" series might not be bad places to start. Finally, I would point out that many programming languages have different dialects that are not quite compatible with each other, and SQL has more dialects than most other languages. At some point, you will have to be sure to be using the SQLite dialect of SQL. It's pretty standard but not completely standard.