SQL commands follow a simple structure, similar in concept to functions in programming languages. The keywords are like function names, the parameters are similar to values passed to the function, and there is always a return value – which can be null. As explained above, once the parameters are understood carefully, the statements can be made more complex to suit the requirement at hand.
Nesting statements is a process by which a complex SQL statement is created to perform more than one database operation in one statement. This is an useful programming trick, especially since it eliminates the need to store the results of one statement before passing them again as parameters to the next statement.
For example, if a record needed to be copied from one database to another, the following nested statement would accomplish the job:
INSERT INTO students
SELECT f_name, s_name, age FROM applicantdb WHERE f_name = ‘Adam’ AND s_name = ’Smith’
As the SELECT statement returns a database row, the VALUES keyword is omitted entirely from the statement. There are a number of qualifiers that can be used within statements to refine the results. ‘WHERE’ is a keyword indicating there is a condition that the results need to meet before they are accepted as valid results.
Insert statements can also be nested within other SQL commands, however the eventualities for them to be nested within other statements is few and far between.