Many programs need to access data in a relational database. This is usually done by means of queries written in SQL. Although the language SQL is declarative, certain runtime errors are possible. Since the occurrence of these errors depend on the data, they are not easily found during testing. The question whether a query is safe can be reduced to a consistency check. It is well known that consistency is in general undecidable, and that this applies also to SQL queries. However, in this paper, we propose a consistency check that can handle a surprisingly large subset of SQL (it uses Skolemization with sorted Skolem functions, and a few other tricks). This consistency check is also the basis for generating other semantic warnings. Furthermore, it can be used to generate test data for SQL queries. Topics: Model Construction, Information Systems, Static Analysis, Software Quality.