Vous êtes sur la page 1sur 3

1. a. Explain the differences between the WHERE and HAVING clause.

Explain the need to use


both clauses in some instances
WHERE: Where clause is used to filter out records before grouping from a result .The filter
occurs before any groupings are made. It can be used without the GROUP BY clause. The
WHERE clause selects rows before grouping and cannot contain aggregate functions
HAVING: It is used to filter values after they have been groups. Only columns or expression in
the group can be included in the HAVING clauses conditions. The HAVING clause is used to
filter values in a GROUP BY.HAVING clause cannot be used without the GROUP BY clause and
it selects rows after grouping. In opposite to where, HAVING clause can only compare results of
aggregate functions/column part of the group by. HAVING clause can contain aggregate function
Using Where and Clause in some Instance
o When SQL statements have both a WHERE clause and HAVING clause, WHERE clause
is applied first, then the results grouped, and finally the groups filtered according to the
HAVING clause. Having clause can only compare results of aggregated functions or
column part of the group by.
o Example Query : SELECT PurchaseorderID,
SUM(Priceperunit * Quantity) AS FinalPrice
FROM Purchase.PurchaseOrderDetail
WHERE LineTotal > 100
GROUP BY PurchaseorderID
HAVING SUM(Priceperunit * Quantity) > 10000;

b. Explain the difference between COUNT, COUNT DISTINCT, and COUNT(*) in SQL. When will these
three commands generate the same and different results?

COUNT COUNT DISTINCT COUNT(*)

Does not consider null values Does not consider null value It consider Null values

It considers only those tuples It also considers only those It considers all rows irrespective
which have a value tuples, which have value. of any rows contains values or
Additionally , it only considers not
once if more than one tuple has
same value

It returns total count of tuples by It returns total number of distinct Count(*) returns total number of
satisfying where condition tuples which satisfy where tuples satisfying the condition
condition. One than one tuple given using where statement
having same value can be
considered only once

Select count(discount) Select count(DISTINCT Select count(*)


discount)
From ITEM From ITEM
From ITEM
Where discount > 20; Where discount > 20;
Where discount > 20;
Result : It returns the number of It returns the number of items
items having discount more It returns the number of items having discount more than $20.
than $20 having discount more than $20.
For those items having same If the table ITEM contains any
discount it count only once Null tuples, they will be
considered as satisfying tuples
These three clause gives same results when there is no null value and no duplicate records in the table.
However, if table contains null value and duplicate records, these clauses will give different results.

1.c. Can an outer join be easily implemented when joining more than two tables?
Why or why not? How does Oracle address this issue?
Ans: Outer join is of 3 types
Left Outer Join
Right Outer Join
Full outer Join

In all outer join, joining more than three tables is not possible in one join statement but can be done in
multiple join statements. The OUTER JOIN syntax does not apply easily to a join condition of more than
two tables.

For example:

Select * from Table A

Left Outer Join Table B on A. column1 = B. Column1

Left Outer Join Table C on B. Column1 = C. Column1

In above Left outer join Query the all records from table B will be joined with matching records of table C
and result set will again get joined with all record from table A. The result set will have all records from
table A matching with result set of join between B and C.

Similar case is with Right outer join and full outer join in Oracle.

Create or replace trigger

CREATE or Replace TRIGGER T1 ON Room

AFTER INSERT, UPDATE

AS

IF EXISTS (

SELECT TYPE, PRICE

FROM Room

Where Price <=100

AND type = double room

)
BEGIN

RAISERROR (' Double room must be greater than 100', 16, 1);

ROLLBACK TRANSACTION;

RETURN

END;

Vous aimerez peut-être aussi