Você está na página 1de 54

2

3
4
5
Group functions do not process NULL values and do not return a NULL value, even when NULL
are the only values evaluated. For example, a COUNT or SUM of NULL values will result in 0.

7
MIN:
This function has the syntax MIN([{DISTINCT | ALL }] <x>), where x is an expression. This
function returns the lowest value in the expression x.
•If the expression x is a date data type, it returns a DATE. For dates, the minimum is the earliest
date.
•If the expression x is a numeric data type, it returns a NUMBER. For numbers, the minimum is
the smallest number.
•If the expression is a character data type, it returns a VARCHAR2. for character strings, the
minimum is the one that sorts lowest based on the database character set.

8
MAX:
This function has the syntax MAX([{DISTINCT | ALL }] <x>), where x is an expression. This
function returns the highest value in the expression x.
•If the expression x is a date data type, it returns a DATE. For dates, the maximum is the latest
date.
•If the expression x is a numeric data type, it returns a NUMBER. For numbers, the maximum is
the largest number.
•If the expression is a character data type, it returns a VARCHAR2. for character strings, the
maximum is the one that sorts lhighest based on the database character set.

9
AVG:
This function has the syntax AVG ( [DISTINCT | ALL] <n>), where n is a numeric expression.
The AVG function returns the mean of the expression n. If neither DISTINCT nor ALL is specified
in the function call, the default is ALL.

10
11
COUNT:
The COUNT function returns the number of rows in the query. The asterisk (*) is a special
quantity – it counts all rows in the result set, regardless of NULLs.

12
13
In the output table all the rows with an identical value in the grouping column will be grouped
together.

Note: If no GROUP BY clause is specified (only group functions and constants appear in the
SELECT clause), the default grouping becomes the entire result set.

15
Grouping is done based on Class column. There are three different values in the
class column so three groups will be formed and for every group one row will be
displayed in the output.

16
First query is wrong because you can not display ItemName if it has not
been used for grouping.

The vice versa is not TRUE.(There can be a column which is used in group
by clause but not used in select clause)

17
The grouping is done based on two column Class and ItemName.
The query will first find the no of different values in Class column, in this case it is three. So three
groups will be formed because of Class column.
Now for every such group there will be subgrouping because of ItemName.

In our example for group of Class A items there are three different ItemName so three subgroups
will be formed.
For group of Class B items there is only one ItemName so no subgrouping will happen
For group of Class C items there is only one ItemName so no subgroup will be formed

18
Limiting Grouped data with HAVING:

Group functions cannot be used in the WHERE clause.

A SQL statement can have both a WHERE clause and a HAVING clause. WHERE filters data
before grouping; HAVING filters data after grouping.

19
Limiting Grouped data with HAVING:

Group functions cannot be used in the WHERE clause.

A SQL statement can have both a WHERE clause and a HAVING clause. WHERE filters data
before grouping; HAVING filters data after grouping.

20
Query is wrong because you cannot use SupplierId in Having Clause if it has not been used for
grouping.

The vice versa is not TRUE.(There can be a column which is used in group by clause but not
used in Having clause)

21
The results of two independent SELECT statements can be worked with using the SET operation
– UNION. By default, UNION returns only distinct values. Union is like an “OR” operation. If the
tuple occurs in relation 1 or relation 2, it is selected.

23
24
Union all:

Returns all rows including duplicates selected by either query.

25
Union all:

Returns all rows including duplicates selected by either query.

26
27
An intersection is an AND operation. It retrieves those tuples which are present in both relation

28
This is the difference operation. It retrieves unique tuples which are present in relation 1 but not
in relation 2.

29
30
Where clause:
This will retrieve only those rows of the table which satisfy the condition in the where clause. Any
logical conditions of the where clause use the comparison operators. Rows are returned or
operated upon where the data satisfies the logical condition (s) of the where clause. Column
names or expressions can be used in the where clause but not column alias names.

31
32
In relational databases, data is spread over multiple tables. Sometimes we may want data from
two or more tables. A join is an operation which combines results from two or more tables.

33
Cartesian Joins:

A Cartesian join occurs when data is selected from two or more tables and there is no common
relation specified in the WHERE clause. If you do not specify a join condition for the tables listed
in the FROM clause, Oracle joins each row from the first table to every row in the second table.

If the first table has 3 rows and the second table has 4 rows, the result will have 12 rows.

34
Inner Joins:

The most common operator used to relate two tables is the equality operator (=). If you relate
two tables using an equality operator, it is an equality join, also known as equijoin. This type of
join combines rows from two tables that have equivalent values for the specified columns. A
simple join is also known as an inner join, because it returns only the rows that satisfy the join
condition.

35
Here the where clause is based on the equality condition “=“. Hence it is called equi join

36
37
38
39
40
41
The inner join takes into account only those non NULL rows from the tables involved. If you want
the result to include even those rows having a NULL for a particular row in the selected column,
then go for an outer join. The syntax for representing this is slightly different in each RDBMS
product. What follows in the next slide is the oracle style.

42
43
44
45
The (+) symbol is next to the column which needs to be expanded to include null values also. In
the example above, there may be some customers who have not made any orders, so if we
select their names from the customers table (the second table based on int position in the query),
the corresponding order detail would be null. Even then such values have to be selected . That’s
what is indicated. A typical output would look like:

ORDER_NUM CUST_NAME
--------- ----------
5 radha
first corp
jcp inc.

46
When you wish to join a table with itself based on some criteria, use the concept of synonyms.
Treat the table as two different tables by giving synonyms

47
48
49
50
51
52
53
54

Você também pode gostar