Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Expt No 3: Grouping Data, Aggregate Functions, Oracle Functions (Mathematical, Character Functions)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

EXPT NO 3 Roll No

Name

Title :Grouping data, aggregate functions, Oracle Performance Date


functions (mathematical, character functions).
Checking Date &
Grade

Theory :
3.1 Aggregate Functions
Aggregate functions are used to compute against a "returned column of numeric data" from your SELECT
statement. They basically summarize the results of a particular column of selected data.

MIN() returns the smallest value in a given column

MAX() returns the largest value in a given column

SUM() returns the sum of the numeric values in a given column

AVG() returns the average value of a given column

COUNT() returns the total number of values in a given column

COUNT(*) returns the number of rows in a table

3.1.1 The MIN() Function: The MIN() function returns the smallest value of the selected column.
SELECT MIN(column_name) FROM table_name;
3.1.2 The MAX() Function: The MAX() function returns the largest value of the selected column.
SELECT MAX(column_name) FROM table_name;
3.1.3The SUM() Function: The SUM() function returns the total sum of a numeric column.
SELECT SUM(column_name) FROM table_name;
3.1.4 The AVG() Function: The AVG() function returns the average value of a numeric column.
SELECT AVG(column_name) FROM table_name;
3.1.5 The COUNT() Function: The COUNT() function returns the number of rows that matches a
specified criteria.
The COUNT(column_name) function returns the number of values (NULL values will not be
counted) of the specified column:
SELECT COUNT(column_name) FROM table_name;

The COUNT(*) function returns the number of records in a table:


SELECT COUNT(*) FROM table_name;
The COUNT(DISTINCT column_name) function returns the number of distinct values of the
specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name;
3.2 GROUP BY CLAUSE
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange
identical data into groups.
The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the
ORDER BY clause.
Syntax:
The basic syntax of GROUP BY clause is given below. The GROUP BY clause must follow the
conditions in the WHERE clause and must precede the ORDER BY clause if one is used.
SELECT column1, column2,columnN
FROM table_names
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2;

3.3 Mathematical Functions

Function Description Example Result


ABS Calculate the absolute value of a number ABS(-10) 10

CBRT Calculate the cube root of a number CBRT(8) 2

CEIL Round a number up to the nearest integer, CEIL(-12.8) -12


which is greater than or equal to number

DIV Return the integer quotient of two numeric DIV(8,3) 2


values

EXP Return the exponential value in scientific EXP(1) 2.718281828


notation of a number

LN Return the natural logarithm of a numeric value LN(3) 1.098612289

LOG Return the base 10 logarithm of a numeric LOG(1000) 3


value

MOD Divide the first parameter by the second one MOD(10,4) 1


and return the remainder

SQRT Return the square root of a numeric value SQRT(3.0) 1.732050808

POWER Raise a numeric value to the power of a POWER(5, 3) 125


second numeric value

PI Return the value of PI PI() 3.141592654

SCALE Return the number of decimal digits in the SCALE(1.234) 3


fractional part

3.4Trigonometric Functions

Function Description
sin() Returns sine.

cos() Returns cosine.


tan() Returns tangent.

asin() Returns inverse sine.

3.5 String Functions:

Function Description Example Result


REVERSE Return reversed string. REVERSE(‘ABC’) ‘CBA’

CHR Convert an ASCII code to a character or a CHR(65) ‘A’


Unicode code point to a UTF8 character

UPPER Convert a string to uppercaseUPPER(‘hI UPPER(‘hI tHERE’) ‘HI THERE’


tHERE’)‘HI THERE’

TRIM Remove the longest string that contains TRIM(‘ ABC ‘) ‘ABC’
specified characters from the left, right or
both of the input string

LENGTH Return the number of characters in a string LENGTH(‘ABC’) 3

3.6 String Matching

The PostgreSQL LIKE operator is used to match text values against a pattern using wildcards. If the
search expression can be matched to the pattern expression, the LIKE operator will return true, which is
1.

There are two wildcards used in conjunction with the LIKE operator −

● The percent sign (%)


● The underscore (_)

The percent sign represents zero, one, or multiple numbers or characters. The underscore represents a
single number or character. These symbols can be used in combinations.

If either of these two signs is not used in conjunction with the LIKE clause, then the LIKE acts like the
equals operator.

Syntax

The basic syntax of % and _ is as follows −

SELECT FROM table_name WHERE column LIKE 'XXXX%';

3.7 The HAVING Clause

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

aggregate functions.

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;


3.8 The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in

a descending order, you can use the DESC keyword.

SELECT column_name, column_name

FROM table_name

ORDER BY column_name ASC|DESC, column_name ASC|DESC;

You might also like