0% found this document useful (0 votes)
9 views17 pages

10 - MySQL Function

The document provides notes on functions in MySQL, categorizing them into single row functions and multiple row (aggregate) functions. It details various numeric, string, and date/time functions, including examples of how to use them. Additionally, it explains the differences between certain functions and includes questions and answers related to the content covered.

Uploaded by

hagodi8367
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)
9 views17 pages

10 - MySQL Function

The document provides notes on functions in MySQL, categorizing them into single row functions and multiple row (aggregate) functions. It details various numeric, string, and date/time functions, including examples of how to use them. Additionally, it explains the differences between certain functions and includes questions and answers related to the content covered.

Uploaded by

hagodi8367
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

Function in MySQL-I -Class Notes, 2016-17

8. Functions in MySQL

1. What do you meant by Function in MySQL?


Ans. A Function in MySQL is a special type of pre-defined command set that
performs some operation and returns a single value.
2. Write the two Categories of Function in MySQL.
Ans. Function can be categories in two ways-

 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 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(), MIN(), MAX() and COUNT().
(Note : Multiple Row functions will be discussed in detail in Class XII)
3. Write various Numeric Function in MySQL.
Ans. i) POWER(m,n) or POW(m,n) – Returns the value of m raised to the power of n.
Eg. 32=9

SELECT POW(2,4);
Result: 16

SELECT POW(2,-2);
Result: 0.25

SELECT POW(-2,3);
Result: -8

SELECT id, salary, POWER(salary,2) FROM employee;


Result:

ii) SQRT(n) – Returns square root of the given number.


Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 96
Function in MySQL-I -Class Notes, 2016-17

SELECT SQRT(26);
Result: 5.09901951

SELECT SQRT(25);
Result: 5

iii) MOD(m,n) – Returns modulus (i.e. remainder) of the given two number.

SELECT MOD(11,4);
Result: 3

SELECT MOD(25,5);
Result: 0

iv) SIGN(n) – Returns sign of the given number. If n<0, returns -1; If n=0,
returns 0; If n>0, returns 1

SELECT SIGN(-11);
Result: -1

SELECT SIGN(25);
Result: 1

v) ROUND(X,D) or ROUND(X) – This function returns a number rounded off


as per given specification:

a) Rounds the argument X to D decimal places.

SELECT ROUND(6.298,1);
Result: 6.3

SELECT ROUND(-1.58);
Result: -2

b) If number of decimal places is not specified or is zero, the number rounds


to the nearest integer OR (0 decimal places).

SELECT ROUND(-1.23);
Result: -1

SELECT ROUND(-1.58);
Result: -2

SELECT ROUND(1.43);
Result: 1

c) If negative value is specified for precision, it counts off that value left from
the decimal point to nearest ten’s/hundred’s etc.

SELECT ROUND(56.235,-1);

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 97
Function in MySQL-I -Class Notes, 2016-17

Result: 60

SELECT ROUND(55.235,-1);
Result: 60

SELECT ROUND(54.235,-1);
Result: 50

SELECT ROUND(54.235,-2);
Result: 100

SELECT ROUND(500.35,-3);
Result: 1000

vi) TRUNCATE(X,D) – This function just truncates the digits without


rounding them off.
Note: TRUNCATE does not round a number. It simply chops off digits from a
number.

a) Returns the number X, truncated to D decimal places.

SELECT TRUNCATE(7.543,1);
Result: 7.5

b) If D is 0, the result has no decimal point or fractional part.

SELECT TRUNCATE(4.567,0);
Result: 4

c) If D is negative, it causes D digits left of the decimal point of the value X to


become zero.
SELECT TRUNCATE(346,-2);
Result: 300

4. Write various String Function in MySQL.


Ans. 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.
Note : ASCII values -
65 to 90  A-Z
97 to 122  a-z
48 to 57  0-9

i) ASCII() – Returns the ASCII value of the leftmost character of the string
str. Returns 0 if str is an empty string. Returns NULL if str is NULL.

