SQL Aggregate Functions
SQL Aggregate Functions
SQL Aggregate Functions
FUNCTIONS
WHAT IS AGGREGATE
FUNCTION IN SQL?
This function can produced a single value for an entire group or table.
They operate on sets of rows and return results based on groups of rows.
LIST OF AGGREGATE
FUNCTIONS
1. COUNT
2.SUM
3. AVERAGE
4. MAX
5. MIN
1. COUNT () function:
The SQL COUNT function returns the number of rows in a table satisfying the
criteria specified in the WHERE clause. It sets on the number of rows or non NULL
column values.
SQL Syntax:
COUNT(*) , COUNT( [ALL|DISTINCT] expression )
WHAT IS SUBQUERY IN SQL?
The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement
or inside another subquery.
A subquery is usually added within the WHERE Clause of another SQL SELECT
statement.
You can use the comparison operators, such as >, <, or =. The comparison operator can
also be a multiple-row operator, such as IN, ANY, or ALL.
WHAT IS SUBQUERY IN SQL?
(CONT’D)
A subquery is also called an inner query or inner select, while the statement
containing a subquery is also called an outer query or outer select.
The inner query executes first before its parent query so that the results of an inner
query can be passed to the outer query
You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to
perform the following tasks:
Compare an expression to the result of the query.
Determine if an expression is included in the results of the query.
Check whether the query selects any rows.
WHAT IS SUBQUERY IN SQL?
(CONT’D)
The subquery (inner query) executes once before the main query (outer query)
executes.
The main query (outer query) use the subquery result.
Syntax:
SUBQUERIES: GUIDELINES
There are some guidelines to consider when using subqueries :
A subquery must be enclosed in parentheses.
A subquery must be placed on the right side of the comparison operator.
Subqueries cannot manipulate their results internally, therefore ORDER BY clause
cannot be added into a subquery. You can use an ORDER BY clause in the main
SELECT statement (outer query) which will be the last clause.
Use single-row operators with single-row subqueries.
If a subquery (inner query) returns a null value to the outer query, the outer query
will not return any rows when using certain comparison operators in a WHERE
clause.
TYPE OF SUBQUERIES