How to quickly validate a select query in sql

SQL (Structured Query Language) is a mechanism that is used to manage as well as store and retrieve information from a RDBMS (Relational Data Base Management Service).

One way to retrieve information from the database is to use the SELECT query.

To be able to execute a select query inside a database server, it needs to be correct both syntactically and semantically.

Syntax and Semantics

A language is a set of valid sentences. What makes a sentence valid? You can break validity down into two things: syntax and semantics. The term syntax refers to grammatical structure whereas the term semantics refers to the meaning of the vocabulary symbols arranged with that structure. Grammatical (syntactically valid) does not imply sensible (semantically valid), however. For example, the grammatical sentence "cows flow supremely" is grammatically ok (subject verb adverb) in English, but makes no sense. Similarly, in a programming language, your grammar (syntax rules) may allow ID EQUALS ID but the language may only allow the sentence if the text matched for ID does not represent a type name (a semantic rule).

Some applications give their user a feature where they can store a SQL query (Custom Report Module) inside the application database and can retrieve the required information by executing the query on demand. To prevent run time surprises, when the user actually execute the report and get faced with any error, it is a good idea to validate the query before storing it in the database.

But executing the query before storing it in the database can be a heavy operation if the query is not optimized or the data set is large on which the query operates. So how to quickly validate a query before storing it in the database? 

Let us assume that we need to validate the following query.

SELECT emp.fname, emp.lname, emp.salary, dep.deptname
FROM employee emp
join department dep on dep.deptid = emp.deptid
where dep.deptid = 10

How to quickly validate the query in MySQL


# Use LIMIT 0 (ZERO) at the end of your query
# This is valid for queries having GROUP, HAVING and ORDER clause.

SELECT emp.fname, emp.lname, emp.salary, dep.deptname
FROM employee emp
join department dep on dep.deptid = emp.deptid
where dep.deptid = 10
LIMIT 0

How to quickly validate the query in SQL SERVER

# Use TOP 0 (ZERO)
# This is valid for queries having GROUP, HAVING and ORDER clause. 

SELECT TOP 0 emp.fname, emp.lname, emp.salary, dep.deptname
FROM employee emp
join department dep on dep.deptid = emp.deptid
where dep.deptid = 10

Comments

Back To Top

Popular posts from this blog

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables

How to Drop or Remove or Decommission a Database in Oracle