San Francisco and San Jose — home to some of the nation’s leading tech success stories — have the highest average total savings rate in their workplace retirement accounts, according to analysis by Fidelity Investments.

The firm’s study of 13 million 401(k) investors across metropolitan regions revealed which cities and its citizens are saving the most — or least — and which are more likely to have an outstanding 401(k) loan.

The typical worker in San Francisco and San Jose puts about 14.6 percent of their annual salary — including both employee and employer contributions— in 401(k)s,  the survey found.

Raleigh, N.C., came in third with 14 percent, followed by Houston, with 13.9 percent and Hartford, Conn., with 13.8 percent.

San Jose also topped the list of highest average employee contribution rate, with 11.1 percent, while San Francisco came in second with 10.4 percent.

Honolulu, Boston and Bridgeport, Conn., also ranked high on employee contribution rate, with 9 percent each.

Madison, Wisc., had the lowest outstanding 401(k) loan rate of just 10 percent. San Jose and San Francisco were not far behind, though, with just 11.1 percent for the former and 13.4 percent for the latter.

“The good news is that Americans nationwide are saving for retirement,” said Jim MacDonald, president of Workplace Investing at Fidelity Investments. “These cities lead the way with some of the most robust 401(k) savers in the country. High savings rates and low proportions of outstanding 401(k) loans indicate that these workers are on the right path.”

On the other end of the spectrum, McAllen, Texas, had the highest 401(k) loan usage, with 33 percent of workers having an outstanding loan balance.

The cities of Riverside and Bakersfield in California were not far behind, with 32 percent and 31.3 percent, respectively. El Paso, Texas, followed with 30.7 percent, while Youngstown, Ohio, had 29.9 percent.

Here are the top and bottom 5:

5 cities with highest average total savings rate, ranked from highest to lowest

1. San Francisco, 14.6%

2. San Jose, 14.6%

3. Raleigh, N.C., 14%

4. Houston, 13.9%

5. Hartford, Conn., 13.8%

5 cities with lowest average total savings rate, ranked from lowest to highest

1. Chattanooga, Tenn., 9.5%

2. El Paso, Texas, 9.8%

3. McAllen, Texas, 10%

4. Provo, Utah, 10.4%

5. Jackson, Miss., 10.4%