Joe Celko's SQL for Smarties: Advanced SQL Programming, Third Edition

The concept of a data element being atomic or scalar is usually taken to mean that it is represented with a single column in a table. This is not always true. A data element is atomic when it cannot be decomposed into independent, meaningful parts. Doing so would result in attribute splitting, a design flaw that we discussed in Section 1.1.11.
Consider an ( x, y) coordinate system. A single x or y value identifies a line of points, while the pair has to be taken together to give you a location on the plane. It would be inconvenient to put both coordinates into one column, so we model them in two columns.
Since geographical data is important, you might find it handy to locate places by their longitude and latitude, then calculate the distances between two points on the globe. This is not a standard function in any SQL, but it is handy to know.
Assume that we have values (Latitude1, Longitude1, Latitude2, Longitude2) that locate the two points, and that they are in radians, and we have trigonometry functions.
To convert decimal degrees to radians, multiply the number of degrees by pi/180 = 0.017453293 radians/degree, where pi is approximately 3.14159265358979:
CREATE FUNCTION Distance(IN latitude1 REAL, IN longitude1 REAL, IN latitude2 REAL, IN longitude2 REAL)RETURNS REALBEGIN DECLARE r REAL; DECLARE lat REAL; DECLARE...