# Query by Example

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Query_by_Example
> Markdown URL: https://mediated.wiki/source/Query_by_Example.md
> Source: https://en.wikipedia.org/wiki/Query_by_Example
> Source revision: 1303158230
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Database query language

Example of QBE query with joins, designed in Borland's [Paradox database](/source/Paradox_database)

**Query by Example** (**QBE**) is a [database](/source/Database) [query language](/source/Query_language) for [relational databases](/source/Relational_database).

## History

Query by Example was devised by [Moshé M. Zloof](https://en.wikipedia.org/w/index.php?title=Mosh%C3%A9_M._Zloof&action=edit&redlink=1) at [IBM Research](/source/IBM_Research) during the mid-1970s, in parallel to the development of [SQL](/source/SQL),[1] and influenced by the work on [relational databases](/source/Relational_database) of [Edgar Codd](/source/Edgar_Codd).[2] It is the first graphical query language, using visual tables where the user would enter commands, example elements and conditions. Many graphical front-ends for databases use the ideas from QBE today. Originally limited only for the purpose of [retrieving data](/source/Data_retrieval), QBE was later extended to allow other operations, such as inserts, deletes and updates, as well as creation of temporary tables.

The motivation behind QBE is that a [parser](/source/Parser) can convert the user's actions into statements expressed in a database manipulation language, such as [SQL](/source/SQL). Behind the scenes, it is this statement that is actually executed. A suitably comprehensive front-end can minimize the burden on the user to remember the finer details of SQL, and it is easier and more productive for end-users (and even programmers) to select tables and columns by selecting them rather than typing in their names.

In the context of [information retrieval](/source/Information_retrieval), QBE has a somewhat different meaning. The user can submit a document, or several documents, and ask for "similar" documents to be retrieved from a document database [see search by multiple examples[3]]. Similarity search is based comparing document vectors (see [Vector Space Model](/source/Vector_Space_Model)).

QBE represents seminal work in [end-user development](/source/End-user_development), frequently cited in research papers as an early example of this topic.

Currently, QBE is supported in several relational database front ends, notably [Microsoft Access](/source/Microsoft_Access), which implements "Visual Query by Example", as well as Microsoft SQL Server Enterprise Manager. It is also implemented in several [object-oriented databases](/source/Object_database) (e.g. in [db4o](/source/Db4o)[4]).

QBE is based on the logical formalism called [tableau query](https://en.wikipedia.org/w/index.php?title=Tableau_query&action=edit&redlink=1), although QBE adds some extensions to that, much like SQL is based on the [relational algebra](/source/Relational_algebra).

## Example

An example using the [Suppliers and Parts database](/source/Suppliers_and_Parts_database) is given here to illustrate how QBE works.

Simple QBE example S S# SNAME OWNER SCITY P.SX J. DOE ROME

## As a general technique

The term also refers to a general technique influenced by Zloof's work whereby only items with search values are used to "filter" the results. It provides a way for a software user to perform queries without having to know a query language (such as [SQL](/source/SQL)). The software can automatically generate the queries for the user (usually behind the scenes). Here are two examples based on a Contacts table with the following text (character) columns: Name, Address, City, State, and Zipcode:

Contacts Query Form - Example **A**:
.....Name: Bob
..Address:
.....City:
....State: TX
..Zipcode:

Resulting [SQL](/source/SQL):

SELECT * FROM Contacts WHERE Name='Bob' AND State='TX';

Note how blank items do not generate [SQL](/source/SQL) terms. Since "Address" is blank, there is no clause generated for it.

Contacts Query Form - Example **B**:
.....Name:
..Address:
.....City: Sampleton
....State:
..Zipcode: 12345

Resulting [SQL](/source/SQL):

SELECT * FROM Contacts WHERE City='Sampleton' AND Zipcode='12345';

More advanced versions of QBE have other comparison operator options, often via a pull-down menu, such as "Contains", "Not Contains", "Starts With", "Greater-Than", and so forth.

Another approach to text comparisons is to allow one or more [wildcard character](/source/Wildcard_character) characters. For example, if an asterisk is designated as a wildcard character in a particular system, then searching for last names using "Rob*****" would return (match) last names such as "Rob", "Robert", "Robertson", "Roberto", etc.

Contacts Query Form - Example **C**:
.....Name: Rob*
..Address:
.....City:
....State:
..Zipcode:

Resulting [SQL](/source/SQL):

SELECT * FROM Contacts WHERE Name LIKE 'Rob%'

In standard SQL, the percent sign functions like a wildcard in a LIKE clause. In this case, the query-by-examplme form processing software would translate the asterisk to a percent sign. (An asterisk is a more common wildcard convention outside of SQL, so here the form is attempting to be more [user friendly](/source/User_friendly).)

WARNING: Query-by-example software should be careful to avoid [SQL injection](/source/SQL_injection). Otherwise, devious users may penetrate further into the database than intended by builders of the query forms.

## See also

- [CRUD](/source/CRUD)

- [Microsoft Query by Example](/source/Microsoft_Query_by_Example)

- [GraphQL](/source/GraphQL) a QBE for JSON front-ends.

- [QBIC](/source/QBIC)

## References

1. **[^](#cite_ref-1)** Zloof, M.M. (1977). "Query-by-Example: A data base language". *IBM Systems Journal*. **16** (4): 324–343. [CiteSeerX](/source/CiteSeerX_(identifier)) [10.1.1.86.3485](https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.86.3485). [doi](/source/Doi_(identifier)):[10.1147/sj.164.0324](https://doi.org/10.1147%2Fsj.164.0324).

1. **[^](#cite_ref-rdbmsearlyyearsoh20070612_2-0)** ["RDBMS Plenary 1: Early Years"](https://archive.computerhistory.org/resources/access/text/2013/05/102702562-05-01-acc.pdf) (PDF) (Interview). Interviewed by Burton Grad. Computer History Museum. 2007-06-12. pp. 23–24. Retrieved 2025-05-30.

1. **[^](#cite_ref-3)** Zhu, Mingzhu; Wu, Yi-Fang Brook (2014-02-24). "Search by multiple examples". *Proceedings of the 7th ACM international conference on Web search and data mining*. ACM. pp. 667–672. [doi](/source/Doi_(identifier)):[10.1145/2556195.2556206](https://doi.org/10.1145%2F2556195.2556206). [ISBN](/source/ISBN_(identifier)) [9781450323512](https://en.wikipedia.org/wiki/Special:BookSources/9781450323512). [S2CID](/source/S2CID_(identifier)) [8397529](https://api.semanticscholar.org/CorpusID:8397529).

1. **[^](#cite_ref-4)** "QBE", [*db4o*](http://www.ibm.com/developerworks/java/library/j-db4o2.html), Developer works Java library, IBM, 27 March 2007

## Sources

- Zloof, Moshé M. (May 1975), "Query by Example", *National Computer Conference*, AFIPS Conference Proceedings, vol. 44, Anaheim, California: AFIPS Press, pp. 431–8, [ISSN](/source/ISSN_(identifier)) [0095-6880](https://search.worldcat.org/issn/0095-6880).

- [Ramakrishnan, Raghu](/source/Raghu_Ramakrishnan); [Gehrke, Johannes](/source/Johannes_Gehrke), ["6. QBE"](http://www.cs.wisc.edu/~dbbook/openAccess/thirdEdition/qbe.pdf) (PDF), *Database Management Systems* (3rd ed.), Wisc.

- Date, Christopher ‘Chris’ J. (2004), "8. Relational Calculus", *An Introduction to Database Systems*, Addison-Wesley Pearson, pp. 242–7, [ISBN](/source/ISBN_(identifier)) [978-0-321-18956-1](https://en.wikipedia.org/wiki/Special:BookSources/978-0-321-18956-1).

- [*Oracle Definitions*](https://web.archive.org/web/20060614102510/http://searchoracle.techtarget.com/sDefinition/0,,sid41_gci214554,00.html), Tech target, archived from [the original](http://searchoracle.techtarget.com/sDefinition/0,,sid41_gci214554,00.html) on 2006-06-14, retrieved 2006-06-25.

- Zaiane, O. (1998), ["5. Query-by-Example (QBE)"](http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter5/node2.html), *CC 354 notes*, Canada: SFI.

## External links

- ["Query by Example in Java using Hibernate"](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-examples). JBoss.

- Soylu, Ahmet; Giese, Martin; Jimenez-Ruiz, Ernesto; Vega-Gorgojo, Guillermo; Horrocks, Ian (2015). ["Experiencing OptiqueVQS: a multi-paradigm and ontology-based visual query system for end users"](https://web.archive.org/web/20221205202950/http://www.ahmetsoylu.com/wp-content/uploads/soylu_UAIS_2016.pdf) (PDF). *Universal Access in the Information Society*. **15**: 129–152. [doi](/source/Doi_(identifier)):[10.1007/s10209-015-0404-5](https://doi.org/10.1007%2Fs10209-015-0404-5). [hdl](/source/Hdl_(identifier)):[11250/2388519](https://hdl.handle.net/11250%2F2388519). [S2CID](/source/S2CID_(identifier)) [254169347](https://api.semanticscholar.org/CorpusID:254169347). Archived from [the original](http://www.ahmetsoylu.com/wp-content/uploads/soylu_UAIS_2016.pdf) (PDF) on 2022-12-05. Retrieved 2021-11-12. Visual Query System (VQS)

---
Adapted from the Wikipedia article [Query by Example](https://en.wikipedia.org/wiki/Query_by_Example) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Query_by_Example?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
