RelationalDBDesign 




Advanced SQL  «Prev  Next»
Lesson 1

Advanced uses of the SELECT statement in SQL

After you have a good understanding of what SQL can do, it is time to move on to more sophisticated queries. It is important to understand what your database engine is capable of, and what your requirements are for pulling information from your system.
This module will introduce you to some of the more advanced queries used for extracting information from tables. These new clauses include
  1. grouping and
  2. filtering.
You will see how to make sure you skip duplicate information in the database, making the information more meaningful and easier to digest.

SQL GROUP Functions

Group functions are built-in SQL functions that operate on groups of rows and return one value for the entire group.
These functions are:
 COUNT, MAX, MIN, AVG, SUM, DISTINCT
 

SQL COUNT (): This function returns the number of rows in the table that satisfies the condition specified in the WHERE condition.
If the WHERE condition is not specified, then the query returns the total number of rows in the table.
For Example: If you want the number of drinks in a particular category, the query would be:


Example of SELECT COUNT (*)

SELECT COUNT (*) FROM fridge
WHERE pop = 'Pepsi'; >
One of the advanced uses of the Select statement is in Group functions.
Group functions are functions applied to a group of rows.

Examples of Group Functions

COUNT(*) - Returns the number of rows in the group.
MIN(exp) - Returns the minimum value of the expression evaluated on each row of the group.
MAX(exp) - Returns the maximum value of the expression evaluated on each row of the group.
AVG(exp) - Returns the average value of the expression evaluated on each row of the group.