🧠Second Brain
Search
SQL is declarative
Is SQL declarative? And why not imperative as you specify the from and every detail in the WHERE?
The key is the SQL optimizer that does everything in the background such as (looking at statistics, deciding if an index is useful, going for a nested, merged or hash join, etc.), see Is SQL declarative.
And yes, SQL (Structured Query Language) is considered a declarative language. This means that it focuses on describing the “what” — what the end result should be, rather than the “how” — how to achieve that result.
When you write a SQL query, you declare what data you’re interested in. For example:
SQLCopy code
|
|
In this query, you’re declaring your intention to get all columns of data from the “Employees” table where the “salary” is greater than 50,000. You’re not instructing the database on how to retrieve that data, i.e., you’re not outlining the step-by-step process the database should use to fetch this data.
This contrasts with imperative programming languages, which require that the developer specify exactly how to achieve a result. For instance, in a language like Python, to get a similar result, you might have to iterate over a list of dictionaries, checking each dictionary to see if it meets your criteria. In other words, you’re telling the program exactly what steps to take to get to the final result.
Why SQL still seems somewhat imperative (like specifying FROM and WHERE clauses) because SQL gives you the ability to specify conditions for your data retrieval. However, even though you’re providing detailed instructions, you’re still not directing the database on how to execute those instructions. The database management system is responsible for figuring out the most efficient way to execute your query.
Origin:
LinkedIn Comment
References:
Created 2023-05-25