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
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.
The SELECT statement retrieves data from a database and returns it to you in the form of query results.
As a reminder, the exact format of the query results will vary from one SQL product to another.
Here are several more sample queries that retrieve information about sales offices:
List the sales offices with their targets and actual sales.
SELECT CITY, TARGET, SALES
For simple queries, the English language request and the SQL SELECT statement are
very similar. When the requests become more complex, more features of the SELECT
statement must be used to specify the query precisely.