0% found this document useful (0 votes)
10 views

Aggregate Functions in SQL

The document provides an overview of aggregate functions in SQL, including MIN, MAX, COUNT, SUM, and AVG, along with their usage examples. It also covers string manipulation functions, date functions in Oracle SQL, and the order of execution for SQL queries. Key distinctions between COUNT(*) and COUNT(column) as well as the use of the DISTINCT keyword are highlighted.

Uploaded by

prashantsutar024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Aggregate Functions in SQL

The document provides an overview of aggregate functions in SQL, including MIN, MAX, COUNT, SUM, and AVG, along with their usage examples. It also covers string manipulation functions, date functions in Oracle SQL, and the order of execution for SQL queries. Key distinctions between COUNT(*) and COUNT(column) as well as the use of the DISTINCT keyword are highlighted.

Uploaded by

prashantsutar024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Aggregate Functions in SQL

Aggregate functions perform calculations on a set of values and return a single value, except
for COUNT (*).

Types of Aggregate Functions in SQL

1. MIN():
o Returns the smallest value within a selected column.
o Example: SELECT MIN(salary) FROM emp;
2. MAX():
o Returns the largest value within a selected column.
o Example: SELECT MAX(salary) FROM emp;
3. COUNT():
o Returns the number of rows in a set.
o Example: SELECT COUNT(*) FROM emp;
4. SUM():
o Returns the sum of numerical values in a column.
o Example: SELECT SUM(salary) FROM emp;
5. AVG():
o Returns the average value of a numerical column.
o Example: SELECT AVG(salary) FROM emp;

Difference between COUNT (*) and COUNT (column)

 COUNT(*):
o Counts the total number of rows in the table, including NULL values.
 COUNT(column):
o Counts the number of records where the specified column is not NULL,
excluding NULL values.

DISTINCT Keyword

The DISTINCT keyword is used with the SELECT statement to eliminate duplicate records and
fetch unique records. In situations where multiple duplicate records exist in a table, it makes
sense to fetch only unique records.

 Example: SELECT DISTINCT edept FROM emp;


String Manipulation Functions

1. Case Manipulation Functions

Case manipulation functions change the case of character strings:

 LOWER()
 UPPER()
 INITCAP()

Example: SELECT UPPER (firstname), LOWER (lastname), INITCAP (firstname) FROM emp;

2. Character Manipulation Functions

Character manipulation functions manipulate character strings:

1. SUBSTR (): Displays specific characters from a main string.


o Syntax: SUBSTR(column_name, x, y)
o Example: SELECT SUBSTR('PRASHANT', 1, 4) FROM emp;
2. INSTR (): Returns the position of a substring within a string.
o Syntax: INSTR(string, substring, start_position, occurrence)
o Example: SELECT INSTR('co-operate floor', 'or') AS instr FROM dual;
o (Output: 2)
3. REPLACE (): Replaces every occurrence of a search string with a new string.
o Example: SELECT REPLACE('Google', 'oo', '') FROM dual;
o (Output: Ggle)
4. LPAD (): Adds characters to the left of a string until a specified length is reached.
o Example: SELECT LPAD('abc', 8, '*') FROM dual;
o (Output: *****abc)
5. RPAD (): Adds characters to the right of a string until a specified length is reached.
o Example: SELECT RPAD('abc', 8, '*') FROM dual;
6. LTRIM (): Removes specified characters from the left side of a string.
o Example: SELECT LTRIM('JAVA', 'J') FROM dual;
o (Output: AVA)
7. RTRIM (): Removes specified characters from the right side of a string.
o Example: SELECT RTRIM('JAVAPOINT', 'POINT') FROM dual;
o (Output: JAVA)

Date Functions in Oracle SQL

1. ADD_MONTHS (): Returns the date with a given number of months added.
o Example: SELECT ADD_MONTHS(DATE '2016-02-29', 1) FROM dual;
o (Output: 31-MAR-16)
2. MONTHS_BETWEEN (): Returns the number of months between two dates.
o Example: SELECT MONTHS_BETWEEN(DATE '05-JAN-81', DATE '05-JAN-80')
FROM dual; (Output: 12)
3. NEXT_DAY (): Gets the first weekday that is later than a specified date.
o Example: SELECT NEXT_DAY(SYSDATE, 'MONDAY') FROM dual;
4. LAST_DAY (): Gets the last day of the month for a specified date.
o Example: SELECT LAST_DAY(SYSDATE) FROM dual;
5. SYSDATE: Returns the current system date and time.
o Example: SELECT SYSDATE FROM dual;
6. SYSTIMESTAMP (): Returns the system date and time, including fractional seconds
and time zone.
o Example: SELECT SYSTIMESTAMP FROM dual;
7. TO_DATE (): Converts a character string to a DATE value.
o Example: SELECT * FROM EMPLOYEE12 WHERE EDOJ = TO_DATE('oct-05-
2018', 'MM-DD-YYYY');
8. TO_CHAR (): Converts a DATE or INTERVAL value to a character string in a specified
format.
o Example: SELECT * FROM EMPLOYEE12 WHERE TO_CHAR(EDOJ, 'YYYY') =
'2020';

SQL Order of Execution

Order Clause Function


1 FROM Tables are merged in order to get the base data.
2 WHERE This clause filters the base data.
3 GROUP BY This clause group the filtered base data.

4 HAVING This clause filters the grouped base data.


5 SELECT This clause returns the final data.
6 ORDER BY This clause stored the final data.
7 LIMIT The returned data is limited to row count.

You might also like