SELECT Statement

The SELECT statement supported by UnityJDBC has the following syntax.

SELECT [ALL | DISTINCT ] <exprList>
      [FROM <tableList>]
      [WHERE <condition>] 
      [GROUP BY <exprList>
      [HAVING <condition>] 
      [ORDER BY <expr> [ASC | DESC],...] 
      [LIMIT <expr> [OFFSET <expr>]]

Some examples using the TPC-H schema follow. The database name for these examples is 'OrderDB'.

Return all nations with their key and name:

SELECT OrderDB.Nation.n_nationkey, OrderDB.Nation.n_name 
FROM   OrderDB.Nation;     

Return the nations and their regions. Only return nations in the region name of 'AMERICA'. Note the use of table aliasing using AS.

SELECT N.n_nationkey, N.n_name, R.r_regionkey, R.r_name
FROM OrderDB.Nation as N, OrderDB.Region as R
WHERE N.n_regionkey = R.r_regionkey AND R.r_name = 'AMERICA';

Calculate the number of countries in each region. Only return a region and its country count if it has more than 4 countries in it. Order by regions with most countries.

SELECT R.r_regionkey, R.r_name, COUNT(N.n_nationkey)
FROM OrderDB.Nation as N, OrderDB.Region as R
WHERE N.n_regionkey = R.r_regionkey
GROUP BY R.r_regionkey, R.r_name
HAVING COUNT(N.n_nationkey) > 4
ORDER BY COUNT(N.n_nationkey) DESC;