Just curious, but what data is stored in a TrackElement? Are you mimicking some structure from MC, or is this a structure you designed yourself? I just started looking at MC yesterday, really, so as far as it is concerned I'm a total newb.
I'm just taking a wild guess, but it looks like you might be using individual TrackElements when you should just be using a single row in a table. I could be totally wrong, though. Not sure how much info you want to share in public, but a look at your data structure may go a long way to streamlining things.
The problem with using IF in SQL is that SQL is a query language, and isn't supposed to be used for procedural code. They have added procedural code elements, because sometimes it's just more efficient to use logic at the database end to restrict the amount of data you have to send back to the client (i.e. remote connections, etc.), but overall it is preferred to keep as much of it out of your queries as possible.
As for JOINs, I think they will make your life MUCH easier in the long run, and I'll be glad to help you out with those.
For example:
SELECT A.Artistname, T.AlbumName, T.TrackName
FROM Artist A
INNER JOIN Track T ON (T.ArtistName = A.ArtistName)
ORDER BY A.ArtistName
This gives you a complete list of your artists, their albums and the tracks on the album (assuming that AlbumName is a field in the Track table - maybe there's an Album table that I haven't seen yet?). If you just SELECT * using the above syntax, you get 1 row for each Track, but each row includes both Artist and Track information. It "joins" the two tables together into a single result, with the ON clause (T.ArtistName = A.ArtistName) telling it what data goes together.