A petrographic investigation revealed polyphase quartz cementation in the Finefrau Sandstone (Upper Carboniferous, Western Germany) and the Solling Sandstone (Lower Triassic, Central Germany). Three different cements could be distinguished in each sandstone based on their cathodoluminescence and trace element composition. The first quartz generation is suggested to have been formed during eogenesis due to dissolution and replacement of feldspar. The mesogenetic paragenesis comprises two generations of quartz and illite, which are accompanied by albite in the Solling Sandstone. Sharp luminescence zoning in quartz overgrowths points to distinct episodes of cementation in both sandstones. Significant amounts of Al, Li and H and traces of Ge and B have been detected in the quartz overgrowths. The Al-content of the quartz cements in the Finefrau Sandstones exceeds that in the quartz cements in the Solling Sandstone by a factor of five. It is suggested that this compositional variation reflects the conditions in the pore-water, such as temperature and pH. The Al-concentration is generally correlated to the Li-content with the exception of the latest quartz generation in the Finefrau Sandstones which is also most enriched in trace elements. The ratio of Li/Al varies between 0·11 and 0·25 in the two sandstones. The Li/H-ratio, which ranges from 0·12 to 0·3, is controlled by the activity ratio of Li and H in the pore fluid. Clay minerals are the most important source for Li and high salinities favour the mobilization of Li during diagenesis. Thus, a relatively low salinity and low pH are responsible for the low Li/H-ratio in the Finefrau Sandstone, while high salinity and neutral to alkaline pH results in a high Li/H-ratio for the Solling Sandstone. The Ge-contents are generally near the average of detrital quartz and indicate that pressure dissolution is a major source for quartz cementation. Different chemical compositions of distinct quartz generations indicate changes in the physico-chemical conditions and point to mobilization of silica from different sources (for example, pressure solution and clay mineral transformations).