Aggregate Functions
==>Numeric Function
1. count
2. sum
3. min
4. max
5. avg
select count(*) as "Total Records" from studentsy;
SQL> select sum(sage) from studentsy;
SUM(SAGE)
----------
56
SQL> select avg(sage) from studentsy;
AVG(SAGE)
----------
18.6666667
SQL> select sum(sage)/count(*) from studentsy;
SUM(SAGE)/COUNT(*)
------------------
18.6666667
SQL> select min(sage) from studentsy;
MIN(SAGE)
----------
17
SQL> select max(sage) from studentsy;
MAX(SAGE)
----------
20
SQL>
SQL> select sysdate from dual;
SYSDATE
---------
19-JUN-23
SQL> select abs(3) from dual;
ABS(3)
----------
3
SQL> select abs(-3) from dual;
ABS(-3)
----------
3
SQL> select abs(-3.6) from dual;
ABS(-3.6)
----------
3.6
SQL> select ceil(4.2) from dual;
CEIL(4.2)
----------
5
SQL> select floor(4.2) from dual;
FLOOR(4.2)
----------
4
SQL> select ceil(-8.4) from dual;
CEIL(-8.4)
----------
-8
SQL> select floor(-8.4) from dual;
FLOOR(-8.4)
-----------
-9
SQL> select mod(18,4) from dual;
MOD(18,4)
----------
2
SQL> select power(3,2) from dual;
POWER(3,2)
----------
9
SQL> select round(4.2) from dual;
ROUND(4.2)
----------
4
SQL> select round(4.8) from dual;
ROUND(4.8)
----------
5
SQL> select round(4.24) from dual;
ROUND(4.24)
-----------
4
SQL> select sign(244.6) from dual;
SIGN(244.6)
-----------
1
SQL> select sign(-244.6) from dual;
SIGN(-244.6)
------------
-1
SQL> select sqrt(25) from dual;
SQRT(25)
----------
5
==>String functions
select ascii('a') from dual;
97
Combined St
-----------
Hello World
SQL> select len('Hello') from dual;
select len('Hello') from dual
*
ERROR at line 1:
ORA-00904: "LEN": invalid identifier
SQL> select length('Hello') from dual;
LENGTH('HELLO')
---------------
5
SQL> select lcase('Hello') from dual;
select lcase('Hello') from dual
*
ERROR at line 1:
ORA-00904: "LCASE": invalid identifier
SQL> select lower('Hello') from dual;
LOWER
-----
hello
SQL> select upper('Hello') from dual;
UPPER
-----
HELLO
SQL> select left('Hello World',2) from dual;
select left('Hello World',2) from dual
*
ERROR at line 1:
ORA-00904: "LEFT": invalid identifier
SQL> select substr('Hello World',2) from dual;
SUBSTR('HE
----------
ello World
SQL> select substr('Hello World',6) from dual;
SUBSTR
------
World
SQL> select substr('Hello World',7) from dual;
SUBST
-----
World
SQL> select substr('Hello World',2,5) from dual;
SUBST
-----
ello
SQL> select substr('Hello World',-1) from dual;
S
-
d
SQL> select substr('Hello World',-2) from dual;
SU
--
ld
SQL> select trim(' Hello World ') from dual;
TRIM('HELLOWOR
--------------
Hello World
SQL> select instr('hello','e') from dual;
INSTR('HELLO','E')
------------------
2
SQL> select instr('hello','l') from dual;
INSTR('HELLO','L')
------------------
3
SQL> select substr('hello world',1) from dual;
SUBSTR('HEL
-----------
hello world
SQL> select substr('hello world',2) from dual;
SUBSTR('HE
----------
ello world
SQL> select substr('hello world',1,1), substr('hello world',2) from dual;
S SUBSTR('HE
- ----------
h ello world
SQL> select upper(substr('hello world',1,1)), substr('hello world',2) from dual;
U SUBSTR('HE
- ----------
H ello world
SQL> select concat( upper(substr('hello world',1,1)), substr('hello world',2)) from
dual;
CONCAT(UPPE
-----------
Hello world
SQL> select initcap('hello world') from dual;
INITCAP('HE
-----------
Hello World
SQL> select replace('hello world','e','a') from dual;
REPLACE('HE
-----------
hallo world
SQL> select replace('hello world','l','a') from dual;
REPLACE('HE
-----------
heaao worad
SQL> select chr(65) from dual;
C
-
A
SQL> select chr(97) from dual;
C
-
a
SQL> select lpad('hello',10,'$') from dual;
LPAD('HELL
----------
$$$$$hello
SQL> select rpad('hello',10,'$') from dual;
RPAD('HELL
----------
hello$$$$$
SQL> select rpad('hello',5,'$') from dual;
RPAD(
-----
hello
>>>Date Functions
SQL> select current_date from dual;
CURRENT_D
---------
19-JUN-23
SQL> select current_timestamp from dual;
CURRENT_TIMESTAMP
---------------------------------------------------------------------------
19-JUN-23 05.58.57.770000 PM +05:30
SQL> select current_timestamp from dual;
CURRENT_TIMESTAMP
---------------------------------------------------------------------------
19-JUN-23 05.59.17.176000 PM +05:30
SQL> select extract(YEAR from sysdate) from dual;
EXTRACT(YEARFROMSYSDATE)
------------------------
2023
SQL> select extract(month from sysdate) from dual;
EXTRACT(MONTHFROMSYSDATE)
-------------------------
6
SQL> select extract(day from sysdate) from dual;
EXTRACT(DAYFROMSYSDATE)
-----------------------
19
SQL> select extract(HOUR from current_timestamp) from dual;
EXTRACT(HOURFROMCURRENT_TIMESTAMP)
----------------------------------
12
SQL> select extract(MINUTE from current_timestamp) from dual;
EXTRACT(MINUTEFROMCURRENT_TIMESTAMP)
------------------------------------
31
SQL> select extract(SECOND from current_timestamp) from dual;
EXTRACT(SECONDFROMCURRENT_TIMESTAMP)
------------------------------------
35.873
SQL> select last_day(sysdate) from dual;
LAST_DAY(
---------
30-JUN-23
SQL> select last_day('02-Jan-2010') from dual;
LAST_DAY(
---------
31-JAN-10
SQL> select next_day('19-Jun-2023','TUESDAY') from dual;
NEXT_DAY(
---------
20-JUN-23
SQL> select next_day('19-Jun-2023','SUNDAY') from dual;
NEXT_DAY(
---------
25-JUN-23
SQL> select months_between('25-JUL-2023','20-JUN-2023') from dual;
MONTHS_BETWEEN('25-JUL-2023','20-JUN-2023')
-------------------------------------------
1.16129032
SQL> select months_between('25-JUL-2023','25-JUN-2023') from dual;
MONTHS_BETWEEN('25-JUL-2023','25-JUN-2023')
-------------------------------------------
1
SQL> select add_months('20-Jun-2023',4) from dual;
ADD_MONTH
---------
20-OCT-23
SQL> select add_months('20-Jun-2023',-4) from dual;
ADD_MONTH
---------
20-FEB-23