Professional Documents
Culture Documents
Co2 Session 9
Co2 Session 9
INSTRUCTIONAL
OBJECTIVES
This session is designed to equip learners with the knowledge and skills necessary to write efficient and effective SQL
LEARNING OUTCOMES
At the end of this session, you should be able to write SQL functions and distinguish between built-in and
user-defined functions. Create user-defined functions in SQL to perform specific calculations on data.
• Definition of Function
• T ypes of SQL
Function
• Numeric Function
• String Function
• Conversion Function
• Date Function
String Functions:
To Processing on String Data type
Conversion Functions:
To Convert Data type from one data type to
another
Date Functions:
To processing on Date Data T ype
Syntax:
AVG ([DISTINCT | ALL ] column_name);
Example:
SELECT AVG(BALANCE) AS AVERAGE _BALANCE
FROM ACCOUNT_MASTER;
Syntax:
MIN ([DISTINCT | ALL] column_name) ;
Example:
SELECTMIN(BALANCE) AS Minimum_Balance
FROM ACCOUNT;
Syntax:
MAX ([ DISTINCT | ALL ] column_name);
Example:
SELECT MAX(BALANCE) AS maximum balance
FROM ACCOUNT_MASTER ;
Syntax:
COUNT ( [ DISTINCT | ALL ] column_name) ;
Example:
SELECT COUNT(ACCOUNT_NO) AS no. of records
FROM ACCOUNT_MASTER;
Syntax:
SUM( [ DISTINCT | ALL ] column_name) ;
Example:
SELECT SUM(BALANCE) AS TOTAL_BALANCE
FROM ACCOUNT_MASTER;
TRUNC Function
Example :
Select ABS(-10);
Output:
10 Displayed on Postgresql O/P Screen
Syntax:
POWER(m,n);
Example:
Select POWER(6,2);
Output:
36 Displayed on The Postgresql Screen 62
Syntax:
SQRT (n);
Returns Square root of n
n must be Positive if n < 0 then Null will be returned
Example:
Select SQRT(25);
Output:
5
Syntax:
EXP (n);
en
Example:
Select EXP(5);
Output:
148.413159 ( e = 2.71828183 )
Syntax:
GREATEST (exp1,exp2,…,expn);
Example:
Select GREATEST(21,10,30);
Output:
30
Syntax:
LEAST (exp1,exp2,…,expn);
Example:
Select LEAST(35,75,25);
Output:
25
Syntax:
MOD (m,n);
Example:
Select MOD(18,7);
Syntax:
TRUNC (number, decimal_Places);
Example:
Select TRUNC(17.235,1);
Output:
17.2
Syntax:
FLOOR (n)
Example:
Select FLOOR(24.18);
Output:
24
Syntax:
CEIL (n);
Example:
Select CEIL(24.83);
Output:
25
Lower
xyz
Output:
Upper_Case
XYZ
First_Case
Abc
Sub_Str
CURE
Ascii_Value
97
Position
6
Length
3
CREATED BY K. VICTOR BABU
LTRIM Function
Ltrim
yz
Rtrim
xy
Lpad
**xyz
Rpad
xyzppp
CONCAT
abcXYZ
CREATED BY K. VICTOR BABU
NESTED SUB QUERIES
Example
●
SELEC T * FRO M t1 WH ERE column1 = (SELEC T column1 FRO M t2);
●
The main advantages of subqueries are:
– They allow queries that are structured so that it is
possible to isolate each part of a statement.
– They provide alternative ways to perform operations
that would otherwise require complex joins and unions.
– More readable than complex joins or unions.
●
Indeed, it was the innovation of subqueries that gave
people the original idea of calling the early SQ L
“Structured Q uery Language.”
WH ERE
YEAR =2015
AND f.country = c.country
G RO U P BY country),
CREATED BY K. VICTOR BABU
ANOTHER EXAMPLE
●
SELEC T column1 FRO M t1 WH ERE EX ISTS (SELEC T * FRO M t2);
●
What countries exists in my two datasources?
– SELEC T D ISTINC T country FRO M countries c
WH ERE EX ISTS (SELEC T name FRO M countries2 c2 WH ERE
c.country = c2.name);
●
Now the opposite
– SELEC T D ISTINC T country FRO M countries c
WH ERE NO T EX ISTS (SELEC T name FRO M countries2 c2
WH ERE c.country = c2.name);
FRO M fertility f
WHERE year BETWEEN 2000 AND 2015
GRO UP BY country) AS avgfert
JO IN countries c O N (c.country = avgfert.country)
CREATED BY K. VICTOR BABU
GRO UP BY continent
ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO
THE SESSION
1. Write a query to display the employee name and hire date for all employees in the same department as Blake. Exclude
Blake.
2. Create a query to display the employee number and name for all employees who earn more than the average salary.
Sort the results in descending order of salary.
3. Display the employee name, department number, and job title for all employees whose department location is Dallas.
4. Write a query to display the name, department number, and salary of any employee whose department number and
salary matches both the department number and salary of any employee who earns a commission.
5. Display the name, department name, and salary of any employee whose salary and commission matches both the salary
and commission of any employee located in Dallas.
6. Create a query to display the name, hire date, and salary for all employees who have both the same salary and
commission as Scott.
7. Create a query to display the employees that earn a salary that is higher than the salary of any of the CLERKS. Sort the
results on salary from highest to lowest.
Functions and nested subqueries are two powerful features of PostregSQL that can be used to retrieve and manipulate data in
complex ways. Functions are named sets of instructions that can perform calculations on data and return a single value,
while nested subqueries are SQL queries that are embedded within other queries to perform more complex operations, such
as filtering, grouping, and joining data.
In a session focused on "Functions and Writing SQL Statements Using Nested Subqueries", learners can expect to gain a
comprehensive understanding of these concepts. They will learn how to create user-defined functions in PostregSQL,
understand the differences between built-in and user-defined functions, and be able to apply these functions to perform
specific calculations on data.
Learners will also learn the syntax of PostregSQL subqueries and differentiate between correlated and non-correlated
subqueries. They will understand how to use nested subqueries to perform complex data retrieval and manipulation
operations and how to construct SQL statements that use functions and nested subqueries efficiently to retrieve and
manipulate data. Additionally, learners will learn how to analyze and optimize existing SQL code that uses functions and
nested subqueries.
Overall, the goal of a session on Functions and Writing SQL Statements Using Nested Subqueries is to equip learners with
the skills and knowledge necessary to manipulate data effectively using these powerful features of SQL.
1. Which of the following is a key difference between built-in and user-defined PostregSQL functions?
(a) Built-in functions are created by users, while user-defined functions are pre-existing.
(b) User-defined functions are created by users, while built-in functions are pre-existing.
(c) Built-in functions can only be used with specific data types, while user-defined functions can
be created for any data type.
(d) User-defined functions can only be used with specific data types, while built-in functions can
be used with any data type.
1. Which of the following is a key difference between built-in and user-defined PostregSQL functions?
(a) Built-in functions are created by users, while user-defined functions are pre-existing.
(b) User-defined functions are created by users, while built-in functions are pre-existing.
(c) Built-in functions can only be used with specific data types, while user-defined functions can
be created for any data type.
(d) User-defined functions can only be used with specific data types, while built-in functions can
be used with any data type.
Answer: d) User-defined functions can only be used with specific data types, while builtin functions can be used with any data type
2. Which type of subquery is used to perform a calculation or retrieve a single value that is then used in the main query?
2. Which type of subquery is used to perform a calculation or retrieve a single value that is then used in the main query?
1. How do you create a user-defined function in PostregSQL, and what are some common use cases for such functions?
2. What is the difference between correlated and non-correlated subqueries, and how do you use each type effectively in
PostregSQL statements?
3.Can you give an example of how nested subqueries can be used to perform a complex data retrieval operation, such as
filtering or grouping data?
4.When should you use built-in PostregSQL functions versus creating your own user-defined functions, and what are
some factors to consider when making this decision?
5.How can you optimize existing PostregSQL code that uses functions and nested subqueries to improve performance
and efficiency?
Reference Books:
1. Database System Concepts, Sixth Edition, Abraham Silberschatz, Yale University Henry, F. Korth Lehigh
University, S. Sudarshan Indian Institute of Technology, Bombay.
2. Fundamentals of Database Systems, 7th Edition, RamezElmasri, University of Texas at Arlington,
Shamkant B. Navathe, University of Texasat Arlington.
3. An Introduction to Database Systems by Bipin C. Desai