The 20 most expensive ZIP codes

If you think it's expensive living in Silicon Valley or Beverly Hills, try buying a place in Sagaponack, New York.

The median sales price for a home in the tony Hamptons village is more than $5.1 million, making it the most expensive ZIP code in the U.S. when it comes to real estate, according to PropertyShark.

The analysis, which looked at median prices for homes that sold between January 2014 and May 2015, found the Eastern Long Island town was followed by Atherton, California, in Silicon Valley, and then Beverly Hills.

Read More The problem with million-dollar Hamptons rentals

In total, California claimed four of the top five spots, and 14 of the top 20. New York claimed all the other top ZIP codes, with the exception of Miami Beach, Florida, which came in ninth.

The median sales prices for the top 20 ZIP codes were all higher than $2 million, with each of the top 10 coming in at or above $2.6 million-about 10 times the national average median home price, according to the U.S. Census Bureau.

See below for the full list.



More From CNBC