What Is Geography Data Type in SQL?


Scott Campbell

What Is Geography Data Type in SQL?

In SQL, the geography data type is used to store spatial data that represents locations on the Earth’s surface. It allows you to perform various spatial operations, such as calculating distances between points, checking for intersections between geometries, and determining the area covered by polygons.

Why Use the Geography Data Type?

The geography data type is particularly useful when working with real-world geographic data, such as addresses, cities, countries, and coordinates. By storing this information in a geography column, you can easily query and analyze spatial relationships.

Benefits of Using the Geography Data Type:

  • Spatial Analysis: The geography data type provides built-in functions for performing spatial operations, allowing you to analyze and manipulate geographic data efficiently.
  • Accuracy: Unlike plain text or numeric fields, the geography data type preserves the integrity of spatial information by storing it in a standardized format.
  • Interoperability: SQL databases that support the geography data type often provide compatibility with popular GIS (Geographic Information System) software. This enables easy integration with other mapping and analysis tools.

Working with the Geography Data Type

To work with the geography data type in SQL, you’ll need a database system that supports it. Some commonly used databases that include native support for geography are Microsoft SQL Server and PostgreSQL.

To create a table with a geography column, you can use a statement similar to:

CREATE TABLE Locations (
    LocationName VARCHAR(50),
    LocationPoint GEOGRAPHY

The above statement creates a table called “Locations” with two columns: “LocationName” of type VARCHAR and “LocationPoint” of type GEOGRAPHY.

Performing Spatial Operations

Once you have a table with a geography column, you can start performing spatial operations on the data. Here are a few examples:

  • Calculating Distance:
SELECT LocationName
FROM Locations
WHERE LocationPoint.STDistance(geography::Point(12.9715987, 77.5945627, 4326)) <= 10000;

The above query retrieves all locations within a distance of 10 kilometers from the specified latitude and longitude coordinates.

  • Finding Intersections:
SELECT LocationName
FROM Locations
WHERE LocationPoint.STIntersects(geography::STPolyFromText('POLYGON((12.9715987 77.5945627, 12.9758272 77.5963678, 12.9770215 77.5909784, 12.5945627))',4326)) = 1;

This query retrieves all locations that intersect with the specified polygon.


The geography data type in SQL provides a powerful tool for working with spatial data. By leveraging its capabilities, you can easily analyze geographic information and perform various spatial operations within your database system.

Whether you’re building a location-based application or conducting spatial analysis, understanding the geography data type will greatly enhance your SQL skills and enable you to work with real-world geographic data effectively.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy