0% found this document useful (2 votes)
2K views

CBSE Class 11 Informatics Practices Functions in MySQL

Functions in MySQL can be categorized as single row functions or multiple row functions. Single row functions operate on a single value and return one result per row, and include numeric, string, and date/time functions. Multiple row functions operate on multiple rows and return a single value, such as with aggregate functions like SUM(), AVG(), and COUNT(). The document then discusses numeric functions in MySQL in detail, providing examples of functions like POWER(), ROUND(), and TRUNCATE() that perform calculations on numeric values.

Uploaded by

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

CBSE Class 11 Informatics Practices Functions in MySQL

Functions in MySQL can be categorized as single row functions or multiple row functions. Single row functions operate on a single value and return one result per row, and include numeric, string, and date/time functions. Multiple row functions operate on multiple rows and return a single value, such as with aggregate functions like SUM(), AVG(), and COUNT(). The document then discusses numeric functions in MySQL in detail, providing examples of functions like POWER(), ROUND(), and TRUNCATE() that perform calculations on numeric values.

Uploaded by

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

Downloaded from www.studiestoday.

com

Functions in MySQL
Learning Objectives

After studying this lesson the students will be able to

Distinguish between two types of functions.

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 Multiple Row 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().

(Note : Multiple Row functions will be discussed in detail in Class XII)

254 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

Let us consider the following table named Employee with 5 rows. We will be referring to
it in our lesson to learn about Functions.

CREATE TABLE Employee(


id int,
first_name VARCHAR(15),
last_name VARCHAR(15),
date_join DATE,
salary DECIMAL(8,2),
city VARCHAR(10)
);

The rows in Employee table are as follows:


mysql> SELECT * FROM Employee;
+----+------------+-----------+------------+----------+---------+
| id | first_name | last_name | date_join | salary | city |
+----+------------+-----------+------------+----------+---------+
| 1 | Amit | Sharma | 1996-07-25 | 25000.00 | Delhi |
| 2 | Deeksha | Verma | 1995-06-27 | 30000.00 | Pune |
| 3 | Navkiran | Ahluwalia | 1990-02-20 | 32000.50 | Delhi |
| 4 | Mamta | Sharma | 1989-08-18 | 37500.50 | Mumbai |
| 5 | Bhawna | Ahlurkar | 2010-03-01 | 42389.50 | Chennai |
+----+------------+-----------+------------+----------+---------+
5 rows in set (0.00 sec)

A) Numeric Functions:

MySQL numeric functions perform operations on numeric values and return


numeric values. The following table tells us about the numeric functions of MySQL
and what they do.

INFORMATICS PRACTICES 255


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

Sno Name & Syntax Description Example

1 POWER(X,Y) Returns the value a)mysql> SELECT POW(2,4);


or of X raised to the Result: 16
POW(X,Y) power of Y. b)mysql> SELECT POW(2,-2);
Result: 0.25
c)mysql> SELECT POW(-2,3);
Result:-8
d)mysql> SELECT id,salary,
POWER(salary,2) FROM employee;
Result:
+----+----------+-----------------+
| id | salary | power(salary,2) |
+----+----------+-----------------+
| 1 | 25000.00 | 625000000 |
| 2 | 30000.00 | 900000000 |
| 3 | 32000.50 | 1024032000.25 |
| 4 | 37500.50 | 1406287500.25 |
| 5 | 42389.50 | 1796869710.25 |
+----+----------+-----------------+
5 rows in set (0.00 sec)

2 ROUND(X,D) a) Rounds the a) mysql> SELECT ROUND(-1.23);

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).

256 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

c) If negative g) mysql> SELECT id,ROUND(salary,0)


FROM employee;
value is
Result:
specified for
+------+-----------------+
precision, it
| id | round(salary,0) |
counts off that
+------+-----------------+
value left from
| 1 | 25000 |
the decimal
| 2 | 30000 |
point.
| 3 | 32001 |
| 4 | 37501 |
| 5 | 42390 |
d) If positive
+------+-----------------+
value is
5 rows in set (0.00 sec)
specified for
precision, it
counts off that
value right
from the
decimal point.

3 TRUNCATE Returns the a) mysql> SELECT TRUNCATE(7.543,1);

(X,D) number X, Result: 7.5

