You didn't mention a date or time of day which of course is relevant but expect to need about 8 hours so it's possible to do.
But you'd need at least a bus from Amalfi to the nearest station (about one hour) so it's not possible to do the complete trip by train.
Thank you very much for your reply. We are leaving Scala area of Amalfi on the 24th of April for Alberobello.
If it takes 7-8 hours, we have no problem. Thank you again.
Ok, I had a look from Amalfi itself.
Of course it depends how long it takes from your place at the Analfi coast to the nearest station.
BTW: The connection from Amalfi mentioned is not via Napoli and not via Bari. And there are more bus than train connections (especially for the last part to Alberobello).
Via Bari, it's 1h 5min by bus to Alberobello from there (train connection needs more than two hours). Best connection from Napoli Centrale is less than 6 hours.