Your search returned no results

Try another search term or view Stack Exchange, where we have active conversations with Developers about GIS.
    Support channel in Stack Exchange

    Thousands of developers around the world have helped make CartoDB a success, and we have active conversations with them about GIS in Stack Exchange.

    Go to Stack Exchange

    Getting Data with SQL

    CARTO offers a powerful SQL API for you to query and retreive data from your CARTO tables. CARTO.js offers a simple to use wrapper for sending those requests and using the results.


    cartodb.SQL is the tool you will use to access data you store in your CARTO tables. This is a really powerful technique for returning things like: items closest to a point, items ordered by date, or GeoJSON vector geometries. It’s all powered with SQL and our tutorials will show you how easy it is to begin with SQL.


    Name Description
    format should be GeoJSON.
    dp float precision.
    jsonp if jsonp should be used instead of CORS. This param is enabled if the browser does not support CORS.

    These arguments will be applied to all the queries performed by this object. If you want to override them for one query see execute options.


    var sql = new cartodb.SQL({ user: 'cartodb_user' });
    sql.execute("SELECT * FROM table_name WHERE id > {{id}}", { id: 3 })
      .done(function(data) {
      .error(function(errors) {
        // errors contains a list of errors
        console.log("errors:" + errors);

    sql.execute(sql [,vars][, options][, callback])

    It executes a sql query.


    Name Description
    sql a string with the sql query to be executed. You can specify template variables like which will be filled with vars object.
    vars a map with the variables to be interpolated in the sql query.
    options accepts format, dp and jsonp. This object also overrides the params passed to $.ajax.


    A promise object. You can listen for the following events:

    Events Description
    done triggered when the data arrives.
    error triggered when something failed.


    You can also use done and error methods:

    sql.execute('SELECT * FROM table_name')

    sql.getBounds(sql [,vars][, options][, callback])

    This query gets the bounding box for any dataset or filtered query using the CARTO.js library. The getBounds function is useful for guiding users to the right location on a map, or for loading the right data (at the right time), based on user actions.

    Returns the bounds [ [sw_lat, sw_lon], [ne_lat, ne_lon ] ] for the geometry resulting of specified query.


    Name Description
    sql a string with the sql query to calculate the bounds from.


    sql.getBounds('select * from table').done(function(bounds) {

    getBounds and Leaflet

    You can use the results from getBounds to center data on your maps using Leaflet.

    sql.getBounds('select * from table').done(function(bounds) {
      // or map.fitBounds(bounds, mapView.getSize());