truncated to D b) mysql> SELECT TRUNCATE(4.567,0);

decimal places. If Result: 4

D is 0, the result c) mysql> SELECT TRUNCATE(-7.45,1);

has no decimal Result: -7.4

point or fractional d) mysql> SELECT TRUNCATE(346,-2);

part.If D is Result: 300

negative, it causes e) mysql> SELECT


id,TRUNCATE(salary,0) FROM
D digits left of the employee;
decimal point of
the value X to
become zero.

INFORMATICS PRACTICES 257


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

Note: TRUNCATE Result:

does not round a +------+--------------------+

number. It simply | id | truncate(salary,0) |

chops off digits +------+--------------------+

from a number. | 1 | 25000 |


| 2 | 30000 |
| 3 | 32000 |
| 4 | 37500 |
| 5 | 42389 |
+------+--------------------+
5 rows in set (0.00 sec)

B) String( Character) Functions

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.

Sno Name & Syntax Description Example

1 LENGTH(str) Returns the length a) mysql> SELECT LENGTH


('Informatics');
of a column or a
Result: 11
string in bytes.
b) mysql> SELECT LENGTH(First_Name)
FROM Employee;
Result:
+--------------------+
| LENGTH(First_Name) |
+--------------------+
| 4 |
| 7 |
| 8 |
| 5 |
| 6 |
+--------------------+
5 rows in set (0.00 sec)

258 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

2 CONCAT(str1, Returns the string a) mysql> SELECT CONCAT


('My', 'S', 'QL');
str2,...) that results
Result: 'MySQL'
from
b) mysql> SELECT CONCAT('Class',
concatenating NULL, 'XI');
the arguments. Result: NULL
May have one c) mysql> SELECT CONCAT(First_
Name,'',Last_Name) FROM Employee;
or more
Result:
arguments.
+---------------------------------+
| CONCAT(First_Name,' ',Last_Name) |
+---------------------------------+
| Amit Sharma |
| Deeksha Verma |
| Navkiran Ahluwalia |
| Mamta Sharma |
| Bhawna Ahlurkar |
+---------------------------------+
5 rows in set (0.00 sec)

3 INSTR Returns the a) mysql> SELECT INSTR


('Informatics', 'for');
(str,substr) position of the first
Result: 3
occurrence of b) mysql> SELECT INSTR
substring substr in ('Computers', 'pet');

string str. Result: 0


c) mysql> SELECT INSTR
(First_Name,'Kiran') FROM
Employee;
Result:
+---------------------------+
| INSTR(First_Name,'Kiran') |
+---------------------------+
| 0 |
| 0 |
| 4 |
| 0 |
| 0 |
+---------------------------+
5 rows in set (0.00 sec)

INFORMATICS PRACTICES 259


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

4 LOWER(str) Returns the a) mysql> SELECT LOWER


('INFORMATICS');
argument (str) in
or Result: 'informatics'
lowercase i.e. it
LCASE(str) b) mysql> SELECT LOWER(Last_Name)
changes all the FROM Employee;
characters of the Result:
passed string to +------------------+
lowercase. | LOWER(Last_Name) |
+------------------+
| sharma |
| verma |
| ahluwalia |
| sharma |
| ahlurkar |
+------------------+
5 rows in set (0.00 sec)

5 UPPER(str) Returns the a) mysql> SELECT UPPER


('Informatics');
argument in
or Result: 'INFORMATICS'
uppercase. i.e. it
UCASE(str) b) mysql> SELECT UPPER(Last_Name)
changes all the FROM Employee;
characters of the Result:
passed string to +------------------+
uppercase. | UPPER(Last_Name) |
+------------------+
| SHARMA |
| VERMA |
| AHLUWALIA |
| SHARMA |
| AHLURKAR |
+------------------+
5 rows in set (0.00 sec)

260 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

6 LEFT(str,n) Returns the a) mysql> SELECT


LEFT('Informatics', 3);
specified number
Result: 'Inf'
of characters
(n)from the left
b) mysql>select
side of string str. LEFT(first_name,3)FROM Employee;
Result:
+--------------------+
| LEFT(first_name,3) |
+--------------------+
| Ami |
| Dee |
| Nav |
| Mam |
| Bha |
+--------------------+
5 rows in set (0.00 sec)

