Tutorial: Working with Attribute Table - Basic Queries

Tutorial moved to  http://www.qgistutorials.com/en/docs/working_with_attributes.html

GIS data has two parts - features and attributes. Attribute Table contains data about each feature. Here I will show you how to carry out some basic operations on the attribute table using Quantum GIS.

The example here contains a point shapefile of populated places around the world. These are from Natural Earth public domain dataset. The task is to find ‘all capital cities in the world that have a population greater than 1000000’.

  1. Download the dataset and extract the files to a folder on your harddrive.
  2. Open QGIS. Click on Layer → Add Vector Layer.

  1. Locate the shapefile 10m_populated_places.shp in your computer and click ‘Open’.

  1. The data will be displayed in the QGIS Canvas.

  1. Right-click on the layer name and select ‘Open Attribute Table’.

  1. Explore the various attributes and their values.

  1. We are interested in the population of each feature, so the POP_MAX is the field we are looking for. You can click on the field header to sort the column in ascending or descending order.

  1. Click on ‘Advanced Search’ at the bottom on the attribute table.

  1. Type in your query as ‘POP_MAX > 1000000’. You can also click on the field value and various operators to form your query. Click OK.

  1. You will see the selected records highlighted in the attribute table. You can click the ‘Move selection to top’ button on bottom-left to bring all selected records to the top.

  1. If you look at the map area, the selected records will be highlighted in yellow.

  1. Now, let’s refine the query to select only those cities which are capitals. The field containing this data is ADM0CAP. In the attribute table, check the box ‘Search selected records only’ and click ‘Advanced search’.

  1. Type in the query ‘ADM0CAP = 1’. Click OK.

  1. You can see that the selection changes to highlight only those cities that are both capitals and have population greater than 1000000.

  1. Now let’s export this selection to a new file. Right click on the layer and select ‘Save selection as...’.

  1. Browse to appropriate directory and name the output file as ‘10m_large_capital_cities.shp’.

  1. Once the export is complete, load the layer into QGIS via Layer → Add Vector Layer. Your final map showing all capital cities having a population greater than 1000000 is ready!