In Harry Potter and the Philosopher's Stone (the first book), the Sorting Hat starts to sing, describing the different houses.
Why does it do so?
According to Ron it's a tradition. He ascribed the motivation as probably being boredom.
The Great Hall rang with applause as the Sorting Hat finished.
‘That’s not the song it sang when it sorted us,’ said Harry, clapping along with everyone else.
‘Sings a different one every year,’ said Ron. ‘It’s got to be a pretty boring life, hasn’t it, being a hat? I suppose it spends all year making up the next one.’ Harry Potter and the Goblet of Fire