CS2072 Database Engineering Laboratory
&
CS2082 Database Management Systems
Laboratory
(LAB-4)
Prof. Sambit Bakshi
Computer Science and Engineering,
National Institute of Technology, Rourkela.
bakshisambit@nitrkl.ac.in
SQL Operators
Arithmetic Operators
Operator Meaning Operates on
+ (ADD) Addition Numeric value
- (Subtract) Subtraction Numeric value
* (Multiply) Multiplication Numeric value
/ (Divide) Division Numeric value
% (Modulo) Returns a
remainder .
Numeric value
Comparison Operator
Operator Description Example
= Equal To SELECT *FROM Student WHERE Total_Marks =450;
> Greater Than SELECT *FROM Student WHERE Total_Marks >400;
< Less Than SELECT *FROM Student WHERE Total_Marks <450;
>= Greater than equal to SELECT *FROM Student WHERE Total_Marks >=450;
<= Less than equal to SELECT *FROM Student WHERE Total_Marks <=450;
<> Not equal to SELECT *FROM Student WHERE Total_Marks <>450;
Compound Operator
Operator Meaning Example
+= Add equals UPDATE Student SET English_Result+=10;
-= Subtract equals UPDATE Student SET English_Result-=10;
*= Multiply equals UPDATE Student SET English_Result*=2;
/= Divide equals UPDATE Student SET English_Result/=2;
%= Modulo equals UPDATE Student SET English_Result%=2;
&= Bitwise AND equals UPDATE Student SET English_Result &=1;
^= Bitwise Exclusive OR
equals
UPDATE Student SET English_Result ^=1;
|= Bitwise OR equals UPDATE Student SET English_Result |=1;
Logical Operator
Operator Example
AND SELECT *FROM Student
WHERE Total_Marks>430 AND Age=19;
OR SELECT *FROM Student
WHERE Total_Marks>430 OR Age=19;
NOT SELECT *FROM Student
WHERE NOT Total_Marks>430;
Mathematical Functions
Function Name Syntax Example
ABS() ABS(expression ) ABS(Entrance_Result);
CEILING() CEILING(expression) CEILING (Entrance_Result);
FLOOR() FLOOR(expression) FLOOR (Entrance_Result);
EXP() EXP(expression) EXP (Entrance_Result) ;
POWER POWER(expression, y) POWER(Entrance_Result,2);
ROUND ROUND( Expression, length ) ROUND(Entrance_Result,1);
SIGN SIGN(expression) SIGN(Entrance_Result);
SQUARE SQUARE(expression) SQUARE(Entrance_Result);
SQRT SQRT(expression) SQRT(Total_Marks);
Aggregate Function
• An aggregate function performs a calculation on a set of values and
returns a single value.
• Various Aggregate Functions:
APPROX_COUNT_DISTINCT AVG VAR COUNT_BIG
CHECKSUM_AGG COUNT MIN GROUPING
GROUPING_ID SUM MAX STDEV
APPROX_COUNT_DISTINCT()
SELECT APPROX_COUNT_DISTINCT
(Column_name) AS Alias_name
FROM TABLE_NAME;
SELECT APPROX_COUNT_DISTINCT
(Roll_NO) AS
Approx_Distinct_Roll_NO
FROM Student;
This function returns the approximate number of unique non-null
values in a group.
COUNT()
SELECT COUNT(*)
FROM TABLE_NAME;
SELECT COUNT(*)
FROM Student;
COUNT function is used to Count the number of rows in a database
table. It can work on both numeric and non-numeric data types.
COUNT()
SELECT COUNT(column_name)
FROM TABLE_NAME;
SELECT COUNT(Roll_NO)
FROM Student;
COUNT with WHERE
SELECT COUNT(*)
FROM TABLE_NAME
WHERE [condition];
SELECT COUNT(*)
FROM Student
WHERE Total_Marks>400;
COUNT() with DISTINCT
SELECT COUNT(DISTINCT
column_name) FROM
TABLE_NAME;
SELECT COUNT(DISTINCT Grade)
FROM Student;
COUNT_BIG()
SELECT COUNT_BIG(*)
FROM TABLE_NAME;
SELECT
COUNT_BIG(column_name)
FROM TABLE_NAME;
SELECT COUNT_BIG(*)
FROM Student;
SELECT
COUNT_BIG(Roll_NO)
FROM Student;
SUM()
SELECT SUM(column_name)
FROM TABLE_NAME;
SELECT SUM(Total_Marks)
FROM Student;
Sum function is used to calculate the sum of all selected columns. It
works on numeric fields only.
SUM() with WHERE
SELECT SUM(column_name)
FROM TABLE_NAME
WHERE [condition];
SELECT SUM(Total_Marks)
FROM Student
WHERE Total_marks>400 ;
AVG()
SELECT AVG(column_name)
FROM TABLE_NAME;
SELECT AVG(Age)
FROM Student;
The AVG function is used to calculate the average value of the numeric
type. AVG function returns the average of all non-Null values.
MIN()
SELECT MIN(column_name)
FROM TABLE_NAME;
SELECT MIN(Total_Marks)
FROM Student;
MIN function is used to find the minimum value of a certain column.
This function determines the smallest value of all selected values of a
column.
MAX()
SELECT MAX(column_name)
FROM TABLE_NAME;
SELECT MAX(Total_Marks)
FROM Student;
MAX function is used to find the maximum value of a certain column.
This function determines the largest value of all selected values of a
column.
CHECKSUM_AGG()
SELECT
CHECKSUM_AGG(column_name)
FROM TABLE_NAME;
SELECT
CHECKSUM_AGG(Total_Marks)
FROM Student;
This function returns the checksum of the values in a group.
CHECKSUM_AGG ignores null values.
GROUPING()
SELECT GROUPING(column_name)
FROM TABLE_NAME GROUP BY
cloumn_name;
SELECT GROUPING(City)
FROM Student GROUP BY City;
A specified column expression in a GROUP BY list is aggregated or not.
GROUPING returns 1 for aggregated or 0 for not aggregated in the result
set.
GROUPING_ID()
SELECT
GROUPING_ID(column_name1,
cloumn_name2,...........) FROM
TABLE_NAME GROUP BY
cloumn_name1, cloumn_name2,.......;
SELECT GROUPING_ID(City, Age, Marks,
Grade) FROM
Studen GROUP BY
City, Age, Marks, Grade;
GROUPING_ID function concatenates the output of the GROUPING
functions applied to all the columns specified.
STDEV()
SELECT STDEV(column_name)
FROM TABLE_NAME;
SELECT STDEV(Total_Marks)
FROM Student;
Returns the statistical standard deviation of all values in the specified
expression.
VAR()
SELECT VAR(column_name)
FROM TABLE_NAME;
SELECT VAR(Total_Marks)
FROM Student;
Returns the statistical variance of all values in the specified expression.
String Manipulation Function
String manipulation function is a built-in functions which take an input
string and return a string or numeric value.
ASCII()
SELECT ASCII ( column_name ) AS
Alias_name FROM TABLE_NAME;
SELECT ASCII ( Grade ) AS
Grade_ASCII FROM Students;
Returns the ASCII code value of the leftmost character of a character
expression.
CHAR()
SELECT CHAR( column_name ) AS
Alias_name FROM TABLE_NAME;
SELECT CHAR( Roll_NO ) AS
CHAR_Roll_NO FROM Student;
Returns the single-byte character with the specified integer code.
CHARINDEX()
SELECT column_name
CHARINDEX( expressionToFind ,
expressionToSearch) FROM
TABLE_NAME;
SELECT Roll_NO,
CHARINDEX('Siva Nagar', Address)
FROM Student;
This function searches for one character expression inside a second character
expression, returning the starting position of the first expression if found.
PATINDEX()
SELECT column_name PATINDEX(
expressionToFind ,
expressionToSearch) FROM
TABLE_NAME;
SELECT Roll_NO,
PATINDEX('Siva Nagar', Address)
FROM Student;
Returns the starting position of the first occurrence of a pattern in a
specified expression, or zeros if the pattern is not found.
CONCAT()
SELECT CONCAT( column_name1, ‘ ‘,
column_name2) AS Alias_name
FROM TABLE_NAME;
SELECT CONCAT( First_Name,‘
‘,Last_Name)
AS Result
FROM Student;
This function returns a string resulting from the concatenation, or joining, of two or
more string values in an end-to-end manner.
SOUNDEX()
SELECT SOUNDEX (
character_expression );
SELECT SOUNDEX (
character_expression,
character_expression );
SELECT SOUNDEX ( 'Smith' );
SELECT SOUNDEX ('Smith'),
SOUNDEX ('Smythe');
Returns a four-character (SOUNDEX) code to evaluate the similarity of
two strings.
DIFFERENCE()
SELECT DIFFERENCE (
character_expression ,
character_expression );
SELECT DIFFERENCE('Smith',
'Smyth');
This function returns an integer value measuring the difference
between the SOUNDEX() values of two different character
expressions.
LEFT()
SELECT LEFT(column_name, 3)
AS Alias_name
FROM TABLE_NAME;
SELECT LEFT(First_Name, 3) AS RESULT
FROM Student;
Returns the left part of a character string with the specified number of
characters.
RIGHT()
SELECT RIGHT(column_name, 2)
AS Alias_name
FROM TABLE_NAME;
SELECT RIGHT(First_Name, 2) AS
'RIGHT_NAME'
FROM Student;
Returns the right part of a character string with the specified number
of characters.
LEN()
SELECT LEN(column_name) AS
Alias_name, column_name FROM
TABLE_NAME;
SELECT LEN(First_Name) AS
Length, First_Name FROM
Student;
Returns the number of characters of the specified string expression,
excluding trailing spaces.
LOWER()
SELECT LOWER(column_name) AS
Alias_name FROM TABLE_NAME;
SELECT LOWER(Grade) AS
Grade_Lower FROM Student;
Returns a character expression after converting uppercase character
data to lowercase.
UPPER()
SELECT UPPER(column_name) AS
Alias_name FROM TABLE_NAME;
SELECT UPPER(First_Name) AS
Name FROM Student;
Returns a character expression with lowercase character data
converted to uppercase.
LTRIM()
SELECT LTRIM ( column_name )
FROM TABLE_NAME ;
SELECT LTRIM(City) FROM
Student;
Returns a character expression after it removes leading blanks.
RTRIM()
SELECT RTRIM ( column_name)
FROM TABLE_NAME;
SELECT RTRIM(City) FROM
Student;
Returns a character string after truncating all trailing spaces.
REPLACE()
SELECT REPLACE (
string_expression , string_pattern
, string_replacement ) AS
Alias_name FROM TABLE_NAME ;
SELECT REPLACE
(Address,'Block','Street') AS
replaced_data FROM Student;
Replaces all occurrences of a specified string value with another string
value.
REVERSE()
SELECT column_name,
REVERSE(column_name) AS
Alias_name FROM TABLE_NAME;
SELECT First_Name, REVERSE
(First_Name) AS REVERSE
FROM Student;
Returns the reverse order of a string value.
TRIM()
SELECT ( column_name) AS
Alias_name FROM TABLE_NAME;
SELECT ( City) AS RESULT
FROM Student;
Removes the space character char(32) or other specified characters
from the start and end of a string.
THANK YOU

0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf