The SNP's Mark McDonald has won the Aberdeen Donside Holyrood by-election.
It followed the death of the SNP MSP Brian Adam in April, aged 64, after a battle with cancer.
Mr McDonald secured 9,814 votes, while Labour's Willie Young in second got 7,789, meaning the majority was down more than 5,000 on the 2011 result.
Lib Dem Christine Jardine was third and Conservative Ross Thomson was fourth. Mr McDonald said: "I will work hard to earn trust, as Brian Adam did."
Mr McDonald said his win, coming in the middle of the SNP's second term in government in Scotland, was "an excellent victory".
He said: "The opposition parties threw everything but the kitchen sink at this by-election campaign. To have held it with a good four-figure majority, in the face of that kind of campaign, I think demonstrates the strong record of the SNP."
He added: "I look forward to working hard for the people of Donside, and if I work hard for them in the way Brian Adam did I am sure we will see those majorities increasing at future elections."
In the last Scottish Parliament election Mr Adam won the seat with 14,790 votes, giving him a majority of 7,175 over Labour.
That was reduced to 2,025 in the by-election, with Labour candidate Willie Young polling 7,789 votes.
The turnout was 38.8% and the result came just after 01:50 on Friday.
There was a swing of just over 9% from the SNP to Labour, with Mr Young delighted to have taken more than 5,000 out of the Nationalist majority.
Mr Young, a councillor in Aberdeen, said the result showed his party could win the seat in the 2016 Holyrood elections and added: "All over Scotland people will be seeing the Labour Party has put in a strong performance and other seats are there for the taking."
The Liberal Democrats, who were fourth in the seat in 2011, overtook the Conservatives to take third place with Christine Jardine polling 1,940 votes.
Conservative Ross Thomson, who took 1,791 votes, insisted he was pleased with the result.
He said the Tory share of the vote "held up remarkably well", adding that in 2011 the UK Independence Party (Ukip) did not contest the seat.
Ukip candidate Otto Inglis took 1,128 votes - 4.83% of votes cast - meaning his party lost its deposit.
Mr Thomson, who is a councillor in Aberdeen, said his party was also squeezed as the contest was seen as a two-horse race between the SNP and Labour.
He claimed Labour targeted the Tory vote, urging people to vote for them in a bid to keep the SNP out.
Mr Thomson said: "Because our voters are such committed unionists that had an appeal for some of them."
First Minister Alex Salmond congratulated Mr McDonald on a "fantastic SNP win", adding he would be a "first-class representative for the people of Donside".
Mr Salmond said: "Midway into our second term of government and we have comfortably retained a seat which Labour once held.
"Their failure to make significant progress is a major setback. This is a very bad result for Labour - after six years of opposition, they show no sign of being anything more than an opposition party.
"I am also delighted that Ukip failed to retain their deposit. They have never saved a single deposit in Scotland, which once again demonstrates a clear divergence between Scottish and Westminster politics."
The by-election result means the SNP still has an overall majority in the Scottish Parliament with 65 of the 129 MSPs.
The results have also been announced of two separate council by-elections held in Edinburgh and Fife on Thursday.
Labour's Keith Robson gained the Liberton and Gilmerton ward in Edinburgh from the SNP.
In the contest for the Glenrothes North, Leslie and Markinch ward at Fife Council, John Wincott successfully defended the seat for Labour.