Accessing Data : Accessing database data using a textual query : Using a SQL query to retrieve data from a JDBC data source : Writing a basic SQL query
 
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