SELECT ASCII(„2‟);
Result: 50

SELECT ASCII('dx');

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 98
Function in MySQL-I -Class Notes, 2016-17

Result: 100

SELECT ASCII('A');
Result: 65

SELECT ASCII('');
Result: 0

ii) CHAR() – This function returns the character for each integer passed.

SELECT CHAR(70,65,67,69);
Result :
+--------------------+
| char(70,65,67,69) |
+--------------------+
| FACE |
+--------------------+

iii) CONCAT(str1,str2) – Returns the string that results from concatenating


the arguments. May have one or more arguments.

SELECT CONCAT('My', 'S', 'QL');


Result: 'MySQL'

SELECT CONCAT('Class„,NULL,'XI');
Result: NULL

SELECT CONCAT(FirstName,'',LastName)FROM Employee;


Result:

iv) LOWER(str) or LCASE(str) – Returns the argument (str) in lowercase i.e.


it changes all the characters of the passed string to lowercase.

SELECT LOWER('INFORMATICS');
Result: 'informatics'

SELECT LOWER(Last_Name)FROM Employee;


Result:

v) UPPER(str) or UCASE(str) – Returns the argument in uppercase i.e. it


changes all the characters of the passed string to uppercase.

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 99
Function in MySQL-I -Class Notes, 2016-17

SELECT UPPER('informatics');
Result: 'INFORMATICS'

SELECT UPPER(Last_Name)FROM Employee;


Result:

vi) SUBSTRING(str,m,n) or SUBSTR(str,m,n) or MID(str,m,n) – Returns


the specified number of characters from the middle of the string. There are
3 arguments.
str is the source string.
m is the position of first character to be displayed. If m is negative, counts
backwards from the end of str.
n is the number of characters to be displayed. If the n is missing, then
starting from the position specified, the rest of the string is returned. If n is
less then 1, a null is returned.
SELECT SUBSTRING('Informatics',3);
Result: 'formatics'

SELECT SUBSTRING('Informatics',-3);
Result: ‘ics'

SELECT SUBSTRING('Informatics' FROM 4);


Result: 'ormatics'

SELECT SUBSTRING('Informatics',3,4);
Result: 'form'

SELECT SUBSTRING('Computers', -5, 3);


Result: 'ute'

SELECT SUBSTRING('Computers' FROM -4 FOR 2);


Result: 'te'

SELECT MID('Informatics„,3,4);
Result: 'form'

Select MID(first_name,3,2) FROM Employee;


Result :

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 100
Function in MySQL-I -Class Notes, 2016-17

vii) LEFT(str,len) – Returns the specified number of characters (len) from the
left side of string str.

SELECT LEFT('Informatics', 3);


Result: 'Inf'

SELECT LEFT(first_name,3)FROM Employee;


Result:

viii) RIGHT(str,len) – Returns the specified number of characters (len) from the
right side of string str.

SELECT RIGHT('Informatics', 3);


Result: ‘ics'

SELECT RIGHT(first_name,3)FROM Employee;


Result:

ix) LENGTH(str) – Returns the length of a column or a string in bytes


including whitespaces.

SELECT LENGTH('Informatics Practices');


Result: 21

SELECT LENGTH(First_Name)FROM Employee;


Result:

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 101
Function in MySQL-I -Class Notes, 2016-17

x) INSTR(str1,str2) – This function searches for given second string into the
given first string and returns the position number of the first occurrence of
substring str2 in string str1.

SELECT INSTR ('Informatics', 'for');


Result: 3

SELECT INSTR ('Computers', 'pet');


Result: 0

SELECT INSTR („Corporate Floor', „or');


Result: 2

SELECT INSTR (First_Name,'Kiran')FROM Employee;


Result : 4

xi) LTRIM(str) – Removes leading spaces i.e. removes spaces from the left side
of the string str.

SELECT LTRIM (' Informatics');


Result: 'Informatics'

SELECT LTRIM(First_Name)FROM Employee;


