Using a SQL query to retrieve data from a JDBC data source
Typically, a JDBC data set retrieves data using a SQL query. SQL is a standard query language for requesting data from a database. This section discusses how to write a basic SQL query and how to combine data from multiple tables. In many cases, a basic knowledge of SQL is sufficient to retrieve the data a report requires.
Writing a basic SQL query
A SQL query consists of one or more statements. The first statement of a SQL query is the SELECT statement that specifies which columns to retrieve from the database. The SELECT statement contains two required clauses: SELECT and FROM. The SELECT clause lists the columns to retrieve. The FROM clause specifies the table from which to retrieve the selected columns of data.
The following is an example of a SQL statement that selects the firstname and lastname columns from a table called customers:
SELECT customers.firstname, customers.lastname
FROM customers
A SQL SELECT query can also include other clauses that limit what data a query returns. Use the WHERE clause to specify criteria that results must meet and use ORDER BY to sort results. The following is an example of the same SQL statement, with the addition of the WHERE and ORDER BY clauses:
SELECT customers.firstname, customers.lastname
FROM customers
WHERE customers.country = 'Japan'
ORDER BY customers.lastname, customers.firstname
Combining data from multiple tables
Typically, you have to select data from two or more tables to retrieve complete data for your report. This operation is called a join. You join tables in a database through a common column called a key.
For example, suppose you want to retrieve the orders for every customer. The database, however, stores customer information in a Customers table, and order information in an Orders table, as shown in Figure 5‑7. Both tables contain a column called CustomerID. You can join the customers and the orders table using the CustomerID column.
Figure 5‑7 Database stores customer and order information in two tables
To retrieve order information for every customer, use the following SELECT statement:
SELECT Customers.CustomerName, Orders.Amount
FROM Customers, Orders
WHERE Customers.CustomerID = Orders.CustomerID
The WHERE clause in this example specifies that the query returns rows where the CustomerID in both tables match. Figure 5‑8 shows the results that the SELECT statement returns.
Alternatively, use the JOIN keyword to select data from the two tables. The rest of this section describes the different types of joins you can use, and the results that each join returns. The following SELECT statement uses INNER JOIN and returns the same results shown in Figure 5‑8:
SELECT Customers.CustomerName, Orders.Amount
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
Figure 5‑8 Results returned by SELECT statement
The INNER JOIN clause returns all rows from both tables where the two CustomerID fields match. If there are rows in the Customers table that do not match rows in the Orders table, those rows are not listed. In the example, Patrick Mason is not listed in the result set because this customer does not have a matching order.
To obtain all the customer names, whether or not a customer has an order, use the LEFT JOIN clause, as shown in the following example:
SELECT Customers.CustomerName, Orders.Amount
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
LEFT JOIN returns all rows from the first (left) table, even if there are no matches in the second (right) table. Figure 5‑9 shows the results of the SELECT statement that uses the LEFT JOIN clause. Here, Patrick Mason is listed in the result set even though he does not have an order, because the record is in the first table.
Figure 5‑9 Results of a left join
Conversely, to retrieve all rows from the second table (the Orders table in our example), even if there are no matches in the first table (the Customers table), use the RIGHT JOIN clause, as shown in the following example:
SELECT Customers.CustomerName, Orders.Amount
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
In our example, all the rows in the second table match rows in the first table, so the result is the same as in Figure 5‑8. If, however, the Orders table had contained rows that did not have matches in the Customers table, those rows would also have been returned.
To retrieve all customer names and orders from both tables, even if there are no matching values, you can use the FULL OUTER JOIN clause, as shown in the following example:
SELECT Customers.CustomerName, Orders.Amount
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
In our example, the result is the same as in Figure 5‑9. All the customer names and all the order amounts are returned. Some databases do not support FULL OUTER JOIN. In most cases, you can get the same results using the UNION operator.
Note that in all the examples, the SELECT statements specify the columns being joined: Customers.CustomerID and Orders.CustomerID. You must specify the columns to join. If you do not, the result is what is commonly referred to as a cartesian join. In a cartesian join, all rows in the first table are joined with all rows in the second table. If the first table has 1000 rows and the second table has 10,000 rows, the cartesian join returns 10,000,000 rows, a result you rarely want.
 
The inner, left, and right joins are the most common types of joins. For more information about these joins and others that your database supports, see the database manufacturer’s documentation.
 
How to create a SQL query to retrieve data from a JDBC data source
This procedure assumes you have already created the JDBC data source that this data set uses.
1 In Data Explorer, right-click Data Sets, then choose New Data Set.
2 In New Data Set, specify the following information:
1 In Data Source Selection, under JDBC Data Source, select the data source to use.
2 In Data Set Type, select SQL Select Query.
3 In Data Set Name, type a name for the data set.
4 Choose Next.
Query displays information to help you create a SQL query. Available Items lists the items in the data source.
3 To see the tables in a database, expand the database, as shown in Figure 5‑10.
Figure 2-19 Viewing a schemaFigure 2-19 Viewing a schema
Figure 5‑10 Viewing a schema
You can use the following filter options to display specific schemas or objects. Choose Apply Filter after specifying the filters.
*In Schema, select an item to display only objects from that schema.
*In Type, select the type of objects—tables, views, or stored procedures—to display.
*In Filter, type one or more letters to display only the objects that have names beginning with that letter or letters. You can also use SQL filter characters for the database that you are using. For example, on some databases, an underscore (_) matches any single character, and the percent sign (%) matches any sequence of characters.
4 To display the columns in a table or view, click the plus sign (+) next to a table or view name.
5 In the text area, type a SQL statement that indicates what data to retrieve from the JDBC data source. Alternatively, drag tables, views, and columns from Available Items to the text area to insert their names in the SQL statement at the insertion point, as shown in Figure 5‑11.
For some databases, if a table or column name contains spaces or SQL reserved words, you must enclose the name in quotation marks (" "). If you drag and drop tables and columns, and those items need to be enclosed in double quotation marks, select the Quote all identifiers option. When this option is selected, the data set editor inserts quotation marks around a table or column name when you drop it in the text area.
Figure 2-20 Adding a table to a SQL queryFigure 2-20 Adding a table to a SQL query
Figure 5‑11 Adding a table to a SQL query
6 Choose Finish. Edit Data Set displays the columns specified in the query, and provides options for editing the data set.