You are on page 1of 17

SQL OPERATORS AND FUNCTIONS

BY
AHAMED HASHIR.M
SQL FUNCTIONS
 The SQL Server built-in functions are as follows:

 Rowset Functions

 Aggregate Functions

 Ranking Functions

 Scalar Functions
SCALAR FUNCTIONS
 Scalar functions operate on a single value.

 Scalar functions return a single value.

 Scalar functions can be used wherever an expression is valid.


FUNCTION CATEGORY DESCRIPTION
Configuration functions Return information about the current
configuration.
Cursor functions Return information about cursors.
Date and Time Data types and functions Perform operations on a date and time
input values and return string, numeric, or
date and time values.
Mathematical functions Perform calculations based on input
values provided as parameters to the
functions, and return numeric values.
Metadata functions Return information about the database
and database objects.
Security functions Return information about users and roles.
String functions Perform operations on a string (char or
varchar) input value and return a string or
numeric value.
FUNCTION CATEGORY DESCRIPTION
System functions Perform operations and return
information about values, objects, and
settings in an instance of SQL Server.
System Statistical functions Return statistical information about the
system.

Text and Image functions Perform operations on text or image input


values or columns, and return information
about the value.
AGGREGATE FUNCTIONS
 Aggregate functions perform a calculation on a set of values and return a
single value.

 Aggregate functions are frequently used with the GROUP BY clause of the
SELECT statement.

 Aggregate functions can be used as expressions only in the following:


I. The select list of a SELECT statement.
II. A COMPUTE or COMPUTE BY clause.
III. A HAVING clause.
FUNCTION DESCRIPTION SYNTAX

AVG The AVG Function returns the AVG(column)


average value for the column.

COUNT The COUNT Function Returns COUNT(column)


the number of rows (without
a NULL value) of a column

Returns the number of


selected rows COUNT(*)

Returns the number of rows


where the specified column COUNT(distinct column name)
has a distinct, non-NULL value.
SUM The SUM Function returns the SUM(column)
sum of all values in the
specified column.
FUNCTION DESCRIPTION SYNTAX

MAX Returns the highest value MAX(column)


of a column

MIN The MIN Function returns MIN(column)


the data item with the
lowest value for a
column.

FIRST Returns the value of the FIRST(column)


first record in a specified
field (not supported in
SQLServer2K)
LAST Returns the value of the LAST(column)
last record in a specified
field (not supported in
SQLServer2K)
ROWSET FUNCTIONS
 The following row set functions return an object that can be used in place of a
table reference in a Transact-SQL statement.

 CONTAINSTABLE

 OPENQUERY

 FREETEXTTABLE

 OPENROWSET

 OPENDATASOURCE

 OPENXML
RANKING FUNCTIONS
 Ranking functions return a ranking value for each row in a partition.
Depending on the function that is used, some rows might receive the same
value as other rows.
 Ranking functions are nondeterministic.
 Ranking functions are as follows:

 RANK

 NTILE

 DENSE RANK

 ROW_NUMBER
SQL OPERATORS
 The SQL operators can be classified as

 COMMON OPERATORS

 LOGICAL OPERATORS

 MATHEMATICAL OPERATORS
COMMON OPERATORS
OPERATOR DESCRIPTION SYNTAX

; - semicolon Semicolon is used when ;


two SQL commands must
be separated
* - "star" Is an alias for all fields of *
the selected table.
ALL ALL is used to select all ALL [ ( SELECT-
records of an SELECT command ) ]
statement.
ANY ANY is used to apply a ANY[ ( SELECT-
logical construct to all command ) ]
records at the right side of
the operator.
BETWEEN Checks when an field is BETWEEN value1 AND
between two values value2
OPERATOR DESCRIPTION SYNTAX

EXISTS Checks the existence of an EXISTS(SELECT_comma


result of an sub-SELECT nd )

IN The IN operator allows you SELECT columnname(s)


to specify multiple values FROM tablename WHERE
in a WHERE clause. columnname IN
(value1,value2,...)
LIKE The LIKE operator is used SELECT columnname(s)
to search for a specified FROM tablename WHERE
pattern in a column. columnname LIKE pattern

UNION The UNION operator is SELECT columnname(s)


used to combine the result- FROM table_name1
set of two or more UNION SELECT
SELECT statements columnname(s) FROM
table_name2
LOGICAL OPERATORS
OPERATOR DESCRIPTION SYNTAX

AND TRUE when the values at Logical construct1 AND


both sides of the operator Logical construct2
are true.
NOT Logical negation NOT Logical construct

OR TRUE when any one value Logical construct1 OR


at both sides of the operator Logical construct2
is true.

= Is TRUE when the values Formula1 = Formula2


at both sides of the operator
are equal.
<> Is TRUE when the values Formula1 <> Formula2
at both sides of the operator
are inequal.
OPERATOR DESCRIPTION SYNTAX

< Is TRUE when the value at Formula1 < Formula2


the left side of the operator
is smaller than the value at
the right side.
> Is TRUE when the value at Formula1>Formula2
the left side of the operator
is greater than the value at
the right side.
<= Is TRUE when the value at Formula1 <= Formula2
the left side of the operator
is smaller than or equal to
the value at the right side.

>= Is TRUE when the value at Formula1 >= Formula2


the left side of the operator
is greater than or equal to
the value at the right side.
MATHEMATICAL OPERATORS
OPERATOR DESCRIPTION SYNTAX

+ Addition Value1+Value2

- Subtraction Value1-Value2

* Multiplication Value1*Value2

/ Division Value1/Value2
OPERATOR PRECEDENCE
OPERATOR PRECEDENCE
+ - (Unary) 0
*/ 1
+- 2
= <> 3
< <= 3
> >= 3
[NOT] BETWEEN 3
IS [NOT] NULL 3
[NOT] IN 3
NOT 4
AND 5
OR 6

You might also like