Algorithm Search⌘K

Definitioncore

Conjunctive query

Also called select-project-join query, SPJ query.

A query built from relational atoms with conjunction and existential quantification; it is the select-project-join part of SQL.

A conjunctive query asks for the ways to fill a pattern of facts with values from a database. It is a rule Q(xˉ): ⁣−R1(uˉ1),…,Rm(uˉm)Q(\bar x) \mathbin{:\!-} R_1(\bar u_1), \ldots, R_m(\bar u_m): the body lists the atoms that must hold, and the head lists the variables to return. In logic it is a first-order formula with only ∃\exists and ∧\wedge. In relational algebra it is an expression with selections on equalities, projections and joins, which is why it is also called a select-project-join (SPJ) query. Most queries that databases run are conjunctive queries or unions of them, and their theory is the best understood part of query languages: containment is decidable by the Chandra-Merlin theorem, and each query has a unique smallest equivalent form.

Definition

A schema is a finite set of relation names, each with an arity. A term is a variable or a constant. An atom is R(t1,…,tr)R(t_1, \ldots, t_r) where RR has arity rr and the tit_i are terms. We use set semantics: a database DD gives each relation name RR a finite set RDR^D of tuples of constants, and a query returns a set of tuples.

Definition (Chandra and Merlin 1977). A conjunctive query (CQ) over a schema is a rule

Q(xˉ): ⁣−R1(uˉ1),…,Rm(uˉm)Q(\bar x) \mathbin{:\!-} R_1(\bar u_1), \ldots, R_m(\bar u_m)

with m≥1m \ge 1, where the body R1(uˉ1),…,Rm(uˉm)R_1(\bar u_1), \ldots, R_m(\bar u_m) is a list of atoms, the head xˉ\bar x is a tuple of terms, and each variable of xˉ\bar x occurs in the body. This last condition is safety. The variables of the body that are not in the head are the existential variables.

A valuation is a map vv from the variables to constants, extended to be the identity on constants. The answer of QQ on DD is

Q(D)={ v(xˉ)  :  v is a valuation with v(uˉi)∈RiD for i=1,…,m }.Q(D) = \{\, v(\bar x) \;:\; v \text{ is a valuation with } v(\bar u_i) \in R_i^D \text{ for } i = 1, \ldots, m \,\}.

QQ is Boolean if its head is empty. Its answer is then the empty tuple (true) or nothing (false). QQ is full if it has no existential variables.

Equivalent forms. The following describe the same class of queries:

  1. Rules. The rule form above.
  2. Logic. First-order formulas φ(xˉ)=∃yˉ (A1∧⋯∧Am)\varphi(\bar x) = \exists \bar y \, (A_1 \wedge \cdots \wedge A_m) where the AiA_i are relational atoms or equalities. An equality x=yx = y is removed by replacing yy by xx everywhere, and an equality x=cx = c by replacing xx by cc.
  3. Algebra. Relational algebra expressions built from the relations with selection σA=B\sigma_{A = B} and σA=c\sigma_{A = c}, projection, renaming and join (or product). These are the SPJ expressions.
  4. Tableaux. A table with one row per atom and a summary row for the head, as Aho, Sagiv and Ullman use for minimization.
  5. SQL. SELECT DISTINCT queries whose WHERE clause is a conjunction of equalities between columns and constants. DISTINCT gives set semantics. Without it, SQL uses bag semantics, and the theory on this page changes.

Canonical database. The canonical database DQD_Q of QQ has one fact Ri(uˉi)R_i(\bar u_i) for each atom of the body, with each variable read as a new constant of its own. The identity valuation shows that xˉ∈Q(DQ)\bar x \in Q(D_Q). This database is the key to the Chandra-Merlin theorem.

Hypergraph. The hypergraph of QQ has the variables as vertices and one edge per atom, the set of variables of that atom. Its structure decides how hard QQ is to evaluate: see acyclic hypergraph, hypertree width and fractional hypertree width.

Basic facts. Let kk be the number of atoms of QQ and nn the number of tuples of DD.

  • Monotone (theorem). If D⊆D′D \subseteq D' relation by relation, then Q(D)⊆Q(D′)Q(D) \subseteq Q(D').
  • Preserved by homomorphisms (theorem). If hh is a map of constants with h(D)⊆D′h(D) \subseteq D' that fixes the constants of QQ, then h(Q(D))⊆Q(D′)h(Q(D)) \subseteq Q(D'). Compose each valuation with hh.
  • Data complexity (theorem). For a fixed QQ, the naive method tries all nkn^k choices of one tuple per atom, so Q(D)Q(D) takes polynomial time in nn. Since QQ is a fixed first-order formula, the problem is in the circuit class AC0\mathrm{AC}^0. See data and combined complexity.
  • Combined complexity (theorem). When QQ is part of the input, deciding whether a Boolean CQ is true on DD is NP-complete (Chandra and Merlin 1977). It is the same problem as the constraint satisfaction problem with DD as the template (Kolaitis and Vardi). For acyclic queries the Yannakakis algorithm takes polynomial time even in combined complexity.

