Seismic waves, which travel in the same way as sound waves, travel fastest through denser objects than objects with lower density.
The Newton-Laplace equation can be used to find the speed of sound through a gas:
It can also be used on any other material. This includes steel, rock, magma, and even the upper atmosphere around space. In this formula, c is referring to the speed of sound in the medium, K is the bulk modulus of the material, and `rho` is the density of the medium. The bulk modulus of a material describes how the material responds to compression, and is the ratio of how much a material compresses under uniform pressure.
Rock has a much higher bulk modulus than air. Think about how much air can be compressed into a cylinder versus rock. You would struggle to compress rock at all, whereas air yields easily to pressure.
The sound itself is a wave in the particles themselves, traveling through with motion. Particles in rock are closer together than those in the air, and can easily be pushed by those around them. This is why seismic waves travel better in rock.