Database Reference
In-Depth Information
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
In the following example, we are counting items on
ID
from
table1
and then
grouping them by
ID
from
table2
:
SELECT table1.Name,COUNT(table2.ID) AS
TotalOrders FROM table2
LEFT JOIN table1
ON table2.ID=table1.ID
GROUP BY Name;
•
ORDER BY
: The
ORDER BY
clause is used with SQL statements to sort the
result data by one or more columns. The sorting is done in
ascending
or-
der by default, and to change sort order you can use
DESC
at the end of the
statement. The syntax of the
ORDER BY
clause is shown in the following ex-
ample:
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Example:
SELECT * FROM citizens ORDER BY state;
SELECT * FROM items ORDER BY price DESC;
•
HAVING
: The
HAVING
clause is also used with aggregate functions such as
COUNT()
,
AVG()
,
SUM()
,
MIN()
, or
MAX()
, when a filter operation is con-
ducted on a
SELECT
query. This clause also works with
GROUP BY
in some
cases. The syntax of a
HAVING
clause is as follows:
SELECT column_name,
aggregate_function(column_name) FROM
table_name
WHERE column_name operator value GROUP