SQL Insights

Help the SQL Engine Do Its Job Better

We hear so much about prompt writing for ChatGPT to get great answers. Crafting a strong prompt reminds me of writing quality SQL queries; we achieve better results, often faster. SQL is a declarative language, so we typically don’t instruct it on how to perform its job.

For the most part, we trust the engine that has been around for decades to make better decisions than we can. However, that’s not to say the optimizer always makes the best decision, especially with outdated statistics and poorly designed indexes.

To ask a more specific question and have SQL return data for a single year, such as 2024, include a WHERE clause instead of omitting it for better results.

SELECT Id,
 SalesAmount,
 SalesPersonId,
 OrderDate
FROM Sales.Orders
WHERE OrderDate > '12-31-2023'
 AND OrderDate < '01-01-2025'

help