Result:

xii) RTRIM(str) – Removes trailing spaces i.e. removes spaces from the right
side of the string str.

SELECT RTRIM („Informatics ');


Result: 'Informatics'

SELECT RTRIM(First_Name)FROM Employee;


Result:

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 102
Function in MySQL-I -Class Notes, 2016-17

xiii) TRIM(str) – Removes both leading and trailing spaces from the string str.

SELECT TRIM („ Informatics ');


Result: 'Informatics'

SELECT TRIM(First_Name)FROM Employee;


Result:

5. Write the various Date/Time function in MySQL.


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

i) CURDATE() or CURRENT_DATE() or CURRENT_DATE - Returns


the current date in YYYY-MM-DD format or YYYYMMDD format,
depending on whether the functions used in a string or numeric context.

SELECT CURDATE();
Result: '2013-11-24'

ii) NOW() - Returns the current date and time in 'YYYY-MM-DD HH:MM:SS'
or YYYYMMDDHHM [Link] format, depending on whether the
function is used in a string or numeric context.

SELECT NOW();
Result: 2013-11-24 [Link]

iii) SYSDATE() - Returns the current date and time in 'YYYY-MM-DD


HH:MM:SS' or [Link] format, depending on
whether the function is used in a string or numeric context.
SYSDATE() returns the time at which the function executes.

SELECT SYSDATE();
Result: 2013-11-24 [Link]

iv) DATE(datetime) - Extracts the date part of a date or datetime expression.

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 103
Function in MySQL-I -Class Notes, 2016-17

SELECT DATE('2010-02-26 [Link]');


Result: '2010-02-26'

v) MONTH(date) - Returns the numeric month from the date passed, in the
range 0 to 12. It returns 0 for dates such as '0000-00-00' or '2010-00-00‘
that have a zero month part.

SELECT MONTH('2010-02-26');
Result: 2

vi) YEAR(date) - Returns the year for date passed in the range 0 to 9999.
Returns values like 1998, 2010, 1996 and so on.

SELECT YEAR('2013-11-24');
Result: 2013

vii) DAYNAME(date) - returns the name of the weekday for the date passed

SELECT DAYNAME('2009-07-21');
Result: 'Tuesday'

viii) DAYOFMONTH(date) - Returns the day of the month in the range 0 to


31.

SELECT DAYOFMONTH('2010-02-26');
Result: 26

ix) DAYOFWEEK(date) - Returns the day of week in number as 1 for


Sunday, 2 for Monday and so on.

SELECT DAYOFWEEK('2013-11-24');
Result: 1

x) DAYOFYEAR(date) - Return the day of the year for the given date in
numeric format in the range 1 to 366.

SELECT DAYOFYEAR('2013-11-24');
Result: 328

6. How the SYSDATE() is differ from NOW() date function in MySQL.

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

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 104
Function in MySQL-I -Class Notes, 2016-17

NCERT Text book Question and Answer

1. Define a Function.
Ans. A Function in MySQL is a special type of pre-defined command set that
performs some operation and returns a single value.
2. List 3 categories of single row functions. Give two examples in each category.
Ans. Single row functions operate on a single value to return a single value.
The three categories of Single Row function are –
i) Numeric functions – Eg. pow(), sqrt(), mod(), round(), truncate()
ii) String functions – Eg. ASCII(), CHAR(), Substring(), INSTR() etc
iii) Date and Time functions – Eg. CURDATE(), NOW(), SYSDATE(), DATE()

3. How are numeric functions different from String functions?


