Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concept of grouping SELECT statement have:

Similar presentations


Presentation on theme: "Concept of grouping SELECT statement have:"— Presentation transcript:

1

2 Concept of grouping SELECT statement have:
Retrieved all the rows from table. Retrieved selected rows from tables with the use of a WHERE clause, which returns only those rows meet the conditions specified. Retrieved unique rows from the table, with the use of DISTINCT clause. Retrieved rows in the sorted order. i.e. ascending or descending order, as specified, with the use of ORDER BY clause.

3 The concept of grouping
Other than the above clauses, there are two other clauses, which facilitate selective retrieval of rows. These are the GROUP BY and HAVING clause. These are similar to ORDER BY and WHERE clause, except that they act on record sets, and not on individual records

4 The GROUP BY Statement The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. Syntax : SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name

5 Example O_ID OrderDate OrderPrice Customer 1 23/10/2008 1000 Hansen 2 02/09/2008 1600 Nilsen 3 03/09/2008 700 4 30/08/2008 300 5 04/10/2008 2000 Jensen 6 12/11/2008 100 Now we want to find the total sum (total order) of each customer.

6 Example We will have to use the GROUP BY statement to group the customers. We use the following SQL statement: SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer

7 Example Output : Customer SUM(Order Price) Hansen 2000 Nilsen 1700
Jensen

8 Let's see what happens if we omit the GROUP BY statement:
SELECT Customer,SUM(OrderPrice) FROM Orders Customer SUM(Order Price) Hansen 5700 Nilsen Jensen

9 GROUP BY More Than One Column
We can also use the GROUP BY statement on more than one column, like this: SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders GROUP BY Customer,OrderDate

10 The HAVING Clause The HAVING clause can be used to find unique values in whichd to SQL because DISTINCT does not apply. Syntax : SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value

11 SQL HAVING Example We have the following "Orders" table: O_ID
OrderDate OrderPrice Customer 1 23/10/2008 1000 Hansen 2 02/09/2008 1600 Nilsen 3 03/09/2008 700 4 30/08/2008 300 5 04/10/2008 2000 Jensen 6 12/11/2008 100

12 Example Now we want to find if any of the customers have a total order of less than 2000. We use the following SQL statement: SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000 Output : Customer SUM(OrderPrice) Nilsen 1700

13 Example Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than 1500. We add an ordinary WHERE clause to the SQL statement: SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500

14 Output The result-set will look like this: Customer SUM(OrderPrice)
Hansen 2000 Jensen

15 SUBQUERY A subquery is a form of SQL statement that appears inside another SQL statement. It is also termed as nested query. The statement containing a subquery is called a parent statement. The parent statement uses the rows returned by the subquery.

16 SUB QUERY It can be used for the following
To Insert records in a target table. To create tables and insert records in the table created. To Update records in a target table. To create views. To provide values for conditions in WHERE, HAVING, IN and so on used with SELECT, UPDATE AND DELETE statements.

17 Example Retrieve the salary of customer named ‘CHINTAN’.
Here, we consider two tables but there is no relation between two tables i.e. primary key, foreign key.

18

19 problem List the customer holding fixed deposits in the bank of amount less than Here, consider three tables, which are in next slide. Look first Structure and data.

20

21 SOLUTION SELECT EMP_NAME FROM EMP_MASTER WHERE EMP_ID IN (
SELECT CUST_NO FROM ACCT_FD_CUST_DETAILS WHERE ACCT_FD_NO IN ( SELECT FD_SER_NO FROM FD_DETAILS WHERE AMOUNT < 30000));

22 OUTPUT EMP_DETAILS CHINTAN SANJAY NITIN


Download ppt "Concept of grouping SELECT statement have:"

Similar presentations


Ads by Google