Examples and non-examples

Let EE be a binary relation, the edges of a directed graph.

  • Paths of length two. Q(x,z): ⁣−E(x,y),E(y,z)Q(x, z) \mathbin{:\!-} E(x, y), E(y, z). In algebra, π1,4(σ2=3(E×E))\pi_{1, 4}(\sigma_{2 = 3}(E \times E)). In SQL, SELECT DISTINCT a.src, b.dst FROM E a, E b WHERE a.dst = b.src. The variable yy is existential.
  • Triangles. The Boolean query Q(): ⁣−E(x,y),E(y,z),E(z,x)Q() \mathbin{:\!-} E(x, y), E(y, z), E(z, x) is true if and only if the graph has a directed cycle of length 3, or a loop, since xx, yy and zz need not be different. Its hypergraph is a triangle, so it is not acyclic.
  • Constants. Q(x): ⁣−Emp(x,d),Dept(d,’Sales’)Q(x) \mathbin{:\!-} \mathit{Emp}(x, d), \mathit{Dept}(d, \text{'Sales'}) returns the employees of the sales department.
  • Repeated variables. Q(x): ⁣−E(x,x)Q(x) \mathbin{:\!-} E(x, x) returns the nodes with a loop. A repeated variable is an equality selection.
  • Non-example: union. R(x)∨S(x)R(x) \vee S(x) is not a CQ. If a CQ QQ were equivalent to it, then by the Sagiv-Yannakakis theorem QQ would be contained in R(x)R(x) alone or in S(x)S(x) alone. Say it is R(x)R(x). On the database {S(a)}\{S(a)\}, the union returns aa and QQ returns nothing. Unions of CQs (UCQs) are the next larger class.
  • Non-example: negation. Q(x): ⁣−R(x),¬S(x)Q(x) \mathbin{:\!-} R(x), \neg S(x) is not monotone: adding S(a)S(a) to {R(a)}\{R(a)\} removes the answer aa. So no CQ expresses it.
  • Non-example: inequality. Q(): ⁣−E(x,y),x≠yQ() \mathbin{:\!-} E(x, y), x \neq y is true on {E(a,b)}\{E(a, b)\}. The map a,b↦ca, b \mapsto c sends this database to {E(c,c)}\{E(c, c)\}, where QQ is false. CQs are preserved by such maps, so no CQ expresses QQ.
  • Non-example: counting. "Nodes with at least as many out-edges as in-edges" is not monotone, and no CQ expresses it. Aggregates such as COUNT are outside the class.
  • Union of conjunctive queries (UCQ). A finite union of CQs with heads of the same arity. UCQs are the positive existential queries: ∃\exists, ∧\wedge and ∨\vee, without negation. Containment is decided member by member, by the Sagiv-Yannakakis theorem.
  • Containment and equivalence. Q1⊆Q2Q_1 \subseteq Q_2 holds if and only if there is a homomorphism from Q2Q_2 to Q1Q_1. See the Chandra-Merlin theorem and the query containment problem.
  • Minimal query and core. Each CQ has an equivalent CQ with the fewest atoms, unique up to renaming. See conjunctive query minimization.
  • CQ with comparisons. Atoms x<yx < y, x≤yx \le y or x≠yx \ne y in the body. Containment becomes Π2p\Pi_2^p-complete.
  • Datalog rule. A Datalog rule has the same form as a CQ, and a non-recursive Datalog program is a UCQ.
  • Acyclic CQ. A CQ whose hypergraph is alpha-acyclic. The GYO reduction tests this.
  • Dependencies. The two sides of a tuple-generating dependency are conjunctions of atoms, so a TGD says that on the database each answer of one CQ is also an answer of another CQ.
  • Bag semantics. If answers are counted with multiplicity, as in SQL without DISTINCT, CQ equivalence becomes isomorphism of queries, and containment is much harder.

References

  1. Ashok K. Chandra and Philip M. Merlin, Optimal implementation of conjunctive queries in relational data bases, Proceedings of the 9th Annual ACM Symposium on Theory of Computing (STOC), 77 to 90 (1977), the definition of conjunctive queries, their equivalence with select, project and join expressions, and the NP-completeness of evaluation, containment and equivalence
  2. Moshe Y. Vardi, The complexity of relational query languages, Proceedings of the 14th Annual ACM Symposium on Theory of Computing (STOC), 137 to 146 (1982), the separation of data complexity and combined complexity of query languages
  3. Phokion G. Kolaitis and Moshe Y. Vardi, Conjunctive-query containment and constraint satisfaction, Journal of Computer and System Sciences 61(2), 302 to 332 (2000), conjunctive query evaluation and containment as constraint satisfaction
  4. Serge Abiteboul, Richard Hull and Victor Vianu, Foundations of Databases, Addison-Wesley (1995), Chapter 4 (conjunctive queries), the rule-based, tableau, calculus and algebra forms and their equivalence

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/PG1J3V/text.yaml