0% found this document useful (0 votes)
20 views14 pages

Functions in Mysql

Uploaded by

jahnvis102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Functions in Mysql

Uploaded by

jahnvis102
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

FUNCTIONS IN MYSQL

Single-row functions: Single row functions operate on a single


value to return a single value. They can accept one or more
arguments but return only one result per row. When applied
on a table, they return a single result for every row of the
queried table. They are further categorized into:
❖ Numeric functions
❖ String functions
❖ Date and Time function

Multiple Row Functions (also called Aggregate


Functions): Multiple row functions operate on a set of
rows to return a single value. Examples include SUM(),
AVG() and COUNT().
Single row functions
I. Mathematical/Numeric functions :
• Returns the value of a number raised
POW() to the power of another number

• Rounds a number to a specified


ROUND() number of decimal places

• Truncates a number to the specified


TRUNCATE() number of decimal places

MOD() • Returns the remainder of a number


divided by another number

SQRT() • Returns the square root of a


number

ABS() • Returns the absolute value of a


number
2.String(text) functions :

• RETURNS THE ASCII VALUE OF GIVEN


a. ASCII() CHARACTER

• CONVERTS CHARACTER STRING


b.LOWER()/LCASE DATA INTO LOWER CASE

• CONVERTS CHARACTER STRING


c.UPPER()/UCASE()
DATA INTO UPPER CASE

• Returns the length of a string (in


d.LENGTH() characters)

• Replaces all occurrences of a substring


e.REPLACE() within a string, with a new substring

• Extracts a number of characters from a


f.LEFT() string (starting from left)

• Extracts a number of characters from a


g.RIGHT() string (starting from right)

h.LTRIM() • Removes leading spaces from a string

i.RTRIM() • Removes trailing spaces from a string


• Removes leading and trailing
j.TRIM() spaces from a string

• Repeats a string as many times as


k.REPEAT() specified

l.SUBSTRING()/ • Extracts a substring from a string


MID()/SUBSTR() (starting at any position)

m.REVERSE() • Reverses a string and returns the result

• Returns the position of the first


n.INSTR() occurrence of a string in another string

o.CONCAT() • Adds two or more expressions together

3.DATE AND TIME FUNCTIONS:


1. CURDATE()/CURRENT_DATE()

Description: Returns the current date in YYYY-MM-DD


format.

Usage Example:

SELECT CURDATE();
2. NOW()

Description: Returns the current date and time in YYYY-


MM-DD HH:MM:SS format.

Usage Example:

SELECT NOW();

3. DATE()

Description: Extracts the date part from a date or


datetime expression.

Usage Example:

SELECT DATE(NOW()); -- Returns current date

4. YEAR()

Description: Extracts the year from a date.

Usage Example:

SELECT YEAR('2024-09-03'); -- Returns 2024

5. MONTH()

Description: Extracts the month from a date.

Usage Example:

SELECT MONTH('2024-09-03'); -- Returns 9

6. DAY()

Description: Extracts the day of the month from a date.

Usage Example:

SELECT DAY('2024-09-03'); -- Returns 3


MULTIPLE ROW FUNCTIONS
In MySQL, multiple row functions are used to perform
operations on a set of rows and are often employed in
conjunction with aggregate functions to summarize or analyze
data. These functions are typically used with SQL queries that
involve GROUP BY clauses or aggregate calculations.

I. SUM():
Calculates the total sum of a numeric column.

II. AVG()

Computes the average value of a numeric column.


III. MAX()

Returns the maximum value from a column.

IV. MIN()

Returns the minimum value from a column.

V. COUNT()

Counts the number of rows that match a specified


condition or the total number of rows in a group.
VI. COUNT-DISTINCT
In MySQL, the COUNT(DISTINCT column_name)
function is used to count the number of unique
(distinct) values in a column.

SORTING IN SQL -ORDER BY:

OPERATIONS ON RELATIONS :
1.UNION
In MySQL, the UNION operator is used to combine the result
sets of two or more SELECT queries into a single result set.
This operation is particularly useful when you need to
aggregate results from multiple queries that have similar
structures.
2.INTERSECTION
In MySQL, there is no direct INTERSECT operator to find the
intersection of two result sets. However, you can achieve the same
result using other SQL constructs. Here are some methods to simulate
the INTERSECT operation in MySQL:

1. Using INNER JOIN

You can use INNER JOIN to find the common rows between two
tables based on a shared column.

2. Using EXISTS Subquery

Another way to find the intersection is by using the EXISTS


subquery, which checks for the existence of matching rows in a
second table.
SQL JOINS :
#we will perform join operations on the table department and
employees.

1.Cartesian product ( cross join)


The Cross join keyword returns all records from both tables
(table1 and table2).
2.Equi join
EQUI JOIN creates a JOIN for equality or matching column(s) values
of the relative tables. EQUI JOIN also create JOIN by using JOIN
with ON and then providing the names of the columns with their
relative tables to check equality using equal sign (=).

3.Inner join
The INNER JOIN keyword selects records that have matching
values in both tables.

4.Right outer join


A right outer join is a method of combining tables. The result includes
unmatched rows from only the table that is specified after the RIGHT
OUTER JOIN clause. If you are joining two tables and want the result
set to include unmatched rows from only one table, use a LEFT
OUTER JOIN clause or a RIGHT OUTER JOIN clause.
5.Natural join
A natural join in SQL combines rows from two or more tables
based on the common column(s) between them. The common
columns on which the tables are joined must have the same
name and data type across the tables.

You might also like