0% found this document useful (0 votes)
10 views

SELECT03

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

SELECT03

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SELECT statement

of SQL language
grouping and aggregate
functions
Aggregate functions

Selected aggregate functions in SQL:


SUM sum
AVG average
COUNT counting number of records
FIRST value in first record of the group
LAST value in last record of the group
MIN minimum value
MAX maximum value
STDEV standard deviation
VAR variation

2
Groupping – example

SELECT c.client_id, client_name, SUM(quantity*price) AS value


FROM orders o INNER JOIN clients c ON o.client_id=c.client_id
GROUP BY c.client_id, client_name;

SELECT c.client_id, client_name, client_phoneNo,


SUM(quantity*price) AS value
FROM orders o INNER JOIN clients c ON o.client_id=c.client_id
GROUP BY c.client_id, client_name;

3
Groupping – HAVING clause

HAVING clause is used for selecting groups. It plays the same role for groups as WHERE
does for individual records.

In this clause a logical condition has to be defined. This condition applies to every group
separately, and if it returns TRUE, the group is appended to the result table. If it returns
FALSE, the group is rejected.

4
Groupping – HAVING clause – example
A query selecting information on how many books in every price (but greater than 10) there
are in a database but only if this quantity of books is greater than 1.

First, the query selects books whose prices are greater than 10…
books
book book …
_id _price
1 9 …
2 23 …
3 25 …
4 23 …
5 6 …
6 26 …
7 23 …
8 8 …
9 26 …
10 21 …

FROM books
WHERE book_price > 10

5
Groupping – HAVING clause – example, cont.
A query selecting information on how many books in every price (but greater than 10) there
are in a database but only if this quantity of books is greater than 1.

…then it groups selected books…


books
book book …
_id _price
2 23 …
4 23 …
7 23 …
3 25 …
6 26 …
9 26 …
10 21 …

FROM books
WHERE book_price > 10
GROUP BY book_price

6
Groupping – HAVING clause – example, cont.
A query selecting information on how many books in every price (but greater than 10) there
are in a database but only if this quantity of books is greater than 1.

…and, finally, it chooses those groups which quantity is greater than 1 and selects columns.
books
book book … book book
_id _price _price _number
2 23 … 23 3
4 23 … 26 2
7 23 …
3 25 …
6 26 …
9 26 …
10 21 …

SELECT book_price, COUNT(*) AS book_number


FROM books
WHERE book_price > 10
GROUP BY book_price
HAVING COUNT(*) >= 2;

7
Groupping – the difference between COUNT(*) and COUNT(field)

COUNT aggregate function may be used twofoldly:


COUNT(*) returns number of records in every group
COUNT(field) returns number of values not equal to NULL
in the field for every group separately
see an example…
clients
client
_id
id
_card
surname … entry
_date
SELECT COUNT(*)
FROM clients; result: 9
1 Smith … 2012-09-21
2 MB25 Jones … 2012-09-21
3 Wilson … 2012-09-25
4 AV10 Smith … 2012-09-29
5 DF45 Brown … 2012-10-11
6 Smith … 2012-10-23 SELECT COUNT(id_card)
7 KW23 Green … 2012-11-12 FROM clients; result: 5
8 Toms … 2012-11-20
9 KU71 Davies … 2012-12-12

You might also like