You are on page 1of 11

Math/Numeric Functions

• Power()/pow()
• Round()
• Truncate()
• Mod()
• Sqrt()
String Functions
• Ucase()/Upper()
• Lcase()/Lower()
• Length()
• Trim()
• Ltrim()
• Rtrim()
• Left()
• Right()
• Mid()/Substring()/Substr()
• Instr()
Date Functions
• Now()
• Date()
• Month()
• MonthName()
• Year()
• Day()
• DayName()
ORDER BY, GROUP BY, HAVING
SORTING OF RECORDS
• ORDER BY:- Used to arrange the records in
ascending/descending order.
• Eg:-
• SELECT * FROM STUDENT ORDER BY MARKS;
OR

• SELECT * FROM STUDENT ORDER BY MARKS ASC;


• Keywords ASC and DESC denotes the order.
• By default ORDER BY clause sorts the result set in
ascending order.
Ordering Data on Multiple Columns

• We can specify the column names in order by


clause along with desired order.
• SELECT * FROM STUDENT ORDER BY MARKS
ASC, NAME DESC;
• The above statement will sort the records
firstly on the column MARKS and then on the
basis of descending order of column NAME.
GROUP BY
• GROUP BY clause:-It combines all those records that have identical
values in a particular field or a group of fields.
• Eg:- SELECT COUNT(ROLLNO) FROM STUDENT;  7
• SELECT COUNT(ROLLNO), STREAM from student GROUP BY STREAM;
 COUNT(rollno) STREAM
3 SCIENCE
2 COMMERCE
2 HUMANITIES
• SELECT COUNT(*), SUM(marks) FROM STUDENT GROUP BY STREAM;
• COUNT(*) SUM(MARKS)
3 240.00
2 180.00
2 150.00
HAVING clause
• HAVING:- Used to give conditions on groups
• WHERE:- Used to give condition on individual
rows.
• In WHERE conditions cannot include aggregate
functions so we use HAVING.
• To calculate the average marks of students of
science stream
SELECT AVG(marks) from STUDENT GROUP BY
STREAM HAVING STREAM=“SCIENCE”;
Practice
Q.1Write a query to display Sum, Average,
Highest and Lowest salary of employee from
emp table.
• SELECT SUM(SAL), AVG(SAL), MAX(SAL),
MIN(SAL) FROM EMP;
Q.2 Write a query to display Sum, Average,
Highest and Lowest salary of the employee
grouped by department number.
SELECT SUM(SAL), AVG(SAL), MAX(SAL), MIN(SAL)
FROM EMP GROUP BY deptno;
• Q.3 Write a query to display the number of
employees with same job.
• SELECT COUNT(*), job FROM emp GROUP BY
job;
• Q.4 Write a query to display the difference of
highest and lowest salary of each department
having maximum salary>50000
• SELECT MAX(sal)- MIN(sal) from emp GROUP
BY deptno HAVING max(sal)>50000;
Assignment
• Page no 274-275 Do Q.1 to 14(Solved
questions)
• Page 285 Q.1 to Q.9(Unsolved questions) in
your class register.

You might also like