Lab - 4 - Retrieving Data From Multiple Tables
Lab - 4 - Retrieving Data From Multiple Tables
Faculty of Engineering
Dept. of Computer Engineering
Database Lab (ECOM 4113)
Lab 4
SQL
Functions
(Single-Row, Aggregate)
Character Functions
Case-conversion functions:
LOWER, UPPER, INITCAP
Function Result
LOWER('SQL Course') sql course
Example: Find the id and student name for all student whose name contains the
begin with “s”.
Use Conversion function
If you don’t care about capital later will not get any records
Character-manipulation functions:
CONCAT, SUBSTR, LENGTH, INSTR, (LPAD | RPAD), TRIM, REPLACE
Function Result
SUBSTR('HelloWorld',1,5) Hello
LENGTH('HelloWorld') 10
INSTR ('HelloWorld',’o’) 5
LPAD(salary,10,'*') *****24000
Number Functions:
• ROUND: Rounds value to a specified decimal
• TRUNC: Truncates value to a specified decimal
• MOD: Returns remainder of division
Function Result
ROUND(45.926, 2) 45.93
TRUNC(45.926, 2) 45.92
MOD(1600, 300) 100
SELECT ROUND(45.923,2),ROUND(45.923,0),ROUND(45.923,-1)
FROM DUAL
SELECT TRUNC(45.923,2),TRUNC(45.923,0),TRUNC(45.923,-1)
FROM DUAL
These are some of the format elements that you can use with the
TO_CHAR function to display a number value as a character
Element Result
9 Represents a number
0 Forces a zero to be displayed
$ Places a floating dollar sign
L Uses the floating local currency symbol
. Prints a decimal point
, Prints a comma as a thousand indicator
Example: Display Salary as the following pattern $##,###.00
Nesting Functions:
Single-row functions can be nested to any level.
Nested functions are evaluated from the deepest level to the least deep
level
NVL2 (expr1, expr2, expr3): Converts a null value or not null to an actual value.
Example: Use NULLIF function to check if two student study in the same
department or not.
Unlike single-row functions, group functions operate on sets of rows to give one
result per group. These sets may comprise the entire table or the table split into
groups.
The group function is placed after the SELECT keyword. You may have multiple
group functions separated by commas.
Note: The AVG, SUM, VARIANCE, and STDDEV functions can be used only with
numeric data types.
Count Function:
SELECT COUNT(*)
FROM INSTRUCTOR
SELECT COUNT(DEPT_NAME)
FROM STUDENT;
SELECT AVG(SALARY )
FROM INSTRUCTOR;
By using NVL function, the average is calculated based on all rows in the table,
regardless of whether null values are stored
SELECT AVG(NVL(SALARY,0) )
FROM INSTRUCTOR;
Grouping of Data:
You can use the GROUP BY clause to divide the rows in a table into groups.
You can then use the group functions to return summary information for each
group.
You should be aware of some notes you deal with group functions:
If you include a group function in a SELECT clause, you cannot select
individual results as well, unless the individual column appears in the
GROUP BY clause.
Using a WHERE clause, you can exclude rows before dividing
them into groups.
You cannot use a column alias in the GROUP BY clause.
. You can also use the group function in the ORDER BY clause:
Note: To express about each course section we need add course_id ,sec_id, semester and year
in grouping ,but ,if we need count number of student enrolled in course which taken in every
semester, regardless about section number, we need add just course_id, semester and year in
grouping.
Example: Find the average salary of instructors in those departments where the
average salary is more than $50,000
Note that you cannot use group functions in the WHERE clause: Instead, use them
in HAVING clause
SELECT DEPT_NAME
FROM INSTRUCTOR
GROUP BY DEPT_NAME
HAVING AVG (SALARY) =( SELECT MAX(AVG (SALARY))
FROM INSTRUCTOR
GROUP BY DEPT_NAME );
Example: Find the average instructors’ salaries of those departments where the
average salary is greater than $50,000.” We wrote this query in above, We can
now rewrite this query, without using the having clause, by using a subquery in
the from clause, as follows:
END