The SELECT statement finds and retrieves rows, columns, and derived values from one or more tables of a database. The SELECT statement is flexible, with many options, and accepts column specifications, search conditions, ordering instructions, and other parameters. The powerful and complex SELECT statement is a core feature of SQL and a thorough description of it well exceeds the scope of this document. This section outlines basic SELECT syntax and clauses which suffice for most simple queries.
for each <variable> in
SELECT [DISTINCT | ALL] <column1>, <column2>,...
FROM <table1>, <table2>, ...
[WHERE <condition>]
[GROUP BY <grouping-column1>, <grouping-column2>, ...]
[HAVING <group-selection-condition1>]
[ORDER BY <ordering-col1> [ASC | DESC],
<ordering-col2> [ASC | DESC], ...]
do
// ...
end
The columns to be retrieved are delimited by commas or, alternatively, an asterisk (*) may be used to retrieve all columns from every table queried. It is recommended that you specify each column you need rather than retrieving all of them, as this will improve performance, substantially if the table is large and contains many columns you do not need.
SELECT clientId, fn AS firstName FROM clientsThis selects clientId and fn from the database, but it will return the columns as clientId and firstName. In this way, you will be able to access the column using firstName in your code rather than fn, so it will be easier to read.
A column can also be an expression or an aggregate function. Aggregate functions combine values from every row into a single value, such as a sum or an average, and are discussed below.
You may use the ORDER BY clause to sort the results of the query according to a given value. Ordering may be ascending (ASC), or descending (DESC). Ascending order is the default and need not be specified. You can order by one or more columns, delimited by commas. Sorting is first done on the first ORDER BY column, and subsequent ORDER BY columns are used when the previous column contains equal values.
for each e in
SELECT *
FROM employees
WHERE salary > 25000
ORDER BY lname
do
display "employee name: " + e.lname + ", " + e.fname
end
for each row in
SELECT MAX(salary)
FROM employees
do
salary = row.1
end
for each e in
SELECT depnumber, COUNT(*), AVG(salary)
FROM employees
WHERE depnumber != 3 and depnumber !=4
GROUP BY depnumber
HAVING COUNT(*) > 5
ORDER BY depnumber
do
// ...
end