Ans. Numeric functions perform operations on numeric values and return numeric values.
String functions operate on character type data. They return either character or numeric
values
4. Which function is used to display the System Date?
Ans. CURDATE()
5. Which Date function displays the result like "Monday" or "Tuesday" etc.
Ans. DAYNAME(date)
6. Name a
i) Date function that returns a number.
ii) String function that returns a number.
iii) Date function that returns a date.
Ans. i) month(date), year(date), dayofmonth(date), dayofweek(date) &
dayofyear(date)
ii) instr(str1, str2), length(str), ASCII()
iii) curdate()
7. Write SQL statements to do the following:
i) Using the three separate words "We," "study," and "MySQL," produce the following
output:
"We study MySQL"
Ans. SELECT CONCAT('My', 'S', 'QL');
ii) Use the string "Internet is a boon" and extract the string "net".
Ans. SELECT SUBSTRING('Internet is boon',6,3);
iii) Display the length of the string "Informatics Practices".
Ans. SELECT LENGTH('Informatics Practices‟);
Output:
21
iv) Display the position of "My" in "Enjoying MySQL".
Ans. SELECT INSTR ('Enjoying MySQL', 'My');
Output:
10
v) Display the name of current month.
Ans. SELECT MONTH(CURDATE());

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 105
Function in MySQL-I -Class Notes, 2016-17

vi)
Display the date 10 years from now. Label the column "Future."
Ans. SELECT YEAR(CURDATE())+10 AS „FUTURE‟;
vii) Display the day of week on which your birthday will fall or fell in 2010.
Ans. SELECT DAYOFWEEK('2010-11-24');
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);
Ans. a) 7.35
b) 2.34
c) 25
d) 2
e) tics

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 Jaspeet 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 Mumbai 1900.00
Punjab Colony
5 Krishnan Vineeta A-75,Adarsh
Nagar
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.
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.

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 106
Function in MySQL-I -Class Notes, 2016-17

Ans. SQL command to creating table “Charity”-


CREATE TABLE CHARITY
(
P_ID int Primary Key,
LastName varchar(30),
FirstName varchar(30),
Address varchar(50),
City varchar(20),
Contribution decimal(10,2)
);
SQL command to Inserting data to the table “Charity”-

INSERT INTO charity VALUES


(1,‟Bindra‟,‟Jaspeet‟,‟5B,Gomti Nagar‟,‟Lucknow‟,3500.50);

INSERT INTO charity VALUES


(2,‟Rana‟,‟Monica‟,‟21 A, Bandra‟,‟Mumbai‟,2768.00);

INSERT INTO charity VALUES


(3,‟Singh‟,‟Jatinder‟,‟8, Punjabi Bagh‟,‟Delhi‟,2000.50);

INSERT INTO charity VALUES


(4,‟Arora‟,‟Satinder‟,‟K/1, Shere Punjab Colony‟,‟Mumbai‟,1900.00);

INSERT INTO charity VALUES


(5,‟Krishnan‟,‟Vineeta‟,‟A-75, Adarsh Nagar‟,null,null);

SQL Command to Display all record from table Charity


SELECT * FROM Charity;
i) Display all first names in lowercase.
SELECT Lower(FirstName) FROM Charity;
ii) Display all last names of people of Mumbai city in uppercase.
SELECT Upper(LastName) FROM Charity WHERE City like “Mumbai”;
iii) Display Person ID along with First 3 characters of his/her name.
SELECT P_ID, LEFT(FirstName,3) FROM Charity;
iv) Display first name concatenated with last name for all the employees.
SELECT Concat(FirstName,” “,LastName) FROM Charity;
v) Display length of address along with Person ID.
SELECT P_ID, Length(Address) FROM Charity;
vi) Display last 2 characters of City and Person ID.
SELECT P_ID, Right(City,2) FROM Charity;
vii) Display Last Names and First names of people who have "at" in the second or
third position in their first names.
SELECT FirstName, LastName FROM Charity
WHERE INSTR(FirstName,‟at‟) IN (2,3);
viii) Display the position of ‘a’ in Last name in every row.
SELECT INSTR(LastName,‟a‟) FROM Charity ;

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 107
Function in MySQL-I -Class Notes, 2016-17