7 RIGHT(str,n) Returns the a) mysql> SELECT


RIGHT('Informatics', 4);
specified number
Result: 'tics'
of characters
b) mysql> select
(n)from the right RIGHT(first_name,3) FROM
side of string str. Employee;
Result:
+---------------------+
| RIGHT(first_name,3) |
+---------------------+
| mit |
| sha |
| ran |
| mta |
| wna |
+---------------------+
5 rows in set (0.00 sec)

INFORMATICS PRACTICES 261


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

8 LTRIM(str) Removes leading a) mysql> SELECT LTRIM


(' Informatics');
spaces i.e. removes
Result: 'Informatics'
spaces from the
b) mysql> SELECT LTRIM(First_Name)
left side of the FROM Employee;
string str. Result:
+-------------------+
| LTRIM(First_Name) |
+-------------------+
| Amit |
| Deeksha |
| Navkiran |
| Mamta |
| Bhawna |
+-------------------+
5 rows in set (0.00 sec)

9 RTRIM(str) Removes trailing a) mysql> SELECT RTRIM


('Informatics ');
spaces i.e. removes
Result: 'Informatics'
spaces from the
b) mysql> SELECT RTRIM(First_Name)
right side of the FROM Employee;
string str. Result:
+-------------------+
| RTRIM(First_Name) |
+-------------------+
| Amit |
| Deeksha |
| Navkiran |
| Mamta |
| Bhawna |
+-------------------+
5 rows in set (0.02 sec)

262 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

10 TRIM(str) Removes both a)mysql> SELECT TRIM(' Informatics


');
leading and
Result: 'Informatics'
trailing spaces
b) mysql> SELECT TRIM(First_Name)
from the string str. FROM Employee;
Result:
+------------------+
| TRIM(First_Name) |
+------------------+
| Amit |
| Deeksha |
| Navkiran |
| Mamta |
| Bhawna |
+------------------+
5 rows in set (0.00 sec)

11 SUBSTRING Returns the a) mysql> SELECT


SUBSTRING('Informatics',3);
(str,m,n) specified number
Result: 'formatics'
of characters from
Or b) mysql> SELECT
the middle of the SUBSTRING('Informatics' FROM 4);
MID(str,m,n) string. There are 3 Result: 'ormatics'
arguments. The c) mysql> SELECT
first argument is SUBSTRING('Informatics',3,4);

the source string. Result: 'form'

The second d) mysql> SELECT


SUBSTRING('Computers', -3);
argument is the
Result: 'ers'
position of first
e) mysql> SELECT
character to be
SUBSTRING('Computers', -5, 3);
displayed. The
Result: 'ute'
third argument is f) mysql> SELECT
the number of SUBSTRING('Computers' FROM -4
characters to be FOR 2);

displayed. Result: 'te'

INFORMATICS PRACTICES 263


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

If the third g) mysql> SELECT MID('Informatics',


3,4);
argument is
Result: 'form'
missing, then
h) mysql> select
starting from the MID(first_name,3,2) FROM
position specified, Employee;
the rest of the Result:
string is returned. +---------------------+
| MID(first_name,3,2) |
It is also possible
+---------------------+
to use a negative
| it |
value for the
| ek |
second argument
| vk |
ie. position (pos).
| mt |
In such a case, the
| aw |
beginning of the
+---------------------+
substring is pos
5 rows in set (0.00 sec)
characters from
the end of the
string,

Note: SUBSTR is
the same as
SUBSTRING

12 ASCII(str) Returns the ASCII a) mysql> SELECT ASCII('2');

value of the Result: 50

leftmost character (ASCII value of character '2')

of the string str. b) mysql> SELECT ASCII('dx');

Returns 0 if str is Result: 100

an empty string. (ASCII value of d)

Returns NULL if c) mysql> SELECT ASCII('A');

str is NULL. Result: 65


(ASCII value of 'A')

264 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

C) Date and Time Functions

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.

Sno Name & Syntax Description Example

1 CURDATE() Returns the current a) mysql> SELECT CURDATE();

date in YYYY-MM- Result: '2010-02-26'

DD format or
YYYYMMDD format,
depending on
whether the function
is used in a string or
numeric context.

2 NOW() Returns the current a) mysql> SELECT NOW();

