0% found this document useful (0 votes)
5 views

Dms Chap 3 Part2 k Scheme

Uploaded by

khaparderahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Dms Chap 3 Part2 k Scheme

Uploaded by

khaparderahil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Unit – III: Interactive SQL and Advanced

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 −

1. LOWER(STR): This function is used to convert given string into


lower case.
Syntax: SELECT LOWER(SNAME) from student;
Output:
SNAME
rahul
ram
sita

2. UPPER(STR): This function is used to convert the given string into


upper case.
Syntax: SELECT UPPER(SNAME) from student;
Output:
SNAME
RAHUL
RAM
SITA

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


3. TRIM(): This function is used to remove the space from the string
from left and right side.
Syntax: SELECT TRIM(' hello ');
Output: ‘hello’
4. RTRIM(): This function is used to remove the right side space from
the original string
Syntax: SELECT RTRIM(' hello ');
Output: ‘ hello’
5. LTRIM(): This function is used to remove the left side space from
the original string
Syntax: SELECT LTRIM(' hello ');
Output: ‘ hello ’
6. RTRIM(str, char): This function is used to remove the given char
from the original string from right side.
Syntax: SELECT RTRIM(‘COMPUTER’, ‘TER’);
Output: COMPU
7. LTRIM(str, char): This function is used to remove the given char
from the original string from left side.
Syntax: SELECT LTRIM(‘COMPUTER’, ‘COM’);
Output: PUTER
8. TRANSLATE (str, from,to): This function is used to replace a
sequence of character in a string with another set of character.
Syntax: SELECT translate(SNAME, ‘a’, ‘m’) as a new_name from student;
Output:
New_name
rmhul
rmm
sitm

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


9. REPLACE(str, STR1,STR2): This function is used to replace a STR1
in a string with STR2.
Syntax: SELECT REPLACE (SNAME, ‘rahul’, ‘jay’) from student;
Output:
SNAME
jay
ram
sita

10. REVERSE(str): This function is used to reverse a string.


Syntax: SELECT REverse (SNAME) from student;
Output:
SNAME
Luhar
Mar
atis

11. LENGTH(): This function is used to find the length of a word.


Syntax: SELECT LENGTH(SNAME) from student;
Output:
SNAME
5
3
4

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


12. CONCAT(): This function is used to add two words or strings.
Syntax: SELECT CONCAT(SID,SNAME) AS NEW_VAL from student;
Output:

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 −

1. ABS(X): This function returns the absolute value of X.


For example SELECT ABS(-6);
This returns 6.

2. MOD(X,Y): The variable X is divided by Y and their remainder is returned.


For example SELECT MOD(9,5);
This returns 4

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.

DATE AND TIME FUNCTION

CURRENT_DATE(): Returns the current date.


Example: SELECT CURRENT_DATE() FROM DUAL;
OUTPUT:
CURRENT_DATE
25-NOV-23

CURRENT_TIMESTAMP: RETURN CURRENT DATE AND TIME.


Example: SELECT CURRENT_TIMESTAMP() FROM DUAL;
OUTPUT:
CURRENT_TIMESTAMP

25-NOV-23 10.50.19.811000 AM +00:00

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


LAST_DAY(date): Takes a date or datetime value and returns the corresponding
value for the last day of the month. Returns NULL if the argument is invalid.
Example: SELECT LAST_DAY(DATE '2023-02-01') FROM dual;
OUTPUT: LAST_DAY(DATE' 2023 - 02- 01')
28-FEB-23

ADD_MONTH(DATE, NO.): Return date after adding the no. Of month specified in function.

Example: SELECT ADD_MONTHS( DATE '2023-10-09', 1 )FROM dual;


OUTPUT:
ADD_MONTHS(DATE' 2023 - 10- 09 ' ,1 )
09-NOV-23

NEXT_DAY(DATE, WEEKDAY): Return Coming Weekday After Given Date In Function.

Example: SELECT NEXT_DAY( DATE '2023-10-08', 'SUNDAY' ) FROM DUAL;


