What is a query?

A query is a question or a request for information expressed in a formal manner. In computer science, a query usually refers to a request for information to be extracted from a database.

What is a database query?

In a database context, a query is a request for information or data made by a user and written in a specific format. The format is determined by the query language supported by that database, such as Structured Query Language. Like other query languages, SQL provides pre-defined standardized code that contains the instructions that a database can understand to generate appropriate results in response to a user's query.

Apart from writing specific query commands in a query language, some of the other ways to retrieve information from a database are the following:

  • Use available parameters. With this scenario, a user defines the parameters required for information exchange between functions and stored procedures to generate some output.
  • Query by example. With QBE, a user populates the data fields and values in a pre-built code template provided by the relational database and query language.
  • Use a query builder. A query builder provides an easy and interactive way to create database queries by choosing pre-defined, pre-populated options from a menu until all desired search criteria are selected. Most query builders enable query creation regardless of the data source and help experienced users create queries by typing in queries using the query builder app's syntax.

A database query may be either an action query or a select query. An action query asks for additional operations on data, such as insertion, updating, deleting or other forms of data manipulation. A select query retrieves data from the database.

What do queries do?

Primarily, queries are used to find specific data in a database by providing or filtering explicit criteria. Some query languages allow users to write and make multi-source data queries across different types of data: structured, semi-structured and unstructured.

Queries also automate data management tasks; help users summarize data; and perform some calculations or manipulations on it, such as append, delete, make a table and find total. For example, consider a relational database, which contains records or rows of information. If SQL is the query language used for this database, by using the SQL SELECT query users can choose data from a database and return it to an application. The result of the query is stored in a result table, which is called a result-set. Users can also break down the SELECT statement into other categories, such as FROM, WHERE and ORDER BY, or use the query to group and aggregate data for analysis or summarization purposes.

How do queries work?

A query gives meaning to the lines of code used in every data selection language so the database can understand what the user wants and execute actions accordingly. The database holds data in multiple rows and columns, with the rows containing data and the columns specifying the attributes for each piece of data. By querying the database, a user can retrieve some specific data and have it displayed in a specific format. As such, the user and the database both exchange information with each other as they both "speak" the same language.

A query parameter lets users run variations of a particular query. The user is prompted to insert a field value and then use that value to create a criterion. Users can choose from a list or menu of available parameters and use them as needed.

An example of SQL's query builder.
Figure 1. Example of creating a SQL query by selecting a table, a field and search criteria

What is a query in SQL?

The most popular query language is structured query language or SQL. One reason for its popularity is that SQL is specifically designed to interact with databases, even databases containing large amounts of data. So whether a user wants to access specific data in a database or perform some kind of manipulation on the data, SQL is one of the best choices available today.

SQL provides several pre-built query commands that can be used to retrieve or manipulate data in a database, as shown in Figure 1.

A list of SQL query commands.
Figure 2. Commands SQL users can use to make database queries.

Note that SQL and MySQL are not the same, as the latter is a software extension that uses SQL. Other SQL extensions include Oracle SQL and NuoDB.

In addition to SQL, other languages can also be used to make database queries, such as Assets Query Language, Cassandra Query Language, Datalog, Data Mining Extensions, Neo4j Cypher and XQuery.

What is query folding?

Query folding describes Microsoft Power Query's ability to generate a single query statement to retrieve and transform source data. The Power Query mashup engine, part of the Microsoft Power Apps offering, attempts to accomplish query folding to enhance data retrieval or transformation efficiency whenever possible.

Power Query is a data mashup and transformation tool that uses the M Language. For data sources such as relational and non-relational databases like Active Directory, OData or Microsoft Exchange, a mashup engine "translates" queries in M Language to a language understood by the underlying data source. This language is often SQL. When complex calculations and transformations are pushed directly to the source, Power Query uses relational database engines to handle large volumes of data efficiently.

Web search query

A web search query is different from a database query. While the latter refers to a request for information from a database, a web query is specifically a search engine query, which is a user's request to a search engine to generate results based on a specific user-provided topic or keyword phrase.

Thus, a web search query describes what users search for when they type a question or a word in search engines such as Bing, Google or Yahoo. Search engine queries provide information that is much different from SQL queries, as they don't require positional parameters or keywords. Instead, every search engine uses an algorithm and specific criteria -- the details of which typically are not revealed -- to search and find the most accurate results for the requests. The results are sorted based on significance and some other parameters, such as relevance, content quality, and date. Then, the list of results, known as a search engine results page, or SERP, are displayed in the web browser application.

The three types of search queries are navigational, informational and transactional. Navigational searches are intended to find a particular website. Informational searches are designed to cover a broad topic, such a comparison between iPhone and Android devices. Finally, transactional searches seek to complete a transaction, such as the purchase of a new product on an e-commerce site like Amazon.

Other kinds of queries

Some queries have nothing to do with databases or search engines. Among these are querySelector() in JavaScript and query errors in Facebook, part of Meta.

JavaScript's querySelector() helps users find the first element that matches a particular CSS selector. To return all matching elements, developers also use the querySelectorAll() method. Whenever the selector isn't valid, the process raises a SyntaxError exception. If there are no matches, the querySelector() returns null.

Query errors occur on Facebook for several reasons. Whenever this happens, users get a message such as "Error performing query." This is usually fixed by rebooting the device, refreshing the page, logging out from the Facebook account and then logging back in, or by clearing the device's cache and cookies.

Choosing which database management system depends on an organization's needs. Review the differences among SQL, NoSQL and NewSQL and how to select the right database system.

This was last updated in April 2024

Continue Reading About query

Dig Deeper on Database management

Business Analytics
Content Management