The solubility of a salt in water depends upon the lattice energy and hydration energy.
For sodium and potassium hydroxides and carbonates, hydration energy is more than that of their lattice energy. Therefore, they are soluble in water. For magnesium and calcium hydroxides and carbonates, lattice energy is greater than that of their hydration energy. Therefore, these are sparingly soluble in water.