Filter a feature layer with SQL

Learn how to use a SQL query to limit displayed in a .

A hosted can contain a large number of . To display a subset of the features, you can filter features on the server-side with a definition expression. Definition expressions are different than feature layer queries: they only support a SQL where clause without a (spatial) parameter, and are only used to filter features at the time they are displayed in a or . They cannot be used to get features like a feature layer query.

In this tutorial, you will apply a server-side SQL query with a definitionExpression to filter the LA County Parcels .

Prerequisites

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Get an access token

You need an with the correct privileges to access the used in this tutorial.

  1. Go to the Create an API key tutorial and create an with the following :
    • Privileges
      • Location services > Basemaps
  2. In CodePen, set esriConfig.apiKey to your access token.
    1
    2
    3
    4
    5
    6
    7
    8
    9
      var esriConfig = {
        apiKey: "YOUR_ACCESS_TOKEN"
      };
    

To learn about other ways to get an access token, go to Types of authentication.

Create a selector

Use a Calcite Select component to provide a list of SQL queries for the LA County Parcels .

  1. Add an arcgis-placement component after the arcgis-zoom component within the <arcgis-map> to place the selector in the top-right corner of the map.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
        <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.027" zoom="13">
    
          <arcgis-zoom position="top-left"></arcgis-zoom>
    
          <arcgis-placement position="top-right">
    
          </arcgis-placement>
    
        </arcgis-map>
    
    Expand
  2. Add a Calcite Select component within the arcgis-placement component. This component has child Option components, each with a different SQL query.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
          <arcgis-placement position="top-right">
    
            <calcite-select id="sqlSelect">
              <calcite-option value="1=0" label="Choose a SQL where clause..."></calcite-option>
              <calcite-option value="Roll_LandValue < 200000" label="Roll_LandValue < 200000"></calcite-option>
              <calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option>
              <calcite-option value="Bedrooms5 > 0" label="Bedrooms5 > 0"></calcite-option>
              <calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option>
              <calcite-option value="Roll_RealEstateExemp > 0" label="Roll_RealEstateExemp > 0"></calcite-option>
            </calcite-select>
    
          </arcgis-placement>
    
    Expand
  3. Verify that the select component is created.

Add modules and a map component event listener

  1. Add a <script> tag in the <body> following the <arcgis-map> component with a require statement. In the require statement, add the FeatureLayer module.

    Within the require statement, use the document.querySelector() method to access the map and select components.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
      <body>
    
        <arcgis-map basemap="arcgis/topographic" center="-118.805, 34.027" zoom="13">
    
          <arcgis-zoom position="top-left"></arcgis-zoom>
    
          <arcgis-placement position="top-right">
    
            <calcite-select id="sqlSelect">
              <calcite-option value="1=0" label="Choose a SQL where clause..."></calcite-option>
              <calcite-option value="Roll_LandValue < 200000" label="Roll_LandValue < 200000"></calcite-option>
              <calcite-option value="TaxRateArea = 10853" label="TaxRateArea = 10853"></calcite-option>
              <calcite-option value="Bedrooms5 > 0" label="Bedrooms5 > 0"></calcite-option>
              <calcite-option value="UseType = 'Residential'" label="UseType = 'Residential'"></calcite-option>
              <calcite-option value="Roll_RealEstateExemp > 0" label="Roll_RealEstateExemp > 0"></calcite-option>
            </calcite-select>
    
          </arcgis-placement>
    
        </arcgis-map>
    
        <script>
    
          require(["esri/layers/FeatureLayer"], (FeatureLayer) => {
    
            const arcgisMap = document.querySelector("arcgis-map");
            const selectFilter = document.querySelector("#sqlSelect");
    
          });
    
        </script>
    
      </body>
    
    Expand
  2. Create an event listener to listen for the map component's arcgisViewReadyChange event.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
        <script>
    
          require(["esri/layers/FeatureLayer"], (FeatureLayer) => {
    
            const arcgisMap = document.querySelector("arcgis-map");
            const selectFilter = document.querySelector("#sqlSelect");
    
            arcgisMap.addEventListener("arcgisViewReadyChange", () => {
    
            });
    
          });
    
        </script>
    
    Expand

Create a feature layer to filter

Use the FeatureLayer class to access the LA County Parcels . Since you are performing a server-side query, the feature layer does not need to be added to the map. However, to view the results of the query, the feature layer will be added to the map.

  1. Create a featureLayer and set the url property to access the in the . Set the outFields property to return all on the client and the popupTemplate to display the parcel description and land value. Set the definitionExpression to 1=0 so no features are displayed when the layer is loaded. Add featureLayer to the map.
    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
            arcgisMap.addEventListener("arcgisViewReadyChange", () => {
    
              const featureLayer = new FeatureLayer({
                url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
                outFields: ["*"],
                popupTemplate: {
                  title: "{UseType}",
                  content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
                },
                definitionExpression: "1=0"
              });
              arcgisMap.addLayer(featureLayer);
    
            });
    
    Expand

Apply the SQL expression

The definitionExpression is the SQL where clause. Use the expression to apply a filter and limit displayed in the map.

  1. Create a setFeatureLayerFilter function with an expression parameter. Set the definitionExpression to filter the with the expression.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
            arcgisMap.addEventListener("arcgisViewReadyChange", () => {
    
              const featureLayer = new FeatureLayer({
                url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
                outFields: ["*"],
                popupTemplate: {
                  title: "{UseType}",
                  content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
                },
                definitionExpression: "1=0"
              });
              arcgisMap.addLayer(featureLayer);
    
              // Server-side filter
              function setFeatureLayerFilter(expression) {
                featureLayer.definitionExpression = expression;
              }
    
            });
    
    Expand
  2. Add an event listener to call the setFeatureLayerFilter function when a SQL where clause is chosen from the selector.

    Expand
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
            arcgisMap.addEventListener("arcgisViewReadyChange", () => {
    
              const featureLayer = new FeatureLayer({
                url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/LA_County_Parcels/FeatureServer/0",
                outFields: ["*"],
                popupTemplate: {
                  title: "{UseType}",
                  content: "Description: {UseDescription}. Land value: {Roll_LandValue}"
                },
                definitionExpression: "1=0"
              });
              arcgisMap.addLayer(featureLayer);
    
              // Server-side filter
              function setFeatureLayerFilter(expression) {
                featureLayer.definitionExpression = expression;
              }
    
              // Event listener
              selectFilter.addEventListener("calciteSelectChange", (event) => {
                setFeatureLayerFilter(event.target.value);
              });
    
            });
    
    Expand

Run the app

In CodePen, run your code to display the map.

When the map displays, you should be able to choose a SQL query from the selector that applies a definition expression to the visible of the map. Only the that match are added to the and displayed in the .

What's next?

Learn how to use additional API features and ArcGIS services in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.

The developer dashboard has moved

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close