0% found this document useful (0 votes)
10 views

Ch-6 Mysql Functions

Uploaded by

katiyarnihal3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Ch-6 Mysql Functions

Uploaded by

katiyarnihal3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

CH-6 MySQL

FUNCTIONS

By Er. Ankit Tomar


Outline
• Introduction
• Text Functions
• Numeric Functions
• Data/Time Functions
What are MySQL Functions?
• Functions are built-in operations in MySQL used to perform calculations,
manipulate data, or retrieve information.
• They can be applied in SQL queries to transform data during the selection
process.
Text/String Functions
1. CHAR( ) 8. TRIM
2. CONCAT( ) 9. INSTR
3. LOWER/LCASE 10. LENGTH
4. SUBSTR( ) 11. LEFT
5. UPPER/UCASE 12. RIGHT
6. LTRIM 13. MID
7. RTRIM
1. CHAR()

• Converts integer ASCII values to


characters.

SELECT CHAR(65, 66, 67);

Output: 'ABC'
2. CONCAT()

• Combines two or more strings into one.

SELECT CONCAT('MySQL', ' Tutorial');


Output: 'MySQL Tutorial'
3. LOWER() / LCASE()

• Converts a string to lowercase.

SELECT LOWER('MySQL');
Output: 'mysql'
4. SUBSTR()

• Extracts a substring from a string.

SELECT SUBSTR('Database', 1, 4);


Output: 'Data'
5. UPPER() / UCASE()

• Converts a string to uppercase.

SELECT UPPER('mysql');
Output: 'MYSQL'
6. LTRIM()

• Removes leading spaces from a string.

SELECT LTRIM(' MySQL');


Output: 'MySQL'
7. RTRIM()

• Removes trailing spaces from a string.

SELECT RTRIM('MySQL ');


Output: 'MySQL'
8. TRIM()

• Removes both leading and trailing spaces


from a string.

SELECT TRIM(' MySQL ');


Output: 'MySQL'
9. INSTR()

• Returns the position of the first


occurrence of a substring in a string.

SELECT INSTR('MySQL Tutorial', 'SQL');


Output: 3
10. LENGTH()

• Returns the length of a string.

SELECT LENGTH('MySQL');
Output: 5
11. LEFT()

• Returns a specified number of characters


from the start of a string.

SELECT LEFT('Database', 4);


Output: 'Data'
12. RIGHT()

• Returns a specified number of characters


from the end of a string.

SELECT RIGHT('Database', 4);


Output: 'base'
13. MID()

• Extracts a substring starting from a


specified position in the string.

SELECT MID('Database', 2, 4);


Output: 'atab'
Numeric Functions
1. MOD
2. POWER/POW
3. ROUND
4. SIGN
5. SQRT
6. TRUNCATE
1. MOD()

• Returns the remainder of a division


between two numbers.
• If either of the numbers is NULL, it SELECT MOD(10, 3);
returns NULL.
Output: 1
2. POWER() / POW()

• Raises a number to the power of another


number.
• POWER() and POW() are SELECT POWER(2, 3);
interchangeable in MySQL.
Output: 8
3. ROUND()

• Rounds a number to the nearest integer


or to a specified number of decimal
places.
SELECT ROUND(123.456, 2);
• If the second argument is omitted, it
rounds to the nearest integer. Output: 123.46
4. SIGN()

• Returns the sign of a number: -1 for


negative, 0 for zero, and 1 for positive.
• Useful for determining the direction of a SELECT SIGN(-10);
value.
Output: -1
5. SQRT()

• Returns the square root of a non-


negative number.
• If the number is negative, it returns SELECT SQRT(16);
NULL.
Output: 4
6. TRUNCATE()

• Truncates a number to a specified


number of decimal places without
rounding.
SELECT TRUNCATE(123.456, 2);
• If the second argument is 0, it truncates
to an integer. Output: 123.45
Date/Time Functions
1. CURDATE( )/ CURRENT_DATE( ) 8. DAYOFMONTH( )
2. DATE( ) 9. DAYOFWEEK( )
3. MONTH( ) 10. DAYOFYEAR( )
4. MONTHNAME( ) 11. NOW( )
5. DAY( ) 12. SYSDATE( )
6. YEAR( )
7. DAYNAME( )
1. CURDATE() /
CURRENT_DATE()

• Returns the current date in 'YYYY-MM-


DD' format.
• CURDATE() and CURRENT_DATE() SELECT CURDATE();
are interchangeable.
Output: '2024-09-22'
2. DATE()

• Extracts the date part from a datetime


expression, removing the time part.
• Can be used to focus only on the date SELECT DATE('2024-09-22 14:30:00');
when working with datetime values.
Output: '2024-09-22'
3. MONTH()

• Returns the month part of a date as a


number (1 to 12).
• Works with dates or datetime values. SELECT MONTH('2024-09-22');
Output: 9
4. MONTHNAME()

• Returns the full name of the month for a


given date.
• Useful for displaying months in a more SELECT MONTHNAME('2024-09-22');
human-readable format.
Output: 'September'
5. DAY()

• Returns the day of the month (1 to 31)


from a date.
• Equivalent to DAYOFMONTH(). SELECT DAY('2024-09-22');
Output: 22
6. YEAR()

• Extracts the year from a date.