date and time in Result: '2010-02-26 21:30:26'

'YYYY-MM-DD
HH:MM:SS' or
YYYYMMDDHHM
MSS.uuuuuu format,
depending on
whether the function
is used in a string or
numeric context.

3 SYSDATE() Returns the current a) mysql> SELECT SYSDATE();

date and time in Result: '2010-02-26 21:30:26'

'YYYY-MM-DD b) mysql> SELECT SYSDATE() + 0;

HH:MM:SS' or Result: 20100226213026.000000

YYYYMMDDHHM
MSS.uuuuuu format,
depending on
whether the function

INFORMATICS PRACTICES 265


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

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.

4 DATE(expr) Extracts the date part a) mysql> SELECT DATE('2010-02-26


01:02:03');
of a date or datetime
Result: '2010-02-26'
expression
b) mysql> SELECT DATE('2009-10-16
01:02:03')
Result: '2009-10-16'

5 MONTH(date) Returns the numeric a) mysql> SELECT MONTH('2010-02-


26');
month from the date
Result: 2
passed, in the range 0
b) mysql> select
to 12. It returns 0 for id,date_join,month(date_join)
dates such as '0000- from employee;
00-00' or '2010-00-00'
that have a zero
month part.

266 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

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)

6 YEAR(date) Returns the year a) mysql> SELECT YEAR('2010-02-26');


Result: 2010
for date passed in
b) mysql> SELECT id,date_join,year
the range 0 to
(date_join) from employee;
9999. Returns Result:
values like 1998, +----+------------+-----------------+
2010,1996 and so | id | date_join | year(date_join) |
on. +----+------------+-----------------+
| 1 | 1996-07-25 | 1996 |
| 2 | 1995-06-27 | 1995 |
| 3 | 1990-02-20 | 1990 |
| 4 | 1989-08-18 | 1989 |
| 5 | 2010-03-01 | 2010 |
+----+------------+-----------------+
5 rows in set (0.00 sec)

7 DAYNAME If you want to a) mysql> SELECT YEAR('2009-07-21');


Result:
(date) know which day
'Tuesday'
you were born on.
b) mysql> Select id,date_join,dayname
Was it a Monday (date_join) from employee;
or a Friday? Use
DAYNAME
function. It

INFORMATICS PRACTICES 267


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

returns the Result:


+----+------------+--------------------+
name of the
| id | date_join | dayname(date_join) |
weekday for the
+----+------------+--------------------+
date passed
| 1 | 1996-07-25 | Thursday |
| 2 | 1995-06-27 | Tuesday |
| 3 | 1990-02-20 | Tuesday |
| 4 | 1989-08-18 | Friday |
| 5 | 2010-03-01 | Monday |
+----+------------+--------------------+
5 rows in set (0.00 sec)

8 DAYOFMONT Returns the day a) mysql> SELECT DAYOFMONTH('2009-07-


21');
H(date) of the month in
Result: 21
the range 0 to
b) mysql> select
31. id,date_join,dayofmonth(date_join)
from employee;
Result:
+----+------------+-----------------------+
| id | date_join | dayofmonth(date_join) |
+----+------------+-----------------------+
| 1 | 1996-07-25 | 25 |
| 2 | 1995-06-27 | 27 |
| 3 | 1990-02-20 | 20 |
| 4 | 1989-08-18 | 18 |
| 5 | 2010-03-01 | 1 |
+----+------------+-----------------------+
5 rows in set (0.00 sec)

9 DAYOFWEEK Returns the day a) mysql> SELECT DAYOFWEEK('2009-07-


21');
(date) of week in
Result: 3
number as 1 for
b) mysql> select
Sunday, 2 for id,date_join,dayofweek(date_join)
Monday and so from employee;

on.

268 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

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)

10 DAYOFYEAR Return the day of a) mysql> SELECT DAYOFYEAR('2009-07-


21');
(date) the year for the
Result: 202
given date in
b) mysql> SELECT DAYOFYEAR('2009-01-
numeric format in 01');
the range 1 to 366. Result: 1
c) mysql> select
id,date_join,dayofyear (date_join)
from employee;
Result:
+----+------------+----------------------+
| id | date_join | dayofyear(date_join) |
+----+------------+----------------------+
| 1 | 1996-07-25 | 207 |
| 2 | 1995-06-27 | 178 |
| 3 | 1990-02-20 | 51 |
| 4 | 1989-08-18 | 230 |
| 5 | 2010-03-01 | 60 |
+----+------------+----------------------+
5 rows in set (0.00 sec)

