CBSE Class 11 Informatics Practices Functions in MySQL
CBSE Class 11 Informatics Practices Functions in MySQL
com
Functions in MySQL
Learning Objectives
State the syntax and working of most of the Numeric, String and date/Time
functions.
Functions are a powerful feature of SQL. Using these functions, we can find sum of
values stored in a column or convert all characters of a name to lowercase or round off
salaries in a column to two decimal places and so on. MySQL supports many
functions to manipulate data. We can broadly categorize functions into two types:
Single Row functions and Multiple Row Functions.
Functions
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:
v
Numeric functions
v
String functions
v
Date and Time functions
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().
Let us consider the following table named Employee with 5 rows. We will be referring to
it in our lesson to learn about Functions.
A) Numeric Functions:
argument X to Result: -1
ROUND(X)
D decimal b) mysql> SELECT ROUND(-1.58);
places. Result: -2
c) mysql> SELECT ROUND(1.43);
b) If number of
Result: 1
decimal places
d) mysql> SELECT ROUND(6.298, 1);
is not specified
Result: 6.3
or is zero, the
e) mysql> SELECT ROUND(6.235, 0);
number rounds
Result: 6
to the nearest
f) mysql> SELECT ROUND(56.235, -1);
integer OR (0
Result: 60
decimal places).
String functions operate on character type data. String functions are used to extract,
change, format or alter character strings. They return either character or numeric
values. The following table tells us about the Character functions of MySQL and
what they do.
Note: SUBSTR is
the same as
SUBSTRING
Date and Time functions allow us to perform many types of tasks on Date type
data.The default date format in MySQL is YYYY-MM-DD.
DD format or
YYYYMMDD format,
depending on
whether the function
is used in a string or
numeric context.
'YYYY-MM-DD
HH:MM:SS' or
YYYYMMDDHHM
MSS.uuuuuu format,
depending on
whether the function
is used in a string or
numeric context.
YYYYMMDDHHM
MSS.uuuuuu format,
depending on
whether the function
is used in a string or
numeric context.
Note : SYSDATE()
returns the time at
which the function
executes. SYSDATE()
differs from NOW()
which returns a
constant time that
indicates the time at
which the statement
began to execute.
* For difference
between SYSDATE()
and NOW() refer to
NOTE at the end of
this table.
Result:
+----+------------+------------------+
| id | date_join | month(date_join) |
+----+------------+------------------+
| 1 | 1996-07-25 | 7 |
| 2 | 1995-06-27 | 6 |
| 3 | 1990-02-20 | 2 |
| 4 | 1989-08-18 | 8 |
| 5 | 2010-03-01 | 3 |
+----+------------+------------------+
5 rows in set (0.00 sec)
on.
Result:
+----+------------+----------------------+
| id | date_join | dayofweek(date_join) |
+----+------------+----------------------+
| 1 | 1996-07-25 | 5 |
| 2 | 1995-06-27 | 3 |
| 3 | 1990-02-20 | 3 |
| 4 | 1989-08-18 | 6 |
| 5 | 2010-03-01 | 2 |
+----+------------+----------------------+
5 rows in set (0.00 sec)
Note
Difference between NOW() and SYSDATE()
Summary
1. Functions perform some operations and return a value.
5. String functions operate on character type data. They return either character or
numeric values.
2. List 3 categories of single row functions. Give two examples in each category.
5. Which Date function displays the result like "Monday" or "Tuesday" etc.
6. Name a
a) Using the three separate words "We," "study," and "MySQL," produce the
following output:
b) Use the string "Internet is a boon" and extract the string "net".
f) Display the date 10 years from now. Label the column "Future."
g) Display the day of week on which your birthday will fall or fell in 2010.
Lab Exercises
1. Create the following table named "Charity" and write SQL queries for the tasks
that follow:
Table: Charity
P_Id LastName FirstName Address City Contribution
(Contribution is in Rs.)
I. Display all first names in lowercase
II. Display all last names of people of Mumbai city in uppercase
III. Display Person Id along with First 3 characters of his/her name.
IV. Display first name concatenated with last name for all the employees.
V. Display length of address along with Person Id
VI. Display last 2 characters of City and Person ID.
VII. Display Last Names and First names of people who have "at" in the second or
third position in their first names.
VIII. Display the position of 'a' in Last name in every row.
INFORMATICS PRACTICES 273
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
IX. Display Last Name and First name of people who have "a" as the last character
in their First names.
X. Display the first name and last name concatenated after removing the leading
and trailing blanks.
XI. Display Person Id, last names and contribution rounded to the nearest rupee
of all the persons.
XII. Display Person Id, last name and contribution with decimal digits truncated
of all the persons.
XIII. Display Last name, contribution and a third column which has contribution
divided by 10. Round it to two decimal points.
2. Consider the table "Grocer" and write SQL queries for the tasks that follow:
Table: Grocer
Item_Id ItemName UnitPrice Quantity (kg) Date_Purchase