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 : 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 and . 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 where has arity and the are terms. We use set semantics: a database gives each relation name a finite set 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
with , where the body is a list of atoms, the head is a tuple of terms, and each variable of 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 from the variables to constants, extended to be the identity on constants. The answer of on is
is Boolean if its head is empty. Its answer is then the empty tuple (true) or nothing (false). is full if it has no existential variables.
Equivalent forms. The following describe the same class of queries:
- Rules. The rule form above.
- Logic. First-order formulas where the are relational atoms or equalities. An equality is removed by replacing by everywhere, and an equality by replacing by .
- Algebra. Relational algebra expressions built from the relations with selection and , projection, renaming and join (or product). These are the SPJ expressions.
- Tableaux. A table with one row per atom and a summary row for the head, as Aho, Sagiv and Ullman use for minimization.
- SQL.
SELECT DISTINCTqueries whoseWHEREclause is a conjunction of equalities between columns and constants.DISTINCTgives set semantics. Without it, SQL uses bag semantics, and the theory on this page changes.
Canonical database. The canonical database of has one fact for each atom of the body, with each variable read as a new constant of its own. The identity valuation shows that . This database is the key to the Chandra-Merlin theorem.
Hypergraph. The hypergraph of has the variables as vertices and one edge per atom, the set of variables of that atom. Its structure decides how hard is to evaluate: see acyclic hypergraph, hypertree width and fractional hypertree width.
Basic facts. Let be the number of atoms of and the number of tuples of .
- Monotone (theorem). If relation by relation, then .
- Preserved by homomorphisms (theorem). If is a map of constants with that fixes the constants of , then . Compose each valuation with .
- Data complexity (theorem). For a fixed , the naive method tries all choices of one tuple per atom, so takes polynomial time in . Since is a fixed first-order formula, the problem is in the circuit class . See data and combined complexity.
- Combined complexity (theorem). When is part of the input, deciding whether a Boolean CQ is true on is NP-complete (Chandra and Merlin 1977). It is the same problem as the constraint satisfaction problem with as the template (Kolaitis and Vardi). For acyclic queries the Yannakakis algorithm takes polynomial time even in combined complexity.
Examples and non-examples
Let be a binary relation, the edges of a directed graph.
- Paths of length two. . In algebra, . In SQL,
SELECT DISTINCT a.src, b.dst FROM E a, E b WHERE a.dst = b.src. The variable is existential. - Triangles. The Boolean query is true if and only if the graph has a directed cycle of length 3, or a loop, since , and need not be different. Its hypergraph is a triangle, so it is not acyclic.
- Constants. returns the employees of the sales department.
- Repeated variables. returns the nodes with a loop. A repeated variable is an equality selection.
- Non-example: union. is not a CQ. If a CQ were equivalent to it, then by the Sagiv-Yannakakis theorem would be contained in alone or in alone. Say it is . On the database , the union returns and returns nothing. Unions of CQs (UCQs) are the next larger class.
- Non-example: negation. is not monotone: adding to removes the answer . So no CQ expresses it.
- Non-example: inequality. is true on . The map sends this database to , where is false. CQs are preserved by such maps, so no CQ expresses .
- 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
COUNTare outside the class.
Related definitions
- Union of conjunctive queries (UCQ). A finite union of CQs with heads of the same arity. UCQs are the positive existential queries: , and , without negation. Containment is decided member by member, by the Sagiv-Yannakakis theorem.
- Containment and equivalence. holds if and only if there is a homomorphism from to . 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 , or in the body. Containment becomes -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
- 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
- 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
- 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
- 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
- Answering queries using views
- Bucket algorithm
- Certain answers
- Conjunctive query minimization
- Consistent query answering
- Equality-generating dependency
- Factorized representation
- Global-as-view and local-as-view
- MiniCon algorithm
- Sagiv-Yannakakis theorem
- Schema mapping
- Tuple-generating dependency
- NP-completeness of rewriting queries using views
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag PG1J3V Edit this page History Report an error Markdown