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

INFORMATICS PRACTICES

(065)

Unit-3
DATABASE CONCEPTS
AND
THE STRUCTURED QUERY LANGUAGE
(Session: 2023-24)
For
Class 12th

Submitted By: Submitted To:


Aashi Nagiya Manish Agarwal
SQL FUNCTION

In the context of databases and SQL (Structured Query Language), a function


is a pre-defined or user-defined routine that can be called to perform a specific
operation. SQL functions are used to encapsulate logic and perform
calculations on data stored in a database.

• It is a set of SQL statements that accept only input parameters that perform
action and return the result.
• A function can return on only single value or a table.
• A function is used as a part of SQL command.

TYPES OF FUNCTION

There are two types of function in SQL

1. Scalar Function

Operate on a single row, returning a single result. Examples include UPPER


(converts a string to uppercase), LOWER (converts a string to lowercase), LEN
(returns the length of a string), etc.

2. AGGREGATE FUNCTION

Perform a calculation on a set of values and return a single value. Examples


include SUM, AVG, COUNT, MIN, and MAX.
SCALAR FUNCTION
A scalar function is a function that operates on the scalar value. It takes one or
more input value or aggregate directly and return a value.

There are different types of scalar function like:

• Math Function
Math function or the mathematical function perform the operation over the
different numeric values.

Types of Math Function:

I. Power (): Power function return the values of a function based to the power of
another function. It is also called the pow ().
Syntax
Pow (m, n) Here ‘m’ is a number which is the base of the exponentiation and ‘n’ is
a number exponent of the exponentiation.

Example 1
mysql> select Pow (5, 3);
mysql>125

Example 2
mysql> select Pow (2, 8);
mysql>256

II. Round (): The round () returns a number rounded to a certain number of definite
places.
Syntax
Round (column__name, Position_ of _Decimal)
Example 1
mysql> select round (454.352, 2);
mysql>454.35

Example 2
mysql> select round (454.352, 0);
mysql>454

Example 3
mysql> select round (454.352, -2);
mysql>500

Example 4
mysql> select round (454.3052, 2);
mysql>454.31

III. MOD (): The mod () return the reminder of one number divided by another.
Syntax
MOD (Dividend, Divisor)
Here Dividend is a literal number or a numeric expression to divide.
Divisor expression by which to divide the dividend.

Example 1
mysql> select mod (19,3);
mysql>1

Example 2
mysql> select mod (22.5,7);
mysql>1.5
• Text Function
Text Function perform operation over the string values.

Syntax
1. UPPER (), Upper(str)

Example 1
mysql> select upper (‘Informatic Practices’);
mysql>INFORMATIC PRACTICES
# Upper case or upper(str) is also represent with Ucase ().
Syntax
Lower(str)
It returns the string (str) with all character changed to lowercase.
Example 1
mysql> select lower (‘AASHI’);
mysql> aashi

2. Sub String ()
Substring perform the operation over the string value. The forms without a len
(length) argument return a substring from a string (str). Starting at the position POS.
The forms with a len argument return a substring len character long from string (str).
Syntax
1) Substring (str, Pos) ______Substring (str from Pos)
2) Substring (str, Pos, len) ______Substring (str from Pos for len)

Example 1
mysql> select substring (‘eligibility’, 3);
mysql> gibility
3. Length (str)
In text function length function perform operation over the string values. Length
function return the length of the STR.
Syntax
length(‘string’)

Example 1
mysql> select (‘Text’);
mysql> 4

Example 2
mysql> select length (‘Informatics Practices’);
mysql> 21

Example 3
mysql> select length (‘Birth’);
mysql> 5

4. LEFT FUNCTION
Left function returns the left most len characters from the string(str) or null if any
argument is null.

Example 1
mysql> select left (‘eligibility’, 5);
mysql> eligi

Example 2
mysql> select left (‘Informatics Practices’, 11);
mysql> informatics
5. Right Function RIGHT (str, len)
The right fune return the right most len characters from the string str.
Or Null, if any arguments null.

Example1
mysql> select left (‘eligibility’, 6);
mysql> bility

6. InStr Function (INSTR/str, substr)


In string function returning the position of the first occurrence of substring (Substr) in
string (str)

Example1
mysql> SELECT INSTR ('toolbox', 'box');
mysql> 5

7. LTRIM (str)
LTRIM (str) reburn the string str with leading my space character remove LTRIM

Example1
mysql> SELECT LTRIM (‘Toolbox’);
mysql> Toolbox

8. RTRIM (str):
R trim function return the string str with leading my space character remove
9. LTRIM.
For Example
mysql> SELECT RTRIM ('toolbox');
mysql> Toolbox