OUTPUT:
NEXT_DAY(DATE' 2023 - 10- 08 ' ,' SUNDAY')
15-OCT-23

MONTH_BETWEEN(DATE1, DATE2): Return The No. Of Month Between Two Dates.

Example: SELECT MONTHS_BETWEEN( DATE '2023-10-01', DATE '2023-04-01' ) FROM DUAL;


OUTPUT:
MONTHS_BETWEEN(DATE' 2023 - 10- 01 ' ,DATE' 2023 - 04- 01')
6

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()

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


4) Min()
5) Max()

o Now let us understand each Aggregate function with a FOLLOWING


EMP TABLE example:
ID NAME SALARY
1 A 2000
2 B 3000
3 C 5000
4 D 5000
5 E 6000

1. COUNT FUNCTION

o COUNT function is used to Count the number of rows in a database table.


o SELECT COUNT(*) FROM EMP;
o OUTPUT: 5

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.

o SELECT AVG(SALARY) FROM EMP;


o OUTPUT: 4200

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


4. MAX Function
o MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
o SELECT MAX(SALARY) FROM EMP;
o OUTPUT: 6000

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

SELECT column_name(s) FROM table_name WHERE condition GROUP BY


column_name(s)

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.

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


ID NAME DEPT SALARY
1 A CM 2000
2 B IF 3000
3 C CM 5000
4 D IF 5000
5 E EE 6000

o The following SQL statement lists the all department name with no. of
employess working.

SELECT DEPT, COUNT(DEPT) FROM EMP GROUP BY DEPT;

o OUTPUT

DEPT NO.OF EMP

CM 2

IF 2

EE 1

o EXAMPLE: Write a sql query to display sum of salaries dept wise.

SELECT DEPT, SUM(SALARY) FROM EMP GROUP BY DEPT;

o OUTPUT

DEPT SUM(SALARY)

CM 7000

IF 8000

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


EE 6000

o EXAMPLE: Write a sql query to display maximun salary grouping on the


basis of dept.

SELECT DEPT, MAX(SALARY) FROM EMP GROUP BY DEPT;

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.

ID NAME DEPT SALARY


1 A CM 2000
2 B IF 3000
3 C CM 5000
4 D IF 5000
5 E EE 6000

SELECT DEPT FROM EMP GROUP BY DEPT HAVING COUNT(DEPT)<2;

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


o OUTPUT

DEPT NO.OF EMP

EE 1

o EXAMPLE: WRITE A SQL QUERY TO DISPLAY SUM OF SALARIES OF ‘CM’


DEPT WISE.

SELECT DEPT, SUM(SALARY) FROM EMP GROUP BY DEPT


HAVING DEPT=’CM’;

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

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

o EXAMPLE: CONSIDER FOLLOWING EMP TABLE AND WRITE A SQL


QUERY TO DISPLAY EPLOYEE INFORMATION AS PER THEIR DEPT IN
ASCENDING ORDER.
DMS | Interactive SQL and Advanced SQL (M.U.MUN)
ID NAME DEPT SALARY
1 A CM 2000
2 B IF 3000
3 C CM 5000
4 D IF 5000
5 E EE 6000

SELECR * FROM EMP ORDER BY DEPT;

o OUTPUT

ID NAME DEPT SALARY


1 A CM 2000
3 C CM 5000
5 E EE 6000
2 B IF 3000
4 D IF 5000

o EXAMPLE: WRITE A SQL QUERY TO DISPLAY EPLOYEE


INFORMATION AS PER THEIR NAME IN DESCENDING ORDER.

SELECR * FROM EMP ORDER BY NAME DESC;

o OUTPUT

DMS | Interactive SQL and Advanced SQL (M.U.MUN)


ID NAME DEPT SALARY
5 E EE 6000
4 D IF 5000
3 C CM 5000
2 B IF 3000
1 A CM 2000

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

DMS | Interactive SQL and Advanced SQL (M.U.MUN)

You might also like