ix) Display Last Name and First name of people who have "a" as the last character
in their First names.
SELECT LastName,FirstName FROM Charity
WHERE FirstName LIKE “%a”;
x) Display the first name and last name concatenated after removing the leading
and trailing blanks.
SELECT Concat(TRIM(FirstName),TRIM(LastName)) FROM Charity;
xi) Display Person Id, last names and contribution rounded to the nearest rupee of
all the persons.
SELECT P_ID, LastName, ROUND(Contribution,-3) FROM Charity;
xii) Display Person Id, last name and contribution with decimal digits truncated of
all the persons.
SELECT P_ID, LastName, TRUNCATE(Contribution,0) FROM Charity;
xiii) Display Last name, contribution and a third column which has contribution
divided by 10. Round it to two decimal points.
SELECT P_ID, LastName, Contribution,
ROUND((Contribution/10),2) FROM Charity;

2. Consider the table "Grocer" and write SQL queries for the tasks that follow:
Table: Grocer

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
Ans. SQL command to creating table “Grocer”-

CREATE TABLE Grocer


(
Item_ID integer PRIMARY KEY,
ItemName varchar(20),
UnitPrice decimal(10,2),
Quantity integer,

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 108
Function in MySQL-I -Class Notes, 2016-17

Date_Purchase date
);

SQL command to Inserting data to the table “Grocer”-

INSERT INTO Grocer VALUES (1,‟Rice‟,52.50,80,‟2010-02-01‟);


INSERT INTO Grocer VALUES (2,‟Wheat‟,25.40,50,‟2010-03-09‟);
INSERT INTO Grocer VALUES (3,‟Corn‟,50.80,100,‟2010-03-11‟);
INSERT INTO Grocer VALUES
(4,‟Semolina‟,28.90,50,‟2010-01-15‟);

i) Display Item name, unit price along with Date of purchase for all the Items.
SELECT ItemName, UnitPrice, Date_Purchase FROM Grocer;
ii) Display Item name along with Month (in number) when it was purchased for all
the items.
SELECT ItemName, Month(Date_Purchase) FROM Grocer;
iii) Display Item name along with year in which it was purchased for all the items.
SELECT ItemName, YEAR(Date_Purchase) FROM Grocer;
iv) Display Item Id, Date of Purchase and day name of week (e.g. Monday) on which
it was purchased for all the items.
SELECT ItemName, Date_Purchase, Dayname(Date_Purchase) FROM Grocer;
v) Display names of all the items that were purchased on Mondays or Tuesdays.
SELECT * FROM Grocer
WHERE Dayname(Date_Purchase) IN („Monday‟,‟Tuesday‟);
vi) Display the day name of the week on which Rice was purchased.
SELECT ItemName, dayname(Date_Purchase) FROM Grocer
WHERE ItemName like „Rice‟;

vii) Display the Item name and unit price truncated to integer value (no decimal
digits) of all the items.
SELECT ItemName, Truncate(UnitPrice) FROM Grocer;

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 109
Function in MySQL-I -Class Notes, 2016-17

LAB EXERCISE (Try Your-Self)

Consider a database LOANS with the following table:

Write SQL commands for the tasks 1 to 35 and write the output for the SQL
commands 36 to 40:

Create Database and use it


1. Create the database LOANS.
2. Use the database LOANS.

Create Table / Insert Into


3. Create the table Loan_Accounts and insert tuples in it.

Simple Select
4. Display the details of all the loans.
5. Display the AccNo, Cust_Name, and Loan_Amount of all the loans.

Conditional Select using Where Clause


6. Display the details of all the loans with less than 40 instalments.
7. Display the AccNo and Loan_Amount of all the loans started before 01-04-2009.
8. Display the Int_Rate of all the loans started after 01-04-2009.

Using NULL
9. Display the details of all the loans whose rate of interest is NULL.
10. Display the details of all the loans whose rate of interest is not NULL.

Using DISTINCT Clause


11. Display the amounts of various loans from the table Loan_Accounts. A loan
amount should appear only once.
12. Display the number of instalments of various loans from the table
Loan_Accounts. An instalment should appear only once.