INFORMATICS PRACTICES 269


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

Note
Difference between NOW() and SYSDATE()

(Sleep(argument) pauses for the number of seconds given by the argument.)

Even after a pause of 20 seconds


induced by sleep(20),now() shows
the same time ie. the time at which
the statement began to execute.

After 20 seconds induced by


sleep(20), sysdate() shows time
incremented by 20 seconds.

Summary
1. Functions perform some operations and return a value.

2. Single row functions operate on a single value to return a single value.

3. Multiple Row functions operate on a set of rows to return a single value.

4. Numeric functions perform operations on numeric values and return numeric


values.

5. String functions operate on character type data. They return either character or
numeric values.

6. Date and Time functions allow us to manipulate Date type data.

270 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

Multiple Choice Questions


1) ________________functions operate on a single value to return a single value
(a) Multiple Row
(b) Aggregate
(c) Single Row
(d) Summation
2) SUM, AVG,COUNT are examples of ____________________functions.
(a) Date
(b) String
(c) Multiple Row
(d) Single Row
3) SELECT POW(-3,2) will display the output:
(a) -6
(b) -9
(c) 9
(d) 6
4) SELECT TRUNCATE(7.956,2) will result in
(a) 7.95
(b) 7.96
(c) 8
(d) 8.0
5) INSTR(str,str2) returns the position of the first occurrence of
(a) Str in "MySQL"
(b) Str in str2
(c) str2 in str
(d) str2 in "SQL"

INFORMATICS PRACTICES 271


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

6) Any String function returns

(a) Only string

(b) Only number

(c) String or number

(d) String, number or date type data.

Answer the following questions.


1. Define a Function.

2. List 3 categories of single row functions. Give two examples in each category.

3. How are numeric functions different from String functions?

4. Which function is used to display the system date?

5. Which Date function displays the result like "Monday" or "Tuesday" etc.

6. Name a

i) date function that returns a number.

ii) String function that returns a number.

iii) date function that returns a date.

7. Write SQL statements to do the following:

a) Using the three separate words "We," "study," and "MySQL," produce the
following output:

"We study MySQL"

b) Use the string "Internet is a boon" and extract the string "net".

c) Display the length of the string "Informatics Practices".

d) Display the position of "My" in "Enjoying MySQL".

e) Display the name of current month.

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.

272 INFORMATICS PRACTICES


Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com

Chapter-10 Functions in MySQL

8. Write the output that the following statements will produce:


a) SELECT ROUND(7.3456, 2);
b) SELECT TRUNCATE(2.3456, 2);
c) SELECT DAYOFMONTH('2009-08-25');
d) SELECT MONTH('2010-02-26');
e) SELECT RIGHT('Informatics', 4);

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

1 Bindra Jaspreet 5B, Gomti Nagar Lucknow 3500.50

2 Rana Monica 21 A, Bandra Mumbai 2768.00

3 Singh Jatinder 8, Punjabi Bagh Delhi 2000.50

4 Arora Satinder K/1, Shere Punjab Mumbai 1900.00


Colony

5 Krishnan Vineeta A-75,Adarsh Nagar

(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

Chapter-10 Functions in MySQL

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

1 Rice 52.50 80 2010-02-01

2 Wheat 25.40 50 2010-03-09

3 Corn 50.80 100 2010-03-11

4 Semolina 28.90 50 2010-01-15

(Unit Price is per kg price)


I. Display Item name, unit price along with Date of purchase for all the Items.
II. Display Item name along with Month (in number) when it was purchased for
all the items.
III. Display Item name along with year in which it was purchased for all the items.
IV. Display Item Id, Date of Purchase and day name of week (e.g. Monday) on
which it was purchased for all the items.
V. Display names of all the items that were purchased on Mondays or Tuesdays.
VI. Display the day name of the week on which Rice was purchased.
VII. Display the Item name and unit price truncated to integer value (no decimal
digits)of all the items.
VIII. Display current date
274 INFORMATICS PRACTICES
Downloaded from www.studiestoday.com

You might also like