For all the success and rapid expansion of Wal-Mart Stores(NYSE:WMT) online grocery efforts, it's still well behind Amazon (NASDAQ:AMZN). Amazon has taken 18% of all online grocery sales in the United States in 2017, twice as much as Wal-Mart's 9% share, according to research firm Packaged Facts.

Wal-Mart's online grocery ordering and in-store pickup has expanded to over 1,000 locations, and it expects to add another 1,000 locations next year. Indeed, Wal-Mart's online grocery sales have been a key part to its booming online sales.

But even the country's largest grocer can't compete with Amazon when it comes to selling items online. And if it's anything like the rest of online sales, the gap will only get bigger.

A man delivering two AmazonFresh bags to a woman on her doorstep.

Image source: Amazon.

Whole Foods provides scale for AmazonFresh

Amazon's acquisition of Whole Foods was a huge signal Amazon is serious about getting into the grocery space. The day the companies announced the deal, shares of nearly every other grocery store chain declined.

Indeed, Amazon has taken its typical price-slashing, margin-squeezing approach with Whole Foods so far. It also started selling Whole Foods' Whole 365 brand items on Amazon.com.

The bigger opportunity, however, is using Whole Foods as a way to scale AmazonFresh. The program is currently limited to a few select metro areas.

As a big buyer of groceries already, Whole Foods gives Amazon increased buying power for groceries. That's key if Amazon wants to compete with pricing from Wal-Mart or national grocery stores partnering with companies like Instacart. Delivering online grocery orders comes with added costs, which is why Wal-Mart is keen to push people to its stores.

Whole Foods also gives Amazon 450 or so distribution centers around the country. Whole Foods used to call them stores, and Amazon kept that naming convention in its third-quarter earnings report, if only for appearances. Those stores should help reduce Amazon's fulfillment costs if it expands AmazonFresh, by helping bridge the last mile.

Earlier this year, Wal-Mart started experimenting with using its store employees to deliver online orders after their shifts. Wal-Mart's 4,000 U.S. locations constitute one of its biggest advantages over Amazon. But Amazon may be able to piece together an equally impressive fulfillment network by using Whole Foods locations.

Online grocery ordering is exploding

PackagedFacts expects online grocery orders to triple by 2022, reaching $41.7 billion. Amazon's competition in the space is fierce, but the company is well-positioned to continue dominating. Even if Amazon only keeps pace with the market, that's still another $5 billion or so it can add to the top line. It's more likely Amazon's online grocery sales increase more than that considering its clear ambition in the area.

Online grocery sales may actually be much more important to Wal-Mart than Amazon. As the nation's largest grocer, Wal-Mart stands to lose significantly from each grocery sales that moves from brick-and-mortar stores to online. What's more, online grocery provides an opportunity for Wal-Mart to bring in customers to its general e-commerce site and compete more effectively with Amazon. Indeed, grocery sales appear to be a key part to Wal-Mart's booming online business.

As online grocery sales continue to climb, investors in both Amazon and Wal-Mart should watch each company's developments closely. While Amazon has invested a lot in groceries with its Whole Foods acquisition, Wal-Mart might have more to lose if it can't close the gap between itself and the market leader.

John Mackey, CEO of Whole Foods Market, an Amazon subsidiary, is a member of The Motley Fool's board of directors. Adam Levy owns shares of Amazon. The Motley Fool owns shares of and recommends Amazon. The Motley Fool has a disclosure policy.