Lab04-Aggregate Functions, Group By, Numeric and String Functions
Lab04-Aggregate Functions, Group By, Numeric and String Functions
GROUPING: The GROUP BY clause is a SQL command that is used to group rows that have
the same values.
ORDER BY CLAUSE:
Use the ORDER BY clause to display the output table of a query in either ascending or
descending alphabetical order. Whereas the GROUP BY clause gathers rows into groups and
sorts the groups into alphabetical order, ORDER BY sorts individual rows.
Example 2: All columns in the SELECT list that are not in the group functions must be in the
GROUP BY clause.
Illegal
SELECT emp_dept, job, MAX(income)
FROM employee
GROUP BY emp_dept;
Legal
SELECT emp_dept, job, MAX(income)
FROM employee
GROUP BY emp_dept, job;
Illegal
SELECT emp_dept, MAX(income)
FROM employee
WHERE MAX(income) >2000
GROUP BY emp_dept;
Legal
SELECT emp_dept, MAX(income)
FROM employee
GROUP BY emp_dept
HAVING MAX(income) >2000;
EXERCISES
Exercise 1:
#Create table employee with the following constraints;
CREATE TABLE employee(emp_id, emp_name ,emp_dept emp_age, place,
income); Set emp_id as the primary key with auto increment starting from 2505.
# insert the below given records into the table employee table
Load “employee.csv”(using the mysql workbench, easy) data into the employee table.
Questionnaire set:
Exercise2:
Questionnaire set:
1. Create the tables for above schema and load data from the respective .csv files 2. For all
customers who have loan from the bank,find their names,loan numbers and loan amount(with
and without renaming tables)
3. Find the customer names,loan numbers and loan amounts for all loans at perryridge
branch.
4. Find the names of all branches that have assets greater than at least one branch located at
Brooklyn.
5. List in alphabetical order all customers who have loans at the perryridge branch. 6. Print
the entire Loan relation in descending order of amount.If several loans have the same
amount,order them in ascending order by loan number.
7. Find the average balance for all accounts.
8. Find no.of tuples in customer relation.
9. Find the total of all loan amounts.
10. Find the average account balance at the Perryridge branch.
11. Find the average account balance at each branch.
12. Find the average account balance at each branch ,where the account balance is more than
1200.
13. Find the number of depositors for each branch.
14. Find the average balance for each customer who lives in ‘’Harrison” and has at least 3
accounts
LOWER (string/ COLUMNNAME): Returns string with all characters in lower case.
INITCAP (string/ COLUMNNAME): Returns string with first letter of the word capitalized.
LENGTH (string/ COLUMNNAME): Returns the length of the string measured in characters.
REVERSE (str): Returns the string str with the order of the characters reversed.
INSTR (str,substr): Returns the position of the first occurrence of substring substr in string
str. SUBSTR (str,pos): It returns a substring from string str starting at position pos.
SUBSTR (str,pos,len): It returns substring len characters long from string str, starting at position pos.
REPLACE(str,from_str,to_str): Returns the string str with all occurrences of the string from_str
replaced by the string to_str. REPLACE performs a case-sensitive match when searching for from_str.
LPAD (str,len,padstr): Returns the string str, left-padded with the string padstr to a length of len
characters. If str is longer than len, the return value is shortened to len characters.
RPAD (str,len,padstr): Returns the string str, right-padded with the string padstr to a length of len
characters. Ifstr is longer thanlen, the return value is shortened to len characters.
LTRIM (str): Returns the string str with leading space characters removed.
RTRIM (str): Returns the string str with trailing space characters removed.
TRIM ([{BOTH | LEADING | TRAILING} [ch] FROM] str): Returns the string str with all characters ch
prefixes or suffixes removed.
ADD_MONTHS (DATE,n): Returns date after adding the number of months specified in the
MONTHS_BETWEEN (DATE1, DATE2): Returns the number of months between the two dates.
NEXT_DAY (DATE, WEEKDAY): Returns the day of the week specified by WEEKDAY after
date.
Exercise-3
1) Display your name with the first letter being capital, where the entered name is in lower
case. 2) Display 2nd- 6th characters of your name.
3) Find the length of your full institute name.
4) Display all the Employee names with its first letter in upper case.
5) List the department name of each employee as a three letter code.
6) Display the month of the joining of each employee.
7) Display the date of joining of each employee in dd/mm/yy format.
8) Display the experience of each employee in terms of months.
9) Display the experience of each employee in terms of years and months.
10) Display the date of the next Friday after today's date.
11) Display the day of joining of each employee.
12) Display the date corresponding to 15 days after today's date.
13) Display the value 94204.27348 truncated up to 2 digits after the decimal point.
14) Display the value of the expression 5 + 89
15) Find out the square root of 6464312.