Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering."— Presentation transcript:

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

2 2 Aggregate Functions COUNT : to count the number of rows of the relation COUNT : to count the number of rows of the relation MAX : to find the maximum value of the attribute (column) MAX : to find the maximum value of the attribute (column) MIN : to find the minimum value of the attribute MIN : to find the minimum value of the attribute SUM : to find the sum of values of the attribute provided the data type of the attribute is number SUM : to find the sum of values of the attribute provided the data type of the attribute is number AVG : to find the average of n values, ignoring null values AVG : to find the average of n values, ignoring null values STDDEV: standard deviation of n values ignoring null values STDDEV: standard deviation of n values ignoring null values VARIANCE : variance of n values ignoring null values VARIANCE : variance of n values ignoring null values

3 3 COUNT SELECT COUNT (*) SELECT COUNT (*) FROM table name; FROM table name; Returns No of rows of a relation Returns 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 relation Returns 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 of returns the number of rows of the relation, by eliminating duplicate values. rows of the relation, by eliminating duplicate values.

4 4 MAX Command SELECT MAX (attribute name) FROM table name; to get the maximum price of the product Select max(Price) From consumer_product; To get the product name with maximum price Table Consumer product Name Price TV 15,000 refrigerator 10,000 washing machine 17,000 mixer 3,500 Select Name from Select Name from Consumer_product Where price in (select max(price) from consumer_product);

5 5 Min Command SELECT MIN (attribute name) FROM table name; to get the Minimum price of the product Select min(Price) From consumer_product; To get the product name with minimum price Table Consumer product Name Price TV 15,000 refrigerator 10,000 washing machine 17,000 mixer 3,500 Select Name from Select Name from Consumer_product Where price in (select min(price) from consumer_product);

6 6 AVG Command The AVG command is used to get the average value of an attribute. The syntax of AVG command is: The AVG command is used to get the average value of an attribute. The syntax of AVG command is: SELECT AVG (attribute name) SELECT AVG (attribute name) FROM table name; FROM table name;

7 7 GROUP BY Function The GROUP BY clause is used to group rows to compute group-statistics. The GROUP BY clause is used to group rows to compute group-statistics. It is to be noted that when the GROUP BY clause is present, then It is to be noted that when the GROUP BY clause is present, then the SELECT clause may include only the columns that appear in the GROUP BY clause the SELECT clause may include only the columns that appear in the GROUP BY clause and aggregate functions. and aggregate functions. The general Form The general Form SELECT attribute name, aggregate function FROM table name GROUP BY attribute name;

8 8 Example Group by

9 9 Having Command The HAVING command is used to select the group. In other words HAVING restricts the groups according to a specified condition. The HAVING command is used to select the group. In other words HAVING restricts the groups according to a specified condition. The syntax of HAVING command is: The syntax of HAVING command is: SELECT attribute name, aggregate function FROM table name GROUP BY attribute name HAVING condition;

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

11 11 Join Operation Join operation is used to retrieve data from more than one table. Join operation is used to retrieve data from more than one table. Cartesian Product Cartesian Product If we have two tables A and B, then Cartesian product combines all rows in the table A with all rows in the table B. If we have two tables A and B, then Cartesian product combines all rows in the table A with all rows in the table B. If n1 is the number of rows in the table A and n2 is the number of rows in the table B. If n1 is the number of rows in the table A and n2 is the number of rows in the table B. Then the Cartesian product between A and B will have n1 × n2 rows. Then the Cartesian product between A and B will have n1 × n2 rows.

12 12 Example The relation doctor has the attribute ID, name and department. The relation doctor has the attribute ID, name and department. The relation nurse has three attributes NID, name and department. The relation nurse has three attributes NID, name and department.

13 13 Joints Cartesian Product Cartesian Product Select * from Doctor, Nurse; Select * from Doctor, Nurse; Gives all fields and all rows (12 Rows) Gives all fields and all rows (12 Rows)

14 14 Equijoin The join condition is based on equality between values in the common columns. The join condition is based on equality between values in the common columns. Called also: Called also: simple joins or inner joins simple joins or inner joins

15 15 Outer Joint In inner join, we select rows common to the participating tables to a join. In inner join, we select rows common to the participating tables to a join. What about the cases where we are interested in selecting elements in a table regardless of whether they are present in the second table? What about the cases where we are interested in selecting elements in a table regardless of whether they are present in the second table? We will now need to use the SQL OUTER JOIN command We will now need to use the SQL OUTER JOIN command

16 16 Outer Joint The syntax for performing an outer join in SQL is database-dependent. The syntax for performing an outer join in SQL is database-dependent. For example, in Oracle, we will place an "(+)" in the WHERE clause on the other side of the table for which we want to include all the rows For example, in Oracle, we will place an "(+)" in the WHERE clause on the other side of the table for which we want to include all the rows

17 17 Example 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_namestore_name EastBoston EastNew York WestLos Angeles WestSan Diego

18 18 Example SELECT A1.store_name, SUM(A2.Sales) SALES SELECT A1.store_name, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name (+) WHERE A1.store_name = A2.store_name (+) GROUP BY A1.store_name GROUP BY A1.store_name store_name SALES store_name SALES Boston $700 Boston $700 New York New York Los Angeles $1800 Los Angeles $1800 San Diego $250 San Diego $250

19 19 Set Operations UNION, INTERSECTION, and the MINUS (Difference) operations are considered as SET operations. UNION, INTERSECTION, and the MINUS (Difference) operations are considered as SET operations. DELL_ DESKTOP IBM_DESKTOP

20 20 UNION command The union of two relations IBM DESKTOP, DELL DESKTOP is given The union of two relations IBM DESKTOP, DELL DESKTOP is given UNION command eliminates duplicate values. UNION command eliminates duplicate values. In order to get the duplicate In order to get the duplicate values, we can use UNION ALL command values, we can use UNION ALL command

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

22 22 MINUS Operation If R and S are two union compatible relations then If R and S are two union compatible relations then R–S returns the tuples that are present in R but not in S. R–S returns the tuples that are present in R but not in S. S–R returns the tuples that are present in S but not in R. S–R returns the tuples that are present in S but not in R. It is to be noted that MINUS operation is not commutative That is R–S # S–R. It is to be noted that MINUS operation is not commutative That is R–S # S–R.

23 23 Minus


Download ppt "1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering."

Similar presentations


Ads by Google