I've seen a sentence like
Ich habe Deutsch an der Schule gelernt.
Ich habe Deutsch in der Schule gelernt.
Why is the preposition "an" used? Does it have something to do with the past?
Well, to me (as a Bavarian) both seems to be correct, even
specifies the act of learing or teaching at a school to me
specifies the location or the act of learning or teaching
You can't say 'an' in this case.
Again, this is my personal opinion.
As a German, I actually have not heard "an der Schule gelernt" and think it's incorrect in this context. "in der Schule" is correct.
"an" is only correct in the right context, like "An meiner Schule war es immer nett" (It was always nice at our school), referring to the building or place rather than school as an institution or part of your life. You can also say "an" if you're making reference to a specific school, like "Ich habe Deutsch an der Albert Einstein High School gelernt"
You can say "Ich habe Deutsch an der Schule gelernt", but only if you talk about specific school:
An der Schule means standing beside the school and in der Schule means inside the school.
Both versions are correct, and both versions are Standard German.
In German German (official language* in Germany, Luxembourg and Belgium) the correct term is:
*offical language means: laws and other legal documents are written in this language, and this language is taught in schools. Also newspapers and magazines are written in this language, and news speakers in TV and radio also use this language.
In Austrian German (official language in Austria and parts of Italy) the correct term is:
There is a third standardized variation of German which is Swiss Standard German (not to be confused with Swiss German, which is a group of German dialects spoken in Switzerland), which is official language in Switzerland and Liechtenstein. But i am not firm with Swiss Standard German, so I don't know if it is »an der Schule« or »in der Schule« in Switzerland.
What I said before was about standard German. But most people in the German sprachraum speak colloquial German, which is close to standard German, but influenced by regional and local dialects. In the colloquial German spoken in the south of Germany, i.e. in Bavaria, you find a lot of terms that are part of the official standard of Austrian German. »In der Schule« is one of it.
Since there are more people living in Bavaria than in Austria (12.8 M in Bavaria, 8.7 M in Austria) the closely related dialects spoken in Bavaria and Austria are called »bavarian dialects«. But the version of German language spoken in the south of German sprachraum is standardized as a standard language only in Austria (and in South Tyrol, which is a part of Italy), so it is called »Austrian German«.
The difference between »an der Schule« and »in der Schule« comes of the different way of thinking about what a school is in Germany and Austria.
In Germany people think of a school as an institution. Take some teachers, a director, and some students, and you have a school. When teachers are teaching students, sitting under trees in the green gras, then this would be a school. So when you attend school in Germany, you are at an institution named school:
When people in Austria hears the word »Schule«, they have the building in mind. A school is a building where teachers and students meet to learn. So when you go to school, you go into a building named school:
There is also a joke in Austria about Germans going to school:
This joke works, because for example »er geht zum See« means »he goes to the lake« it does not mean »he goes into the lake«. But in Germany the same construction is used for going to school: »er geht zur Schule«. In Austria this will be interpreted as: »He goes to the school, but does not enter it.« And this is, because in Austria the word »Schule« will more likely be understood as the name of a building, not as the name of an institution.