Query languages allow us to compactly query the data inside a database based on a data model. There are two main types of query paradigms:

  • Imperative querying follows from imperative programming. It may iterate through the data through a sequential list of instructions.
  • Declarative querying allow us to specify the pattern of data we want, i.e., what conditions the results must meet, and how we want the data to be transformed (sorted, grouped, aggregated). We do not specify how this goal is achieved — this is done by the database system’s optimisation systems to decide how the query is executed.
    • This carries many benefits: it’s easier for us to work with and is usually more concise. It also abstracts many details of the database, so the database can be optimised (with parallelisation) without changes to the query structure.

Some declarative systems follow relational algebra very closely. As a concrete example, let’s search for sharks within a list of animals. Consider imperatively iterating through a data structure and saving the value if it’s a shark, versus a relational approach:

sharks = σ(animals)

Declarative methods are also used on the web, and are a much more compact way to program. In CSS selectors, for example, we are able to modify according to a specific pattern of elements:1

li.selected > p {
	background-color: blue;
}

Language list

Footnotes

  1. From Designing Data-Intensive Applications, by Martin Kleppmann.