23
1 SQL-3 SQL-3 Tarek El-Shishtawy Tarek El-Shishtawy Professor Ass. Of Computer Professor Ass. Of Computer Engineering Engineering

Tarek Ahmed Alsheshtawy_SQL-3 data base

Embed Size (px)

Citation preview

Page 1: Tarek Ahmed Alsheshtawy_SQL-3 data base

11

SQL-3SQL-3Tarek El-ShishtawyTarek El-Shishtawy

Professor Ass. Of Computer EngineeringProfessor Ass. Of Computer Engineering

Page 2: Tarek Ahmed Alsheshtawy_SQL-3 data base

22

Aggregate FunctionsAggregate Functions COUNTCOUNT : to count the number of rows of the : to count the number of rows of the

relationrelation MAXMAX : to find the maximum value of the attribute : to find the maximum value of the attribute

(column)(column) MINMIN : to find the minimum value of the attribute : to find the minimum value of the attribute SUMSUM : to find the sum of values of the attribute : to find the sum of values of the attribute

provided the data type of the attribute is numberprovided the data type of the attribute is number AVGAVG : to find the average of n values, ignoring : to find the average of n values, ignoring

null valuesnull values STDDEVSTDDEV: standard deviation of n values : standard deviation of n values

ignoring null valuesignoring null values VARIANCEVARIANCE : variance of n values ignoring null : variance of n values ignoring null

valuesvalues

Page 3: Tarek Ahmed Alsheshtawy_SQL-3 data base

33

COUNTCOUNT SELECT COUNT (*)SELECT COUNT (*) FROM table name;FROM table name;

Returns No of rows of a relationReturns No of rows of a relation SELECT COUNT (attribute name)SELECT COUNT (attribute name) FROM table name;FROM table name;

Returns No of rows of a relationReturns No of rows of a relation SELECT COUNT (DISTINCT attribute name)SELECT COUNT (DISTINCT attribute name) FROM table name;FROM table name;

returns the number ofreturns the number of rows of the relation, by eliminating duplicate values.rows of the relation, by eliminating duplicate values.

Page 4: Tarek Ahmed Alsheshtawy_SQL-3 data base

44

MAX CommandMAX CommandSELECT MAX (attribute name)SELECT MAX (attribute name)FROM table name;FROM table name;

to get the maximum price of the to get the maximum price of the productproduct

Select max(Price)Select max(Price)From consumer_product;From consumer_product;

To get the product name with To get the product name with maximum pricemaximum price

Table Consumer productTable Consumer productName Name Price Price TV TV 15,00015,000refrigerator refrigerator 10,00010,000washing machine washing machine

17,00017,000mixer mixer 3,5003,500

Select Name fromSelect Name from Consumer_product Where price in

(select max(price) from consumer_product);

Page 5: Tarek Ahmed Alsheshtawy_SQL-3 data base

55

Min CommandMin CommandSELECT MIN (attribute name)SELECT MIN (attribute name)FROM table name;FROM table name;

to get the Minimum price of the to get the Minimum price of the productproduct

Select min(Price)Select min(Price)From consumer_product;From consumer_product;

To get the product name with To get the product name with minimum priceminimum price

Table Consumer productTable Consumer productName Name Price Price TV TV 15,00015,000refrigerator refrigerator 10,00010,000washing machine washing machine

17,00017,000mixer mixer 3,5003,500

Select Name fromSelect Name from Consumer_product Where price in

(select min(price) from consumer_product);

Page 6: Tarek Ahmed Alsheshtawy_SQL-3 data base

66

AVG CommandAVG Command

The AVG command is used to get the The AVG command is used to get the average value of an attribute. The syntax average value of an attribute. The syntax of AVG command is:of AVG command is:

SELECT AVG (attribute name)SELECT AVG (attribute name)FROM table name;FROM table name;

Page 7: Tarek Ahmed Alsheshtawy_SQL-3 data base

77

GROUP BY FunctionGROUP BY Function The GROUP BY clause is used to group rows to The GROUP BY clause is used to group rows to

compute group-statistics. compute group-statistics. It is to be noted that when the GROUP BY It is to be noted that when the GROUP BY

clause is present, then clause is present, then the SELECT clause may include only the columns the SELECT clause may include only the columns

that appear in the GROUP BY clausethat appear in the GROUP BY clause and aggregate functions.and aggregate functions.

The general FormThe general FormSELECT attribute name, aggregate functionSELECT attribute name, aggregate functionFROM table nameFROM table nameGROUP BY attribute name;GROUP BY attribute name;

Page 8: Tarek Ahmed Alsheshtawy_SQL-3 data base

88

Example Group byExample Group by

Page 9: Tarek Ahmed Alsheshtawy_SQL-3 data base

99

Having CommandHaving Command

The HAVING command is used to select The HAVING command is used to select the group. In other words HAVING the group. In other words HAVING restricts the groups according to a restricts the groups according to a specified condition. specified condition.

The syntax of HAVING command is:The syntax of HAVING command is:SELECT attribute name, aggregate functionSELECT attribute name, aggregate functionFROM table nameFROM table nameGROUP BY attribute nameGROUP BY attribute nameHAVING condition;HAVING condition;

Page 10: Tarek Ahmed Alsheshtawy_SQL-3 data base

