Examples

In the previous sections of this chapter, we examined the fundamental components of SQL statement processing found in most database programs, but didn’t include any programs in their entirety. In the following section, we combine the necessary steps into a small program that executes a simple SELECT statement and prints out the results. The same example is provided for both of the APIs covered in this chapter.

The examples execute a SELECT statement and print out the results after establishing connectivity with the database. The SELECT statement is as follows:

SELECT a.au_lname, a.au_fname, SUM(t.ytd_sales)
FROM authors a, titleauthor, titles t 
WHERE titleauthor.au_id = a.au_id and 
      titleauthor.title_id = t.title_id
GROUP BY a.au_lname, a.au_fname
ORDER BY 3 DESC

The examples execute the statement and print out the three-column result set returned by the database server.

Get SQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.