Oracle PL/SQL Tutorial - PL/SQL Select






The following code is a query for selecting all the author names from the authors table.

There's no WHERE clause to constrain which rows you will see.

Example

SELECT name FROM authors ORDER BY name; 

The syntax of the SELECT statement is as follows:


SELECT <column_name_1>, 
<column_name_2>, 
<column_name_N> 
FROM <table_name> 
[ORDER BY <order_by_column_name_N>] 

<column_name> is one of the columns in the table listed <table_name> is the table to query <order_by_column_name> is one or more columns by which to sort the results.

The following code adds a WHERE clause to constrain the output to only those authors born before the year 2000.

SELECT name 
FROM authors 
WHERE birth_date < to_date('20000101', 'YYYYMMDD') 
ORDER BY name; 




Joins in a Where Clause

The following code shows the SQL SELECT statement from that view with an added ORDER BY clause.

The code is joining the two tables using the WHERE clause.


SELECT a.id, a.name, p.title, p.publish_date 
FROM authors a, 
author_books p 
WHERE a.id = p.author_id 
ORDER BY a.name, 
p.publish_date, 
p.title;




Joins in a From Clause

The following code shows the newer join syntax.

The newer join syntax is part of the ANSI standard.

It would be easier to move to another ANSI standard compliant SQL database.


SELECT a.id,a.name, p.title, p.publish_date 
FROM authors a JOIN 
author_books p 
ON a.id = p.author_id
ORDER BY a.name;