title: Select all geospatial points inside a bounding box
body: <p>I have a table called 'flags' in a MySQL database with 400,000 rows. This table is made up of geospatial points that represent different positions around the UK.</p>
  
  <p>The application I'm creating uses Google Maps. On the map is a button that should toggle the flags visibility on the map. It's my job now to create an API that when passed a bounding box all flags inside the bounding box are returned (so that they can then be displayed on the map).</p>
  
  <p>The passed parameters are the North East latitude/longitude and the South West latitude/longitude of the viewport's current position.</p>
  
  <p>I now need to perform an SQL query that will return all geospatial points that are inside this sets of coordinates (the viewport).</p>
  
  <p>Ideally the solution needs to be optimized as there are many rows to search. The application does force you to zoom into a certain level before the user can reveal the flags, however.</p>
  
  <p>Flags table:</p>
  
  <ul>
  <li>id</li>
  <li>coordinates</li>
  <li>name</li>
  </ul>
  
  <p>Example row:</p>
  
  <p>1 | [GEOMETRY - 25B] | Tenacy AB</p>
  
  <p>The coordinates field can also be converted to a literal point by using AsText(coordinates). The X and Y functions do this for you however.</p>
  
  <p>The coordinates column is data type: POINT</p>
  
  <p>I know that to obtain the latitude/longitude of a point you can use the X and Y functions. For example, the latitude of a point can be retrieved like this: X(coordinates) </p>
  
  <p>DBMS: MySQL<br>
  DBMS version: 5.6.14</p>