Everybody likes Walmart Inc.’s everyday low prices. But can Walmart keep everybody on board?

The big-box company said on Tuesday that comparable-store sales at Walmart U.S. grew 8.2% in the quarter ended Oct. 28, well ahead of the 3.4% increase Wall Street analysts polled by Visible Alpha were penciling in. While Walmart turned to a net loss last quarter, that was due to a $3.3 billion charge related to opioid legal settlements. Excluding one-off charges and the effect of currency fluctuations, Walmart’s operating income grew by an impressive 4.6% last quarter.

Inventory was up 12.6% last quarter compared with a year earlier, a marked improvement from the 25% growth Walmart saw a quarter earlier. Much of that growth in inventory (roughly 70%) was due to inflation, and Walmart estimates that “just under $1 billion” worth of inventory is true excess merchandise. Walmart shares jumped nearly 7% after its earnings call on Tuesday morning.

Consumers are continuing to shift spending away from discretionary goods toward essentials, a shift that benefits grocery-heavy retailers such as Walmart, though it does come with lower margins. At Walmart U.S., comparable-store sales of grocery products grew by a midteens percentage, while sales of higher-margin general merchandise declined. Demand was particularly weak in electronics, home and apparel, though lawn and garden, automotive and back-to-school products did better. Walmart said it saw trade-downs in proteins, baking goods, baby products and dog food.

The more eye-catching development for Walmart, though, is traffic from higher-earning consumers. Walmart said that three-quarters of the grocery market share it saw last quarter came from households that make more than $100,000 in annual income. While part of that can be chalked up to inflation’s effect on customer wallets, Walmart deserves some credit for improving the kind of services that the higher-income cohort wants. The company said Tuesday that its last-mile delivery capabilities increased fourfold since January; that has helped increase sign-ups for Walmart+ memberships. Also notable: Its online marketplace added 50% more in stock-keeping units last quarter alone compared with the prior quarter.

Keeping this cohort of new customers will be key to its so-called flywheel strategy, which involves growing high-margin revenue streams such as sales of advertising and fulfillment services to vendors and third-party marketplace sellers. Walmart said its global advertising business grew more than 30% last quarter, and analytics firm eMarketer estimates that Walmart will see a 40% increase in ad spending this year. That is double Amazon.com Inc.’s expected growth rate, albeit from a lower base. For four consecutive quarters, Walmart U.S. has called out growth in membership and “other income,” which includes that advertising bucket, as offsets to gross margin pressure.

After the rally on Tuesday, Walmart’s shares trade at 23.3 times forward-12-month earnings, 6% above its five-year average. Its shares are up 2.9% year to date, outperforming Amazon by roughly 43 percentage points. If Walmart can convince investors that its fancy, new customers will stick around, it could hang on to its fancy, new valuation too.

Write to Jinjoo Lee at jinjoo.lee@wsj.com