Exp 4
Exp 4
1. String Functions
• UPPER (): Converts a string to uppercase.
SELECT UPPER (name) AS uppercase_name
FROM employees;
2. Numeric Functions
• ROUND(): Rounds a number to a specified number of decimal places.
SELECT ROUND(salary, 2) AS rounded_salary
FROM employees;
3. Date Functions
• CURRENT_DATE: Returns the current date.
SELECT CURRENT_DATE AS today
FROM dual;
• ADD_MONTHS(): Adds a specified number of months to a date.
SELECT ADD_MONTHS(hiringdate, 6) AS new_hire_date
FROM employees;
• EXTRACT(): Retrieves sub-parts from a date (like year, month, day).
SELECT EXTRACT(YEAR FROM hire_date) AS hire_year
FROM employees;
4. Conversion Functions
• TO_CHAR(): Converts a number or date to a string.
SELECT TO_CHAR(hiringdate, 'YYYY-MM-DD') AS formatted_hire_date
FROM employees;
• TO_NUMBER(): Converts a string to a number.
SELECT TO_NUMBER(salary_string) AS salary
FROM employee_salaries;
• TO_DATE(): Converts a string to a date.
SELECT TO_DATE('2024-01-01', 'YYYY-MM-DD') AS formatted_date
FROM dual;
5. Miscellaneous Functions
• NVL(): Replaces null with a specified value.
SELECT NVL(phone_number, 'N/A') AS contact_number
FROM employees;
• COALESCE(): Returns the first non-null value in a list.
SELECT COALESCE(middle_name, 'No Middle Name') AS full_name
FROM employees;
3. AVG()
Calculates the average value of a numeric column.
SELECT departmentid, AVG(salary) AS average_salary
FROM employees
GROUP BY departmentid;
4. MIN()
Returns the minimum value of a column.
SELECT departmentid, MIN(salary) AS lowest_salary
FROM employees
GROUP BY departmentid;
5. MAX()
Returns the maximum value of a column.
SELECT departmentid, MAX(salary) AS highest_salary
FROM employees
GROUP BY departmentid;
7. HAVING
Used to filter groups based on the result of aggregate functions.
SELECT departmentid, COUNT(*) AS employee_count
FROM employees
GROUP BY departmentid
HAVING COUNT(*) > 10;
8. ROLLUP
Creates subtotals and grand totals.
SELECT departmentid, COUNT(*) AS employee_count
FROM employees
GROUP BY ROLLUP(departmentid);