Sandstone, a sedimentary rock, is formed when grains of sand are compacted and cemented together over thousands or millions of years. The sand grains often are composed of the minerals quartz or feldspar that were worn off other rocks and ground down into pebbles.