1010

ExampleExample

find the details of the department in which find the details of the department in which more than 90 students got placementmore than 90 students got placement

Page 11: Tarek Ahmed Alsheshtawy_SQL-3 data base

1111

Join OperationJoin OperationJoin operation is used to retrieve data from Join operation is used to retrieve data from

more than one table.more than one table.Cartesian ProductCartesian Product

If we have two tables A and B, then Cartesian If we have two tables A and B, then Cartesian product combines all rows in the table A with product combines all rows in the table A with all rows in the table B. all rows in the table B.

If n1 is the number of rows in the table A and If n1 is the number of rows in the table A and n2 is the number of rows in the table B. n2 is the number of rows in the table B.

Then the Cartesian product between A and B Then the Cartesian product between A and B will have n1 × n2 rows.will have n1 × n2 rows.

Page 12: Tarek Ahmed Alsheshtawy_SQL-3 data base

1212

ExampleExample The relation The relation doctordoctor has the attribute ID, name has the attribute ID, name

and department. and department. The relation The relation nursenurse has three attributes NID, has three attributes NID,

name and department.name and department.

Page 13: Tarek Ahmed Alsheshtawy_SQL-3 data base

1313

JointsJoints

Cartesian ProductCartesian ProductSelect * from Doctor, Nurse;Select * from Doctor, Nurse;Gives all fields and all rows (12 Rows)Gives all fields and all rows (12 Rows)

Page 14: Tarek Ahmed Alsheshtawy_SQL-3 data base

1414

EquijoinEquijoin The join condition is based on equality between The join condition is based on equality between

values in the common columns.values in the common columns. Called also:Called also:

simple joins or inner joinssimple joins or inner joins

Page 15: Tarek Ahmed Alsheshtawy_SQL-3 data base

1515

Outer JointOuter Joint

In inner join, we select rows common to In inner join, we select rows common to the participating tables to a join. the participating tables to a join.

What about the cases where we are What about the cases where we are interested in selecting elements in a table interested in selecting elements in a table regardless of whether they are present in regardless of whether they are present in the second table? the second table?

We will now need to use the SQL OUTER We will now need to use the SQL OUTER JOIN commandJOIN command

Page 16: Tarek Ahmed Alsheshtawy_SQL-3 data base

1616

Outer JointOuter Joint

The syntax for performing an outer join in The syntax for performing an outer join in SQL is database-dependent. SQL is database-dependent.

For example, in Oracle, we will place an For example, in Oracle, we will place an "(+)" in the WHERE clause on the other "(+)" in the WHERE clause on the other side of the table for which we want to side of the table for which we want to include all the rowsinclude all the rows

Page 17: Tarek Ahmed Alsheshtawy_SQL-3 data base

1717

ExampleExample Tab;e Store_Information store_name Sales Date

Los Angeles $1500 Jan-05-1999 San Diego $250 Jan-07-1999 Los Angeles $300 Jan-08-1999 Boston $700 Jan-08-1999

Table Geography

region_name store_name

East Boston

East New York

West Los Angeles

West San Diego

Page 18: Tarek Ahmed Alsheshtawy_SQL-3 data base

1818

ExampleExample SELECT A1.store_name, SUM(A2.Sales) SALESSELECT A1.store_name, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name (+)WHERE A1.store_name = A2.store_name (+) GROUP BY A1.store_nameGROUP BY A1.store_name

store_name store_name SALES SALES Boston Boston $700 $700 New York New York Los Angeles Los Angeles $1800 $1800 San Diego San Diego $250 $250

Page 19: Tarek Ahmed Alsheshtawy_SQL-3 data base

1919

Set OperationsSet Operations UNIONUNION, , INTERSECTIONINTERSECTION, and the , and the MINUSMINUS

(Difference) operations are considered as SET (Difference) operations are considered as SET operations.operations.

DELL_ DESKTOPDELL_ DESKTOP

IBM_DESKTOPIBM_DESKTOP

Page 20: Tarek Ahmed Alsheshtawy_SQL-3 data base

2020

UNION commandUNION command The union of two relations IBM DESKTOP, DELL The union of two relations IBM DESKTOP, DELL

DESKTOP is givenDESKTOP is given UNION command eliminates duplicate valuesUNION command eliminates duplicate values.. In order to get the duplicateIn order to get the duplicate values, we can use UNION ALL commandvalues, we can use UNION ALL command

Page 21: Tarek Ahmed Alsheshtawy_SQL-3 data base

2121

INTERSECTION OperationINTERSECTION Operation

The intersection operation returns the The intersection operation returns the tuples that are common to the two tuples that are common to the two relations.relations.

Page 22: Tarek Ahmed Alsheshtawy_SQL-3 data base

2222

MINUS OperationMINUS Operation

If R and S are two union compatible If R and S are two union compatible relations then relations then

R–S returns the tuples that are present in R–S returns the tuples that are present in R but not in S. R but not in S.

S–R returns the tuples that are present in S–R returns the tuples that are present in S but not in R. S but not in R.

It is to be noted that MINUS operation is It is to be noted that MINUS operation is not commutative That is R–S # S–R.not commutative That is R–S # S–R.

Page 23: Tarek Ahmed Alsheshtawy_SQL-3 data base

2323

MinusMinus