Dms Chap 3 Part2 k Scheme
Dms Chap 3 Part2 k Scheme
SQL(PART 2)
Functions:
String Functions: SQL String functions are the predefined functions that allow
the database users for string manipulation. These functions only accept, process,
and give results of the string data type.
CONSIDER FOLLOWING student TABLE FOR STRING FUNCTION
SID SNAME
1 Rahul
2 Ram
3 sita
Some of the major string functions in SQL are as follows −
NEW_VAL5
1rahul
2ram
3sita
13. SUBSTR(STR, START, LENGTH): This function is used to find a sub
string from the a string from the given position with given length
Syntax:SUBSTR('maharashtra', 1, 4);
Output: ‘maha’
Arithmetic/Numeric Functions:
Mathematical functions are very important in SQL to implement different
mathematical concepts in queries. Some of the major mathematical functions in
SQL are as follows −
3. FLOOR(X) : This returns the largest integer value that is either less than X
or equal to it.
For example SELECT FLOOR(5.7);
DMS | Interactive SQL and Advanced SQL (M.U.MUN)
This returns 5.
4. CEIL(X): This returns the smallest integer value that is either more than X or
equal to it.
For example SELECT CEIL(5.7);
This returns 6.
5. POWER(X,Y): This function returns the value of x raised to the power of Y.
For example SELECT POWER(2,5);
This returns 32
6. ROUND(X): This function returns the value of X rounded off to the whole
integer that is nearest to it.
For example SELECT ROUND(5.7);
This returns 6.
7. SQRT(X): This function returns the square root of X.
For example SELECT SQRT(9);
This returns 3.
ADD_MONTH(DATE, NO.): Return date after adding the no. Of month specified in function.
AGGREGATE FUNCTION
o SQL aggregation function is used to perform the calculations on multiple
rows of a single column of a table. It returns a single value.
o Various Aggregate Functions
1) Count()
2) Sum()
3) Avg()
1. COUNT FUNCTION
2. SUM Function
o Sum function is used to calculate the sum of all selected columns. It works
on numeric fields only.
o SELECT SUM(SALARY) FROM EMP;
o OUTPUT: 21000
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG
function returns the average of all non-Null values.
5. MIN Function
o MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column.
o SELECT MIN(SALARY) FROM EMP;
o OUTPUT: 2000
GROUP BY Clause
o The GROUP BY statement groups’ rows that have the same values into
summary rows, like "find the number of customers in each country".
o The GROUP BY statement is often used with aggregate functions (COUNT,
MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
o GROUP BY Syntax
o EXAMPLE: consider following EMP TABLE and write a sql query to display
all the dept name along with no. Of employees working in that department.
o The following SQL statement lists the all department name with no. of
employess working.
o OUTPUT
CM 2
IF 2
EE 1
o OUTPUT
DEPT SUM(SALARY)
CM 7000
IF 8000
o OUTPUT
DEPT MAX(SALARY)
CM 5000
IF 5000
EE 6000
HAVING CLAUSE
o The HAVING clause was added to SQL because the WHERE keyword could
not be used with aggregate functions.
o HAVING clause IS USED to place conditions IN GROUP BY CLAUSE.
o EXAMPLE: Consider following EMP TABLE and write a sql query to
display all the dept name where no. of employees are less than 2.
EE 1
o OUTPUT
DEPT SUM(SALARY)
CM 7000
ORDER BY CLAUSE
o The ORDER BY keyword is used to sort the result-set in ascending or
descending order.
o The ORDER BY keyword sorts the records in ascending order by default.
To sort the records in descending order, use the DESC keyword.
o ORDER BY Syntax
o OUTPUT
o OUTPUT
SQL JOIN
o The SQL JOIN clause takes records from two or more tables in a database and
combines it together.
o Different Types of SQL JOINs Here are the different types of the JOINs in SQL:
1. (INNER) JOIN: Returns records that have COMMON values in both
tables
2. LEFT (OUTER) JOIN: Returns all records from the left table, and
the matched records from the right table
3. RIGHT (OUTER) JOIN: Returns all records from the right
table, and the matched records from the left table
4. FULL (OUTER) JOIN: Returns all records when there is a match in
either left or right table