title: Fetch all points from database inside a bounding box of north east and south west coordinates in MySQL
body: <p>I am currently building an application to show all geo-tagged trees in a particular city.
  The main columns I am using to fetch data from the table are as follows,</p>
  
  <pre><code>+-----------------+-------------+------+-----+---------+-------+
  | Field           | Type        | Null | Key | Default | Extra |
  +-----------------+-------------+------+-----+---------+-------+
  | tree_geotags_id | int(11)     | NO   | PRI | None    |       |
  | lattitude_dl    | double(9,7) | YES  |     | NULL    |       |
  | longitude_dl    | double(9,7) | YES  |     | NULL    |       |
  +-----------------+-------------+------+-----+---------+-------+
  </code></pre>
  
  <p>The table has over 158000 rows.</p>
  
  <p>Currently I am using the following query to get my output,</p>
  
  <pre><code>SELECT gt.tree_geotags_id, gt.latitude_dl, gt.longitude_dl,
      SQRT(
          POW( 69.1 * ( gt.latitude_dl - [ center_lat ] ), 2) +
          POW( 69.1 * ( [ center_lon ] - gt.longitude_dl ) * COS( gt.latitude_dl / 57.3 ), 2 )
      ) AS distance
      FROM tree_geotags_t gt
      HAVING distance &lt; 0.4 ORDER BY distance
  </code></pre>
  
  <p>What this does is, it fetches all records at a radius of 0.4.
  I use an ajax call to fetch the data, every time the center coordinate of the map changes( on map pans or zooms ), convert the fetched data into geojson format and then load it on the map as a layer. The issue I am having with this is, in locations where there is a very high density of trees, it takes a long time for the map to place all the points and since it fetches it on a radius it loads points that are even outside the viewport.</p>
  
  <p><strong><em>I need a query to only load data in coordinates inside the viewport, using the northeast and southwest coordinates as boundaries.</em></strong> I searched for quite a while here but couldn't find anything suited to my requirements. Please help me out. Thanks in advance..!!</p>