Create the following table named "Charity" and write SQL queries for the tasks that follow:
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.
Answers
Answer:
1. select lcase(firstname) from charity;
2. select ucase(lastname) from charity where city='mumbai'
I. Select Lcase(FirstName) from Charity;
II. Select Ucase(LastName) from Charity where City = 'Mumbai';
III. Select P_ID, left(FirstName, 3) from Charity;
IV. Select concat(FirstName, LastName) from Charity;
V. Select length(Address), P_ID from Charity;
VI. Select right(P_ID, 2), right(Address, 2) from Charity;
VII. Select FirstName, LastName from Charity where instr(FirstName, 'at') in (2, 3);
VIII. Select instr(LastName, 'a') from Charity;
IX. Select FirstName, LastName from Charity where FirstName like '%a';
X. Select trim(concat(FirstName, LastName)) from Charity;
XI. Select P_ID, LastName, round(Contribution) from Charity;
XII. Select P_ID, LastName, truncate(Contribution) from Charity;
XIII. Select LastName, Contribution, round(Contribution/10, 2) from Charity;
- lcase() is used to convert the text to lowercase.
- ucase() is used to convert the text to uppercase.
- left() is used to retrieve the specified number of characters from the left of the data.
- concat() is used to add two or more values.
- length() is used to display the number of characters present in the specified argument.
- right() is used to retrieve the specified number of characters from the right of the data.
- instr() is used to display the position of the specified substring within a string.
- trim() is used to remove the leading and trailing spaces from the data.
- round() is used to estimate decimal values.
- truncate() is used to remove the decimal occurrences.