CBSE CS

cbse cs logo

Home

MYSQL FUNCTIONS

What is Function?


A function is a special types of command in MySQL that  performs some operation on table and returns a single value  as a result.
 Types of Functions:
 Numeric Functions
 String Functions
 Date & Time Function
 Aggregate Functions
Numeric, String and Date-Time functions are called Single row functions because they can accept one row and return only one value.
When applied on a table, they return a single result for every row of the queried table.
Aggregate Functions are called Multiple row functions because they operate on a set of rows to return a single value.

 

Aggregate Functions

Aggregate functions are also known as group functions. Aggregate functions return a result only in single row based on group of rows, rather than on single row. It always appears
in SELECT command and in ORDER BY and HAVING clauses. They are commonly used with the GROUP BY clause in a SELECT statement. Some of the aggregate functions are MIN, MAX, SUM, AVG, COUNT. The SQL aggregate functions return a single value, calculated from values in a column.
There are different types of aggregate functions

AVG()

This function returns the average value of a specified column.
Syntax SELECT AVG(column_name) FROM table_name;

COUNT( )

This function returns the total number of values or rows of the specified field or column. COUNT (*) is a special function, as it returns the count of all rows in a specified table. It includes all the null and duplicate values.
Syntax
SELECT COUNT(*) FROM table_name;

DISTINCT Clause with COUNT( ) Function

The DISTINCT keyword helps us in removing the duplicate value from the result. When it is used with aggregate function COUNT, it returns the number of distinct rows in a
specified table.
Syntax SELECT COUNT(DISTINCT column_name) FROM table_name;
e.g. To count total number of rows in Department column from PAYMENTS table.
SELECT COUNT(Department) “Deptid” FROM PAYMENTS;

e.g. To count distinct values of column Department from PAYMENTS table.
SELECT COUNT(DISTINCT Department) “Deptid” FROM PAYMENTS;

MAX( )

This function returns the largest value from the selected column.
Syntax SELECT MAX(column_name) FROM table_name;
e.g. To find the maximum salary of employee from PAYMENTS table.
SELECT MAX(Salary) “Maximum” FROM PAYMENTS;

MIN( ) 

This function returns the smallest value from the selected  column.
Syntax SELECT MIN(column_name) FROM table_name;
e.g. To display the minimum salary of employee from PAYMENTS table.
SELECT MIN(Salary) “Minimum” FROM PAYMENTS;

SUM( )

This function returns the sum of values in the specified column. The SUM works on numeric fields only. Null values are excluded from the result returned.
Syntax SELECT SUM(column_name) FROM table_name;
e.g. To count sum of employee’s salary from PAYMENTS table.
SELECT SUM(Salary) “Salary” FROM PAYMENTS;

 

GROUP BY Statement

The GROUP BY statement is used with the aggregate functions to group the result set by one or more columns.
Syntax SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name;

HAVING Clause

The HAVING clause is used with GROUP BY clause to place condition because the WHERE keyword could not be used with aggregate functions.
Syntax
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name
HAVING aggregate_function(column_name)

Example:

If we want to find any of the customers have a total order of less than 2000 from table ORDERS. We use the following SQL statement
SELECT Customer,SUM(OrderPrice)
FROM ORDERS
GROUP BY Customer
HAVING SUM(OrderPrice)<2000;

Example:

SELECT Customer,SUM(OrderPrice)
FROM ORDERS
WHERE Customer=‘Hansen’ OR Customer=‘Jensen’
GROUP BY Customer
HAVING SUM(OrderPrice)>1500;

 

OBJECTIVE TYPE QUESTIONS /MULTIPLE CHOICE QUESTIONS
1. What is the full form of SQL?
(a) Structured Query Language (b) Structured Query List
(c) Simple Query Language (d) Data Derivation Language
2. What does DML stand for?
(a) Different Mode Level (b) Data Model Language
(c) Data Mode Lane (d) Data Manipulation Language
3. The __________clause of SELECT query allows us to select only those rows in the results that satisfy a
specified condition.
(a) Where (b) from (c) having (d) like
4. Which of the following function is used to FIND the largest value from the given data in MYSQL?
(a) MAX () (b) MAXIMUM () (c) LARGEST () (c) BIG ()
5. The data types CHAR (n) and VARCHAR (n) are used to create _______ and _______ types of string/text
fields in a database.
(a) Fixed, equal (b) Equal, variable (c) Fixed, variable (d) Variable, equal
6. The term ___________ is use to refer to a record in a table.
(a) Attribute (b) Tuple (c) Row (d) Instance
7. Which command is used for cleaning up the environment (sql with Python)?
(a) my.close (b) is.close (c) con.close (d) mycon.close
8. A relational database consists of a collection of
(a) Tables (b) Fields (c) Records (d) Keys
9. What is the full form of DDL?
(a) Dynamic Data Language             (b) Detailed Data Language
(c) Data Definition Language           (d) Data Derivation Language

10. A(n) in a table represents a logical relationship among a set of values.
(a) Attribute                    (b) Key                   (c) Tuple                   (d) Entry
11. Name the method which is used for displaying only one resultset.
(a) fetchmany                   (b) fetchno                   (c) fetchall                   (d) fetchone
12. Name the host name used for signing in the database.
(a) localhost                   (b) localpost                   (c) localcost                   (d) none of the above
13. A relational database consists of a collection of
(a)Tuples                   (b) Attributes                   (c) Relations                   (d) Keys
14. Which is the subset of SQL commands used to manipulate database structure including tables?
(a) Data Definition Language (DDL)                   (b) Data Manipulation Language (DML)
(c) Both (a) and (b)                                                   (d) None
15. The term ____________ is used to refer to a field in a table.
(a) Attribute                   (b) Tuple                   (c) Row                   (d) Instance

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

error: Content is protected !!