0% found this document useful (0 votes)
317 views4 pages

Assignment-6: 1. Differentiate Aggregate Function and Scalar Function

Scalar functions operate on single values and return a single result, while aggregate functions operate on sets of data and return a single result. COUNT(*) counts all rows in a table while COUNT() counts non-null values in a column. ROUND() rounds a value to the nearest integer or specified number of decimal places, while TRUNCATE() truncates a value to the specified number of decimal places by removing digits. REPLACE() replaces all occurrences of a substring within a string with another string, while TRANSLATE() does an orderly character-by-character substitution. Age can be calculated from a date of birth using datediff() and date_format() functions.

Uploaded by

Mihir Khunt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
317 views4 pages

Assignment-6: 1. Differentiate Aggregate Function and Scalar Function

Scalar functions operate on single values and return a single result, while aggregate functions operate on sets of data and return a single result. COUNT(*) counts all rows in a table while COUNT() counts non-null values in a column. ROUND() rounds a value to the nearest integer or specified number of decimal places, while TRUNCATE() truncates a value to the specified number of decimal places by removing digits. REPLACE() replaces all occurrences of a substring within a string with another string, while TRANSLATE() does an orderly character-by-character substitution. Age can be calculated from a date of birth using datediff() and date_format() functions.

Uploaded by

Mihir Khunt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT-6

1. Differentiate aggregate function and scalar function.

Scalar function Aggregate function


scalar functions operate against a single Aggregate Functions operate on
value and return a single value. complete sets of data and return a single
2. result.
Scalar functions can be placed aggregate functions can be placed only
anywhere. in select statement or with the clause
having.
A scalar functionis a function that An aggregate function is a function that
operates on scalar values -- that is, it operates on aggregate data -- that is, it
takes one (or more) input values as takes a complete set of data as input
arguments directly and returns a value. and returns a value that is computed
from all the values in the set.

scalar functions return a single value, aggregate functions return a single


based on the input value. value, calculated from values in a
column.

Useful scalar functions: Useful aggregate functions:


UCASE() - Converts a field to upper case AVG() - Returns the average value
LCASE() - Converts a field to lower case COUNT() - Returns the number of rows
MID() - Extract characters from a text FIRST() - Returns the first value
field LAST() - Returns the last value
LEN() - Returns the length of a text field MAX() - Returns the largest value
ROUND() - Rounds a numeric field to the MIN() - Returns the smallest value
number of decimals specified SUM() - Returns the sum
NOW() - Returns the current system
date and time
FORMAT() - Formats how a field is to be
displayed

Differenciate COUNT() and COUNT (*) with example.

COUNT(*) COUNT()
Counts the rows in table Counts the entries in a column-ignoring null
values
SELECT COUNT (*) val_count CREATE TABLE t(
FROM t; Val int
);
Output: INSERT INTO t(val)
VALUES(1),(2),(2),(3),(NULL),(NULL),(4),(5);
val_count
SELECT val FROM t;
-------------
8 Output:
Val
-------------
(1 row affected) 1
2
3
NULL
NULL
4
5

3. Explain Numeric functions ROUND() and TRUNCATE() with


proper Example.

The TRUNCATE() function is used to return the value of X truncated to D number of decimal
places. If D is 0, then the decimal point is removed. If D is negative, then D number of values in
the integer part of the value is truncated. Consider the following example –

mysql> Select TRUNCATE(7.536432,2);

+----------------------+

| TRUNCATE(7.536432,2) |

+----------------------+

| 7.53 |

+----------------------+

1 row in set (0.00 sec)

The ROUND() function returns X rounded to the nearest integer. If a second argument, D, is
supplied, then the function returns X rounded to D decimal places. D must be positive or all
digits to the right of the decimal point will be removed. Consider the following example −

mysql>SELECT ROUND(5.693893);

+---------------------------------------------------------+

| ROUND(5.693893) |

+---------------------------------------------------------+

| 6 |

+---------------------------------------------------------+

1 row in set (0.00 sec)

mysql>SELECT ROUND(5.693893,2);
+---------------------------------------------------------+

| ROUND(5.693893,2) |

+---------------------------------------------------------+

| 5.69 |

+---------------------------------------------------------+

1 row in set (0.00 sec)

4. Explain character function TRANSLATE() AND REPLACE() with


proper example.
Repalce:
The Replace function replaces one value in a string with another.
For example, you can replace each occurrence of a letter with matching number.

REPLACE (char,search_string,replace_string)

If value for replace_string is not specify, the search_string value, when found, is removed.

Possible input can be any character data types, like CHAR, VARCHAR2,NCHAR,CLOB.

SELECT REPLACE('COMPUTER','OM','AB')

FROM dual;
Output
-----------
CABPUTER

Translate:

Translate does an orderly character-by-character substitution in a string.

TRANSLATE (string,if,then)

SELECT TRANSLATE(1256364,2345678,'BDEFGHI')

FROM dual;

Output

----------

BFGDGE

5. Find Age of an Employee from his Date of Birth using suitable


date function.
SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(now(),'2010-11-25')), '%Y')+0 AS Age;

+------+
| Age |
+------+
| 8|
+------+
1 row in set (0.00 sec)

--- 196170307055

You might also like