0% found this document useful (0 votes)
21 views4 pages

MySQL Functions and Queries Guide

The document contains multiple choice questions and SQL exercises related to functions in SQL, including single row and aggregate functions. It also includes tasks to create and manipulate tables, such as 'Charity' and 'Grocer', with specific SQL queries to extract and format data. Additionally, it covers various string and date functions, providing examples and expected outputs.

Uploaded by

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

MySQL Functions and Queries Guide

The document contains multiple choice questions and SQL exercises related to functions in SQL, including single row and aggregate functions. It also includes tasks to create and manipulate tables, such as 'Charity' and 'Grocer', with specific SQL queries to extract and format data. Additionally, it covers various string and date functions, providing examples and expected outputs.

Uploaded by

mansipanda.29
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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
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
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
1Bindra Jaspreet 5B, Gomti Nagar
Lucknow 3500.50
2Rana Monica 21 A, Bandra Mumbai 2768.00
3Singh Jatinder 8, Punjabi BaghDelhi 2000.50
4Arora Satinder K/1, Shere Mumbai 1900.00
Punjab Colony
5Krishnan 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.
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.

Common questions

Powered by AI

You can extract the substring 'net' from 'Internet is a boon' using the SQL SUBSTRING() function. The query would be: SELECT SUBSTRING('Internet is a boon', 4, 3); This command starts at the 4th character of 'Internet is a boon' and retrieves 3 characters thereafter, extracting the substring 'net' .

The system's current date can be retrieved using the CURDATE() function in SQL. To display the day of the week, you can use the DAYNAME() function, which returns the name of the day for a given date .

The typical categories of single-row functions in SQL include Numeric, String, and Date functions. Numeric functions perform arithmetic operations (e.g., ROUND, TRUNCATE), string functions manipulate text (e.g., CONCAT, LENGTH), and date functions process temporal data (e.g., DAYOFMONTH, MONTH). Each category serves different purposes: numeric functions handle numbers, string functions work with text, and date functions manipulate date formats, making SQL a versatile language for data processing .

The INSTR function in SQL is used to find the position of a substring within a string. It returns the first occurrence of the substring. The SQL statement would be structured as: SELECT INSTR('MainString', 'SubString'); For example, SELECT INSTR('Enjoying MySQL', 'My'); would return the starting position of 'My' in the string 'Enjoying MySQL' .

To round contributions to the nearest rupee, use the ROUND function: SELECT ROUND(Contribution, 0) FROM Charity;. To truncate, use TRUNCATE: SELECT TRUNCATE(Contribution, 0) FROM Charity;. Rounding adjusts the number to the nearest whole number based on decimal value (rounding up if 0.5 or above), while truncation removes the decimal part without adjustment, thus always rounding down .

Numeric functions operate on numeric data types and return numeric values, performing mathematical calculations such as ROUND, TRUNCATE, or POW. In contrast, string functions manipulate string data to return strings or numeric values representing string information, such as LENGTH or INSTR. This differentiation lies in the type of data they operate on and the nature of the result .

To display the current month's name, you can use the MONTHNAME() function. For the date 10 years from now, you can use the DATE_ADD() function. The query would look like: SELECT MONTHNAME(CURDATE()) AS CurrentMonth, DATE_ADD(CURDATE(), INTERVAL 10 YEAR) AS Future; This query provides the current month and computes the date 10 years from today, presenting it under the 'Future' column .

To display all first names in lowercase from a table named 'Charity', you can use the SQL LOWER() function. The SQL query would be: SELECT LOWER(FirstName) FROM Charity; This query selects the FirstName field from the 'Charity' table and converts each first name to lowercase before displaying it .

To retrieve items purchased on specific days like Monday or Tuesday, use the DAYNAME function in conjunction with a WHERE clause. For example, the query SELECT ItemName FROM Grocer WHERE DAYNAME(Date_Purchase) IN ('Monday', 'Tuesday'); fetches item names with purchases on either Monday or Tuesday by comparing the day's name extracted from 'Date_Purchase' .

To display the length of addresses along with the Person ID, you can use the SELECT statement with the LENGTH function: SELECT P_Id, LENGTH(Address) FROM Charity;. This query retrieves the Person ID and the length of each corresponding address in the 'Charity' table .

You might also like