Practical 4
Single row functions in SQL
Single row functions in SQL operate on individual rows and return a single value for each
row. Here are some common types of single row functions with examples:
1. String Functions
• UPPER (): Converts a string to uppercase.
SELECT UPPER (name) AS uppercase_name
FROM employees;
• LOWER (): Converts a string to lowercase.
SELECT LOWER(name) AS lowercase_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;
• CEIL(): Returns the smallest integer greater than or equal to a number.
SELECT CEIL(salary) AS ceiling_salary
FROM employees;
• FLOOR(): Returns the largest integer less than or equal to a number.
SELECT FLOOR(salary) AS floor_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;
Multi row functions in SQL
Multi-row functions in SQL, often referred to as aggregate functions, operate on a set of
rows and return a single summary value. These functions are typically used with the
GROUP BY clause to summarize data. Here are some common multi-row functions with
examples:
1. COUNT()
Counts the number of rows or non-null values.
SELECT departmentid, COUNT(*) AS employee_count
FROM employees
GROUP BY departmentid;
2. SUM()
Calculates the total of a numeric column.
SELECT departmentid, SUM(salary) AS total_salary
FROM employees
GROUP BY departmentid;
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;
6. GROUP_CONCAT() (or equivalent)
Concatenates values from multiple rows into a single string (available in MySQL and
other databases with similar functionality).
SELECT departmentid, GROUP_CONCAT(first_name) AS employees_names
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);