You are on page 1of 3

Functions in SQL

A function is a special type of command. Infact, functions are one-word command that return a
single value. The value of a function can be determined by input parameters, as with a function
that averages a list of database values. But many functions do not use any type of input
parameter, such as the function that returns the current system time, CURRENT_TIME. There
are normally two types of functions. First is Built in, which are provided by any specific tool or
language. Second is user defined, which are defined by the user. The SQL supports a number of
useful functions.. In addition, each database vendor maintains a long list of their own internal
functions that are outside of the scope of the SQL standard.

Categories of Functions:
These categories of functions are specific to SQL Server. Depending on the arguments and the
return value, functions are categorized as under:
• Mathematical (ABS, ROUND, SIN, SQRT)
• String (LOWER, UPPER, SUBSTRING, LEN)
• Date (DATEDIFF, DATEPART, GETDATE ())
• System (USER, DATALENGTH, HOST_NAME)
• Conversion (CAST, CONVERT)
We will now see an example using above-mentioned functions:

SELECT upper (stName), lower (stFName), stAdres, len(convert(char, stAdres)),


FROM student

In this example student name will be displayed in upper case whereas father name will be
displayed in lower case. The third function is of getting the length of student address. It has got
nesting of functions, first address is converted into character and then its length will be
displayed.

Aggregate Functions:
These functions operate on a set of rows and return a single value. If used among many other
expressions in the item list of a SELECT statement, the SELECT must have a GROUP BY
clause. No GROUP BY clause is required if the aggregate function is the only value retrieved by
the SELECT statement. Following are some of the aggregate functions:
Function Usage
AVG(expression) Computes average value of a column by the expression
COUNT(expression) Counts the rows defined by the expression
COUNT(*) Counts all rows in the specified table or view
MIN(expression) Finds the minimum value in a column by the expression
MAX(expression) Finds the maximum value in a column by the expression
SUM(expression) Computes the sum of column values by the expression
SELECT avg(cgpa) as 'Average CGPA', max(cgpa) as 'Maximum CGPA' from student
GROUP BY Clause
The GROUP BY clause can be used in a SELECT statement to collect data across multiple
records and group the results by one or more columns. It is added to SQL because aggregate
functions (like SUM) return the aggregate of all column values every time they are called, and
without the GROUP BY function it is impossible to find the sum for each individual group of
column values.
The syntax for the GROUP BY clause is:
SELECT column1, column2, ... column_n, aggregate_function (expression)
FROM tables
WHERE predicates
GROUP BY column1, column2, ... column_n;
Aggregate function can be a function such as SUM, COUNT, MIN or MAX

Example using the SUM function:


For example, the SUM function can be used to return the name of the department and
the total sales (in the associated department).
SELECT department, SUM (sales) as "Total sales"
FROM order_details
GROUP BY department;
In this example we have listed one column in the SELECT statement that is not
encapsulated in the SUM function, so we have used a GROUP BY clause. The
department field must, therefore, be listed in the GROUP BY section.
Example using the COUNT function
We can also use the COUNT function to return the name of the department and the
number of employees (in the associated department) that make over Rs 25,000 / year.
SELECT department, COUNT (*) as "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department;
HAVING Clause
The HAVING clause is used in combination with the GROUP BY clause. It can be
used in a SELECT statement to filter the records that a GROUP BY returns. At times
we want to limit the output based on the corresponding sum (or any other aggregate
functions). For example, we might want to see only the stores with sales over Rs
1,500. Instead of using the WHERE clause in the SQL statement, though, we need to
use the HAVING clause, which is reserved for aggregate functions. The HAVING
clause is typically placed near the end of the SQL statement, and a SQL statement
with the HAVING clause may or may not include the GROUP BY clause. The syntax
for the HAVING clause is:
SELECT column1, column2, ... column_n, aggregate_function
(expression)
FROM tables
WHERE predicates
GROUP BY column1, column2, ... column_n
HAVING condition1 ... condition_n;
Aggregate function can be a function such as SUM, MIN or MAX.
We will now see few examples of HAVING Clause.
Example using the SUM function
We can use the SUM function to return the name of the department and the total sales (in the
associated department). The HAVING clause will filter the results so that only departments with
sales greater than Rs 1000 will be returned.
SELECT department, SUM (sales) as "Total sales"
FROM order_details
GROUP BY department
HAVING SUM (sales) > 1000;

Example using the COUNT function


For example, you could use the COUNT function to return the name of the department and the
number of employees (in the associated department) that make over $25,000 / year. The
HAVING clause will filter the results so that only departments with at least 25 employees will be
returned.
SELECT department, COUNT (*) as "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department
HAVING COUNT (*) > 10;

Accessing Multiple Tables:


Until now we have been accessing data through one table only. But there can be occasions where
we have to access the data from different tables. So depending upon different requirements data
can be accessed from different tables. Referential integrity constraint plays an important role in
gathering data from multiple tables.
Following are the methods of accessing data from different tables:
Cartesian Product
• Inner join
• Outer Join
• Full outer join
• Semi Join
• Natural JoinWe will now discuss them one by one.
Cartesian product:
A Cartesian join gives a Cartesian product. A Cartesian join is when you join every row of one
table to every row of another table. You can also get one by joining every row of a table to every
row of itself. No specific command is used just Select is used to join two tables. Simply the
names of the tables involved are given and Cartesian product is produced. It produces m x n rows
in the resulting table. We will now see few examples of Cartesian product.
Select * from program, course
Now in this example all the attributes of program and course are selected and the total number of
rows would be number of rows of program x number of rows of course.In Cartesian product
certain columns can be selected, same column name needs to be qualified. Similarly it can be
applied to more than one table, and even can be applied on the same table .For Example

SELECT * from Student, class, program

You might also like