# Queries in Business Central
Queries enable you to retrieve records from one or more tables or data sources and then combine the data into rows and columns in a single dataset. Queries can also perform calculations on data, such as finding the sum or average of all values in a column of the dataset.
## Query Types
There are two types of query objects: normal and API.
 A normal query retrieves records from business data tables in the Dynamics 365 Business Central database, and can be used to display data in the user interface. This type of query is created by a query object. See devenv-query-object.
 An API query is used to generate web service endpoints and this type of page cannot be displayed in the user interface. A query of the API type can be used to join data from different data sources. The data can only be viewed. For information about creating a query of the type API, see API Query Type<devenv-api-querytype>.
### Query usage scenarios
The following examples show how you can use queries in your Business Central application.
 Allowing users to analyze data from a query using the data analysis mode. See Analyze query data using analysis mode</analysis-mode?toc=/dynamics365/business-central/dev-itpro/toc.json>.
 Exposing data as an OData web service. You can register and publish a query as a web service in the same way that you can register and publish pages or codeunits as web services. You use the Web Services page to register and publish pages, codeunits, or queries. After you expose a query as a web service, you can import it into other applications.
 Using the query as a data source for a page. To do this, you have to copy the query resulting dataset into a temporary table and set it as the source table for the page.
 Using the query as a data source for a report. To do this, create a global variable that points to the query. Then use the variable in the report dataset. For more information see, Defining a Report Dataset<devenv-report-dataset>.
 Performing calculations on data such as computing sums and averages. See devenv-query-totals-grouping.
 Replacing nested loops that use record variables to retrieve or to detect duplicate records. See Using Queries Instead of Record Variables<devenv-query-using-instead-record-variables>.
 Creating charts that are based on a query instead of a table.
 Saving the data from a query as an .xml or .csv file. For example, you can use the methods-auto/query/query-saveasxml-integer-string-method to create an .xml file that contains the resulting dataset of a query. You can use the .xml file to integrate with external applications.
## Query Data Type
The query resulting dataset cannot be used directly by other objects such as pages and reports. Instead, you must create a methods-auto/query/query-data-type instance and apply methods for handling the data from the query object. You must first call the methods-auto/query/queryinstance-open-method to be able to perform actions such as reading the dataset or setting filters and you must use the methods-auto/query/queryinstance-close-method when you are finished. You can write this code in a codeunit, for example. The following example illustrates how to do this.
```al
var
    MyQuery: Query "Customer SalesQuantity";
    Text000: Label 'Customer name = %1, Quantity = %2';
begin
    // Sets a filter to display only sales quantities greater than 20.
    MyQuery.SETFILTER(Quantity, '>20');
    // Runs the query.
    MyQuery.Open;
    // Reads each row in the dataset and displays a message with column values.
    // Stops reading when there are no more rows remaining in the dataset (READ is FALSE).
    while MyQuery.Read do
    begin
      Message(Text000, MyQuery.Name, MyQuery.Quantity);
    end;
    MyQuery.Close;
end;
```
## Related information
devenv-query-object
Linking and Joining Data Items<devenv-query-links-joins>
Aggregating Data in Query Objects<devenv-query-totals-grouping>
properties/devenv-query-properties
Analyze query data using analysis mode</analysis-mode?toc=/dynamics365/business-central/dev-itpro/toc.json>
Using Queries Instead of Record Variables<devenv-query-using-instead-record-variables>
Developing Extensions<devenv-dev-overview>
AL Development Environment<devenv-reference-overview>
Utilizing Read Scale-Out for Better Performance<../administration/database-read-scale-out-overview>

Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-query-overview, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact. https://jonas-paprotka.de/docs/dev-itpro/developer/devenv-query-overview/