Using Logical Operators (NOT, AND, OR)


13. Display the details of all the loans started after 31-12-2008 for which the number
of instalments are more than 36.

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 110
Function in MySQL-I -Class Notes, 2016-17

14. Display the Cust_Name and Loan_Amount for all the loans which do not have
number of instalments 36.
15. Display the Cust_Name and Loan_Amount for all the loans for which the loan
amount is less than 500000 or int_rate is more than 12.
16. Display the details of all the loans which started in the year 2009.
17. Display the details of all the loans whose Loan_Amount is in the range 400000 to
500000.
18. Display the details of all the loans whose rate of interest is in the range 11% to
12%.

Using IN Operator
19. Display the Cust_Name and Loan_Amount for all the loans for which the number
of instalments are 24, 36, or 48. (Using IN operator)
Using BETWEEN Operator
20. Display the details of all the loans whose Loan_Amount is in the range 400000 to
500000. (Using BETWEEN operator)
21. Display the details of all the loans whose rate of interest is in the range 11% to
12%. (Using BETWEEN operator)

Using LIKE Operator


22. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with 'Sharma'.
23. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with 'a'.
24. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a'
25. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name does not contain 'P'.
26. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a' as the second last character.

Using ORDER BY clause


27. Display the details of all the loans in the ascending order of their Loan_Amount.
28. Display the details of all the loans in the descending order of their Start_Date.
29. Display the details of all the loans in the ascending order of their Loan_Amount
and within Loan_Amount in the descending order of their Start_Date.

Using UPDATE, DELETE, ALTER TABLE


30. Put the interest rate 11.50% for all the loans for which interest rate is NULL.
31. Increase the interest rate by 0.5% for all the loans for which the loan amount is
more than 400000.
32. For each loan replace Interest with (Loan_Amount*Int_Rate*Instalments)
12*100.
33. Delete the records of all the loans whose start date is before 2007.
34. Delete the records of all the loans of 'K.P. Jain'
35. Add another column Category of type CHAR(1) in the Loan table.

Find the Output of the following queries


36. SELECT cust_name, LENGTH(Cust_Name), LCASE(Cust_Name),
UCASE(Cust_Name) FROM Loan_Accounts WHERE Int_Rate < 11.00;

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 111
Function in MySQL-I -Class Notes, 2016-17

37. SELECT LEFT(Cust_Name, 3), Right(Cust_Name, 3), SUBSTR(Cust_Name, 1, 3)


FROM Loan_Accounts WHERE Int_Rate > 10.00;
38. SELECT RIGHT(Cust_Name, 3), SUBSTR(Cust_Name, 5) FROM
Loan_Accounts;
39. SELECT DAYNAME(Start_Date) FROM Loan_Accounts;
40. SELECT ROUND(Int_Rate*110/100, 2) FROM Loan_Account
WHERE Int_Rate > 10;

Write the output produced by the following SQL commands:


41. SELECT POW(4,3), POW(3,4);
42. SELECT ROUND(543.5694,2), ROUND(543.5694), ROUND(543.5694,-1);
43. SELECT TRUNCATE(543.5694,2), TRUNCATE(543.5694,-1);
44. SELECT LENGTH("Prof. M. L. Sharma");
45. SELECT CONCAT("SHEIKH", " HAROON") "FULL NAME";
46. SELECT YEAR(CURDATE()), MONTH(CURDATE()), DAY(CURDATE());
47. SELECT DAYOFYEAR(CURDATE()), DAYOFMONTH(CURDATE()),
DAYNAME(CURDATE());
48. SELECT LEFT("Unicode",3), RIGHT("Unicode",4);
49. SELECT INSTR("UNICODE","CO"), INSTR("UNICODE","CD");
50. SELECT MID("Informatics",3,4), SUBSTR("Practices",3);

Prepared by : Mr. Taposh Karmakar, Air Force School Jorhat. Mobile – 78961 63247. Last Updated : Saturday, November 19, 2016 112

You might also like