• Useful for filtering or displaying only the
year part of a date. SELECT YEAR('2024-09-22');
Output: 2024
7. DAYNAME()

• Returns the name of the day of the week


for a given date.

SELECT DAYNAME('2024-09-22');
Output: 'Sunday'
8. DAYOFMONTH()

• Returns the day of the month for a given


date (1 to 31).
• Similar to DAY(), but more explicit in its SELECT DAYOFMONTH('2024-09-22');
purpose.
Output: 22
9. DAYOFWEEK()

• Returns the weekday index for a date,


with Sunday as 1 and Saturday as 7.
• Useful for determining the day’s position SELECT DAYOFWEEK('2024-09-22');
within a week.
Output: 1 (Sunday)
10. DAYOFYEAR()

• Returns the day of the year (1 to 366) for


a given date.
• Useful for calculating the number of SELECT DAYOFYEAR('2024-09-22');
days passed in a year.
Output: 266
11. NOW()

• Returns the current date and time in


'YYYY-MM-DD HH:MM' format.
• Commonly used for timestamping SELECT NOW();
records.
Output: '2024-09-22 14:30:00'
SYSDATE()

• Returns the current date and time at the


moment the function is executed.
• Unlike NOW(), which returns a constant SELECT SYSDATE();
value for the entire query, SYSDATE()
returns the exact time each time it is Output: '2024-09-22 14:30:00' (example format)
called, which can be useful in scenarios
involving multiple queries or
transactions.
Practice Time
1. Get all employees.
2. Display the first name and
last name of all employees.
3. Display all the values of the
“First_Name” column using the
alias “Employee Name”
4. Get all “Last_Name” in
lowercase.
Practice Time
5. Get all “Last_Name” in
uppercase.
6. Get unique “DEPARTMENT”.
7. Get the first 4 characters of
“FIRST_NAME” column.
8. Get the position of the letter
‘h’ in ‘John’.
Practice Time
9. Get all values from the
“FIRST_NAME” column after
removing white space on the
right.
10. Get all values from the
“FIRST_NAME” column after
removing white space on the
left.
Practice Time
11. Write the syntax to create
the “employee” table.
Answers
1. SELECT * FROM employee;
2. SELECT First_name, Last_name FROM employee;
3. SELECT First_name AS "Employee Name" FROM employee;
4. SELECT LOWER(Last_name) FROM employee;
5. SELECT UPPER(Last_name) FROM employee;
6. SELECT DISTINCT Departement FROM employee;
Answers
7. SELECT SUBSTRING(First_name,1,4) FROM employee;
8. SELECT LOCATE('h',First_name) FROM employee where
First_name='John’;
9. SELECT RTRIM(First_name) FROM employee;
10. SELECT LTRIM(First_name) FROM employee;
Answers
11. CREATE TABLE Employee(
employee_id int NOT NULL,
First_name varchar(50) NULL,
Last_name varchar(50) NULL,
salary decimal(18, 0) NULL,
joining_date datetime2(7) default getdate(),
departement varchar(50) NULL
);
CH-7 Querying Using
SQL

By Er. Ankit Tomar


ORDER BY clause
• The ORDER BY clause in SQL is
used to sort the result set of a
query by one or more columns,
either in ascending (ASC) or
descending (DESC) order. By
default, it sorts in ascending order
if no order is specified.
Aggregate Functions
• Aggregate functions are used to
perform calculations on multiple
rows of a table and return a single
value. These functions are
commonly used in SQL for
summarizing data. 1. COUNT()
2. SUM()
3. AVG()
4. MAX()
5. MIN()
Types of SQL functions

Single Row Multiple Row


• Operate on individual rows and return one result • Operate on multiple rows and return a single
per row. result, summarizing the data.
• These functions are typically used to modify or • Used to perform calculations across a set of rows.
calculate values for each row in a query result.
• Examples:
• Examples:
• COUNT(): Returns the number of rows.
UPPER(): Converts text to uppercase.
• SUM(): Returns the sum of a numeric columns.
LENGTH(): Returns the length of a string.
• AVG(): Returns the average of a numeric column.
ROUND(): Rounds a number to a specified number
of decimal places.
GROUP BY clause
• The GROUP BY clause in SQL is
used to group rows that have the
same values in specified columns
into summary rows.
• It is commonly used with aggregate
functions (COUNT(), SUM(),
AVG(), etc.) to perform calculations
on each group of data.
Placing Conditions on Groups (HAVING Clause)
The `HAVING` clause in SQL is used to place conditions on groups created by the `GROUP BY` clause. While the
`WHERE` clause filters individual rows before grouping, the `HAVING` clause filters groups after the `GROUP
BY` operation.

**Syntax:**

SELECT column1, aggregate_function(column2)


FROM table_name
GROUP BY column1
HAVING condition;
**Example:**
Suppose we have a table called `Sales` with columns `Salesperson` and `SalesAmount`. If we want to find the
salespeople whose total sales exceed $10,000, we can use the `HAVING` clause:

SELECT Salesperson, SUM(SalesAmount) AS TotalSales


FROM Sales
GROUP BY Salesperson
HAVING SUM(SalesAmount) > 10000;

Explanation:
- The `GROUP BY` clause groups the rows by `Salesperson`.
- The `HAVING` clause filters out groups where the total sales are less than or equal to $10,000, showing only those
with higher sales.

You might also like