The “direct product problem” is a fundamental question in complexity theory which seeks to understand how the difficulty in computing a function on each of k independent inputs scales with k. We prove the following direct product theorem (DPT) for query complexity: if every T-query algorithm has success probability at most \({1 - \varepsilon}\) in computing the Boolean function f on input distribution μ, then for α ≤ 1, every \({\alpha \varepsilon Tk}\)-query algorithm has success probability at most \({(2^{\alpha \varepsilon}(1-\varepsilon))^k}\) in computing the k-fold direct product \({f^{\otimes k}}\) correctly on k independent inputs from μ. In light of examples due to Shaltiel, this statement gives an essentially optimal trade-off between the query bound and the error probability. Using this DPT, we show that for an absolute constant α > 0, the worst-case success probability of any α R 2(f) k-query randomized algorithm for \({f^{\otimes k}}\) falls exponentially with k. The best previous statement of this type, due to Klauck, Špalek, and de Wolf, required a query bound of O(bs(f) k). Our proof technique involves defining and analyzing a collection of martingales associated with an algorithm attempting to solve \({f^{\otimes k}}\). Our method is quite general and yields a new XOR lemma and threshold DPT for the query model, as well as DPTs for the query complexity of learning tasks, search problems, and tasks involving interaction with dynamic entities. We also give a version of our DPT in which decision tree size is the resource of interest.