8. TRIM (str)
Trim function returns the string (str) with all space character prefer or suffer removed.

Example1
mysql> SELECT TRIM('Toolbox');
mysql> Toolbox

Example 2
mysql> SELECT TRIM (LEADING 'x' FROM 'xxxToolboxxxx');
mysql> Toolboxxxx

Example 3
mysql> SELECT TRIM (TAILING 'x' FROM 'Toolboxxxx');
mysql> Toolbox
Data Function
In SQL data function perform operation over the data values. It refers to functions that
are used to manipulate or perform operations on date and time values. These
functions are particularly useful when working with columns or variables that store
temporal information
Following are the different types of data function:
1) NOW ()
Now function return the current data and time at a value in ‘YYYY_MM__DD’,
‘hh:mm:ss’ format, depending on the location. The function is used in string or numeric
content.
For Example:
mysql> Select now ();
Output
‘2023-11-22 11:59:59’

2) DATE ()
Date function extracts the date part of the data expression expr.
For Example
mysql> Select date (‘2023_11_23’);

Output
2023_11_23

3) MONTH ()
Month function returns the month for date, in the range of 1 to 12 (January to
December)
For Example
mysql>Select month (‘2023_11_23’);
Output
11
4) MONTHNAME ()
Month Name function returns the full name of the month for date.
For Example
mysql>Select monthname (‘2023_11_23’);
Output
November

5) YEAR ()
Year function returns the year for date, in the range 1000 to 9999.
For Example
mysql>Select year (‘2023_11_23’);
Output
2023

6) DAY ()
Day function returns the day of the month for date in the range 01-31.
For Example
mysql>Select day of the month (‘2023_11_23’);
Output
23

7) DAYNAME ()
Day name function returns the name of the weekday for day.
For Example
mysql>Select dayname (‘2023_11_23’);
Output
Thursday
AGGREGATE FUNCTION
An aggregate function that operates on aggregate data. It is a complete set of data as
input and returns the value that is complete from all the values in the set. SQL function
perform operation over the set of values or apply on the column. Aggregate function is
also called group function.

Types of Aggregate Function


Following are the types of Aggregate function:

i. Max ()
Max function is use to get the maximum value from a column. To get the
maximum salary (as per the given table) drawn by an employee, the query would
be:
Mysql> Select max (salary) from tablename/emp;

ii. Min ()
Min function is use to get the minimum value from a column. To get the
maximum salary (as per the given table) drawn by an employee, the query would
be:
Mysql> Select min (salary) from tablename/emp;

iii. Avg ()
Avg function is use to get the Average value from a column. To get the maximum
salary (as per the given table) drawn by an employee, the query would be:
Mysql> Select avg (salary) from tablename/emp;

iv. Sum ()
Sum function is use to get the sum of a numeric value in the column. To get the
total salary (as per the given table) drawn by an employee, the query would be:
Mysql> Select sum (salary) from tablename/emp;
v. Count ()
Count function return the number of rows in the given table that satisfies the
condition specified in WHERE clause. If the WHERE clause condition is not
satisfied then the query return the total number of rows in the table.
For Example:
If we want the number of employees in a particular department from the given
table then the query would be:
Mysql>Select count from Emp
Mysql> Select count (*) from Emp;
The output will be
7 (as per the given Emp table)
Another condition of count function
Mysql>Select count () from Emp WHERE dpt=HR;
Output:
3

To understand the SQL aggregate function considering an employee’s database


table which have the following records:
EMPLOYEE (Emp)
Employee Id Name Salary Department
001 Aashi 5000 HR
002 Bhumika 4000 Tech.
003 Mansi NULL HR
004 Mohit 3500 Management
005 Sandeep 4500 Management
006 Krishna 4000 HR
007 Umang NUll Tech.
SQL Quarries (Aggregate):

1. mysql> Select max (salary) from emp;


2. mysql> Select min (salary) from emp;
3. mysql> Select avg (salary) from emp;
4. mysql> Select sum (salary) from emp;
5. mysql> Select count (salary) from emp;
6. mysql> Select count (salary) from emp;
Output:
1- 5000
2- 3500
3- 4200
4- 5
5- 7

My SQL Order by Clause


My Sql order by clause is use to sort the table data in ascending is use to sort the table
data in ascending order or descending order.
By default, data is not inserted into table in any order then the data will be arranged in
ascending order.

If we want to retrieve the data in any particular order then we have to sort it. By using
Mysql order by clause or statement.

Syntax
Select columnname from table name order by (columnname ASC/DESC)

For Example
mysql> Select * from Emp order by salary;

You might also like