Filter a feature layer with SQL

Learn how to use a SQL query to limit features displayed in a feature layer.

A hosted feature layer can contain a large number of features. 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 geometry (spatial) parameter, and are only used to filter features at the time they are displayed in a map or scene. 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 feature layer .

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 access token with the correct privileges to access the location services used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):
    • Privileges
      • Location services > Basemaps
  2. In CodePen, set esriConfig.apiKey to your access token.
    Use dark colors for code blocks
    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 feature layer.

  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
    Use dark colors for code blocks
    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
    Use dark colors for code blocks
    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
    Use dark colors for code blocks
    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
    Use dark colors for code blocks
    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 feature layer. 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 feature layer in the feature service. Set the outFields property to return all attributes 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
    Use dark colors for code blocks
    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 features displayed in the map.

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

    Expand
    Use dark colors for code blocks
    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
    Use dark colors for code blocks
    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 extent of the map. Only the features that match are added to the feature layer and displayed in the view.

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.