@Leo Do you know what happens if Trump is selected as the GOP candidate but then has to pull out due to legal or health problems? Would the runner-up then get the nomination by default?
And is there any credible alternative to Biden on the other side?
Trump has cockroach DNA, he will never die or pull out due to legal issues (the cases will drag out for years and he'll use them to play up his martyr image). Also, there is no clearcut runner up, since each state conducts its own primary election. You could say the candidate with the second-most state wins is next in line, but they don't allocate electoral votes (ie, points) like in the general election. Not sure what would happen, maybe the party would select someone (and it could be the "next in line" but not necessarily).
I don't think Biden will be opposed in the Dem primary. A few people have declared, but they're insignificant and won't ever reach the polling and fundraising criteria needed to qualify for the debate stage.
Has there ever been an election where the sitting president has tried to stand again but lost in the primary and failed to become the candidate?
Have no idea about throughout history, but that sort of happened to Lyndon Johnson. He was all set to cruise to re-election when the Tet Offensive blew up his Vietnam plans. He barely beat Eugene McCarthy (a weak candidate) in the first primary, then Bobby Kennedy declared himself a candidate. Johnson knew he couldn't beat the young, charismatic Kennedy, so he dropped out of the race.