Page-3(SQL Functions)

SQL has many built-in functions for performing calculations on data.

SQL Aggregate Functions

SQL aggregate functions return a single value, calculated from values in a column.

Useful aggregate functions:

  • AVG() - Returns the average value

  • COUNT() - Returns the number of rows

  • FIRST() - Returns the first value

  • LAST() - Returns the last value

  • MAX() - Returns the largest value

  • MIN() - Returns the smallest value

  • SUM() - Returns the sum

SQL Scalar functions

SQL scalar functions return a single value, based on the input value.

Useful scalar functions:

  • UCASE() - Converts a field to upper case

  • LCASE() - Converts a field to lower case

  • MID() - Extract characters from a text field

  • LEN() - Returns the length of a text field

  • ROUND() - Rounds a numeric field to the number of decimals specified

  • NOW() - Returns the current system date and time

  • FORMAT() - Formats how a field is to be displayed

Tip: The aggregate functions and the scalar functions will be explained in details in the next chapters.

The FIRST() Function

The FIRST() function returns the first value of the selected column. --> means first row

SQL FIRST() Syntax

SELECT FIRST(column_name) FROM table_name

The LAST() Function

The LAST() function returns the last value of the selected column.--> means last row

SQL LAST() Syntax

SELECT LAST(column_name) FROM table_name

Note: these two functions first() and last() is mainly useful when need to return 1st or last row from a table

Example: to get the highest paying and lowest paying employee name from employee table.

select first(Emp.Name) as HighlyPaid, last(Emp.Name) as LowestPaid from Employee as Emp order by salary DESC

The GROUP BY Statement

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

SQL GROUP BY Syntax

SELECT column_name, aggregate_function(column_name)

FROM table_name

WHERE column_name operator value

GROUP BY column_name

SQL GROUP BY Example

We have the following "Orders" table:

Now we want to find the total sum (total order) of each customer.

We will have to use the GROUP BY statement to group the customers.

We use the following SQL statement:

SELECT Customer,SUM(OrderPrice) FROM Orders

GROUP BY Customer

The result-set will look like this:

Nice! Isn't it? :)

Let's see what happens if we omit the GROUP BY statement:

SELECT Customer,SUM(OrderPrice) FROM Orders

The result-set will look like this:

The result-set above is not what we wanted.

Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (that is the total sum of the "OrderPrice" column), while "Customer" returns 6 values (one value for each row in the "Orders" table). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.

GROUP BY More Than One Column

We can also use the GROUP BY statement on more than one column, like this:

SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders

GROUP BY Customer,OrderDate

The HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

SQL HAVING Syntax

SELECT column_name, aggregate_function(column_name)

FROM table_name

WHERE column_name operator value

GROUP BY column_name

HAVING aggregate_function(column_name) operator value

Example:

SELECT Customer,SUM(OrderPrice) FROM Orders

GROUP BY Customer

HAVING SUM(OrderPrice)<2000

More..

SQL mid()

SQL len()

SQL round()

SQL now()

SQL format()

Next..