Illegal buildings (IBs) are a dramatic problem in developing countries due to the population explosion, but, at the same time, they represent an unsolved issue in several states usually called advanced (as, for instance, Italy). To protect the environment, and hence, people, land authorities must respond to the challenge of IBs by demolishing them. However, in countries where the phenomenon is extended, it is indispensable to provide those figures with an IT tool that returns to them an order of demolition. Through remote sensing methods, suspicious buildings can be identified with a good approximation, but they are all ex aequo. The research summarized in this paper formalizes a two-steps method to deal with a specific category of IBs, namely, those that are close to rivers. These buildings are of special interest to land authorities because people living or simply working inside them are exposed to the flood hazard that each year claims many victims all over the world. The first step of the method computes the census of the IBs located close to rivers, while the second step computes the ranking of these buildings. The ranking may be used as the IBs demolition order. In the paper, it is also proposed the structure of a Spatial DataBase (briefly, SDB) that is suitable to store the input data necessary to solve the problem, as well as the final ranking. Spatial SQL queries against the SDB implement the proposed two-steps method. A real case study was carried out to make a preliminary validation of the method.