Do earthquakes only happen near the ocean?

No, earthquakes can happen anywhere in the world, including inland areas. Earthquakes are caused by the movement of tectonic plates, which can occur anywhere on the planet.