Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture7:Data Manipulation in SQL Advanced Queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture7 1.

Similar presentations


Presentation on theme: "Lecture7:Data Manipulation in SQL Advanced Queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture7 1."— Presentation transcript:

1 Lecture7:Data Manipulation in SQL Advanced Queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture7 1

2 The Process of Database Design Real World Domain Conceptual model (ERD) Relational Data Model Create schema (DDL) Load Data (DML) Lecture7 2

3 Tables in the Examples Customer(custNo, custName, custSt, custCity, age) Product(prodNo, prodName, prodDes, price) Orders(ordNo, ordDate, custNo, prodNo, quantity) Where custName, custSt, custCity, prodName, prodDes are strings ordDate is date Others are numbers Lecture7 3

4 Sample Data in Customer Table custNocustNamecustStcustCityage 1C1Olaya StJeddah20 2C2Mains StRiyadh30 3C3Mains RdRiyadh25 4C4Mains RdDammam 5C5Mains RdRiyadh Lecture7 4

5 Sample Data in Product Table prodNoprodNameprodDesprice 100P0Food100 101P1healthy food100 102P2200 103P3self_raising flour,80%wheat 300 104P4network 80x300 Lecture7 5

6 Sample Data in Orders Table ordNoordDatecustNoprodNoquantity 101-jan-200311002 202-jan-200311011 301-jan-200321021 401-jan-200331002 503-jan-200311011 606-mar-2003210010 Lecture7 6

7 Aggregate Functions COUNT - returns the number of selected values SUM - returns the sum of selected (numeric) values AVG - returns the average of selected (numeric) values MIN - returns the minimum of selected values MAX - returns the maximum of selected values Lecture7 7

8 Use of COUNT(column_name) The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column Syntax Lecture7 8 SELECT COUNT(column_name) FROM table_name;

9 Use of COUNT(column_name) Example 1: List the number of products in the product table SELECT count(prodNo) FROM product; Example 2: List the number of product descriptions in the product table SELECT count(prodDes) FROM product; Note: count(prodDes) does not count rows that have NULL value for prodDes. 5 4 Lecture7 9 prodNoprodNameprodDesprice 100P0Food100 101P1healthy food100 102P2200 103P3self_raising flour,80%wheat 300 104P4network 80x300

10 Use of COUNT(*) The COUNT(*) function returns the number of records in a table (NULL values will be counted) Syntax Lecture7 10 SELECT COUNT(*) FROM table_name;

11 Use of COUNT (*) Example 1: How many products are there in the product table? SELECT count(*) FROM product; Example 2: How many products are priced at 300? SELECT count(*) FROM product WHERE price =300; Note: count(*) also count rows that have NULL values 5 2 prodNoprodNameprodDesprice 100P0Food100 101P1healthy food100 102P2200 103P3self_raising flour,80%wheat 300 104P4network 80x300 Lecture7 11

12 Use of COUNT(DISTINCT column_name) The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column: Syntax Lecture7 12 SELECT COUNT(DISTINCT column_name) FROM table_name;

13 Use of COUNT(DISTINCT column_name) Example1: How many cities are the customers located in ? SELECT count(distinct custCity) from customer; Example 2: How many customers ordered products since 01/01/2003? SELECT count(distinct custNo) FROM orders WHERE ordDate >= '01-jan-2003'; 3 3 Lecture7 13 ordNoordDatecust No prodNoquantit y 101-jan-200311002 202-jan-200311011 301-jan-200321021 401-jan-200331002 503-jan-200311011 606-mar-2003210010

14 Use of SUM The SUM() function returns the total sum of a numeric column. Syntax Lecture7 14 SELECT SUM(column_name) FROM table_name;

15 Use of SUM Example Example 1: How many products were ordered by customer 1? SELECT SUM(quantity) FROM orders WHERE custNo =1; Example 2: How many orders were made by customer 1 and how many products did he order? SELECT count(ordNo), SUM(quantity) FROM orders WHERE custNo =1; 4 34 Lecture7 15 ordNoordDatecust No prodNoquantit y 101-jan-200311002 202-jan-200311011 301-jan-200321021 401-jan-200331002 503-jan-200311011 606-mar-2003210010

16 Use of Avg The AVG() function returns the average value of a numeric column. Syntax Lecture7 16 SELECT AVG(column_name) FROM table_name;

17 Use of Min, Max The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column. Syntax Lecture7 17 SELECT MIN(column_name), MAX (column_name) FROM table_name;

18 Example Use of AVG, MIN and MAX Example: list the minimum, maximum and average price of all products. SELECT MIN(price), MAX(price), AVG(price) FROM product; Note: if some product's price are NULLs, then SUM and AVG do not take those products into consideration. 100 300200 Lecture7 18 prodNoprodNam e prodDesprice 100P0Food100 101P1healthy food100 102P2200 103P3self_raising flour,80%wh eat 300 104P4network 80x300

19 Lecture7 19

20 Advanced queries (GROUP BY) General Syntax of SELECT command SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [,...] } FROMTableName [alias] [,...] [WHEREcondition] [GROUP BYcolumnList] [HAVINGcondition] [ORDER BYcolumnList] Order of the clauses cannot be changed. Only SELECT and FROM are mandatory

21 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 condition GROUP BY column_name;

22 Use of GROUP BY Use GROUP BY clause to get sub-totals. SELECT and GROUP BY closely integrated: each item in SELECT list must be single-valued per group, and SELECT clause may only contain: Column names in the group by clause Aggregate functions Constants Expression involving combinations of the above If WHERE is used with GROUP BY, WHERE is applied first, then groups are formed from rows satisfying condition.

23 Example 1 ( use of group by ) Orders find the total (total order) of each customer. use the GROUP BY statement to group the customers. SELECT Customer, SUM(OrderPrice) FROM Orders GROUP BY Customer; O_IdOrderDateOrderPriceCustomer 12008/11/121000Nora 22008/10/231600Sara 32008/09/02700Nora 42008/09/03300Nora 52008/08/302000Yara 62008/10/04100Sara

24 Example 1 The result ( output ): what happens if we omit the GROUP BY statement SELECT Customer,SUM(OrderPrice) FROM Orders; The result CustomerSUM(OrderPrice) Nora2000 Sara1700 Yara2000 CustomerSUM(OrderPrice) Nora5700 Sara5700 Nora5700 Nora5700 Yara5700 Sara5700

25 Example 2 List the quantity of each product ordered during Jan 2003. SELECT prodNo, sum(quantity) FROM orders WHERE ordDate>='01-jan-2003' AND ordDate<'01-Feb-2003' GROUP BY prodNo; prodNoSum ( quantity ) 1004 1012 1021 ordNoordDatecustNoprodNoquantity 101-jan-200311002 202-jan-200311011 301-jan-200321021 401-jan-200331002 503-jan-200311011 606-mar-2003210010

26 Example 3 return the minimum and maximum salaries for each department in the employees table SELECT deptNumber, MIN(salary), MAX (salary) FROM employees GROUP BY deptNumber ORDER BY deptNumber; department_idMIN(salary),MAX (salary) D14400060000 D24500058000

27 SELECT count(*) FROM EMPLOYEE; Example 1 : no grouping Without group by COUNT(*) returns the number of tuples in the table COUNT (*) = 5 Example 4 Grouping Output from Queries

28 Example 2 : group by SELECT deptNumber, count(*) FROM EMPLOYEE GROUP BYdeptNumber ORDER BY deptNumber; Grouping Output from Queries

29 Use of HAVING HAVING clause is designed for use with GROUP BY to restrict groups that appear in final result table. Similar to WHERE, but WHERE filters individual rows whereas HAVING filters groups. Column names in HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function. 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 ;

30 EXAMPLE 1 find if any of the customers have a total order of less than 2000 SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000; - Without Having CustomerSUM(OrderPrice) Sara1700 CustomerSUM(OrderPrice) Nora2000 Sara1700 Yara2000 O_IdOrderDateOrderPriceCustomer 12008/11/121000Nora 22008/10/231600Sara 32008/09/02700Nora 42008/09/03300Nora 52008/08/302000Yara 62008/10/04100Sara

31 Example 2 find if the customers “Nora" or “Yara" have a total order of more than 1500 SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer=‘Nora' OR Customer=‘Yara' GROUP BY Customer HAVING SUM(OrderPrice)>1500 CustomerSUM(OrderPrice) Nora2000 Yara2000 O_IdOrderDateOrderPriceCustomer 12008/11/121000Nora 22008/10/231600Sara 32008/09/02700Nora 42008/09/03300Nora 52008/08/302000Yara 62008/10/04100Sara

32 Example 3 List the product number and the quantity ordered for each product which has a total order of more than 2 in Jan 2003. SELECT prodNo, sum(quantity) FROM orders WHERE ordDate>='01-jan-2003' AND ordDate<'01-Feb-2003' GROUP BY prodNo HAVING sum(quantity)>2; prodNosum(quantity) 1004 ordNoordDatecustNoprodNoquantity 101-jan-200311002 202-jan-200311011 301-jan-200321021 401-jan-200331002 503-jan-200311011 606-mar-2003210010

33 Example 4 SELECT deptNumber, count(*) FROM EMPLOYEE GROUP BY deptNumber HAVING count(*)>2 ORDER BY deptNumber; deptNumbercount(*) D13

34 Lecture7 34

35 Transaction Control There are following commands used to control transactions: COMMIT: to save the changes. Syntax ROLLBACK: to rollback the changes. Syntax SAVEPOINT: creates points within groups of transactions in which to ROLLBACK Syntax Lecture7 35 COMMIT; ROLLBACK; SAVEPOINT SAVEPOINT_NAME;

36 Savepoint The SAVEPOINT statement names and marks the current point in the processing of a transaction Example: SQL> INSERT INTO AUTHOR VALUES ('A11l', 'john', 'garmany', '123-345-4567', '1234 here st', 'denver', 'CO','90204', '9999'); 1 row created. SQL> savepoint in_author; Savepoint created. SQL> INSERT INTO BOOK_AUTHOR VALUES ('A111', 'B130',.20); 1 row created. SQL> savepoint in_book_author; Savepoint created. SQL> INSERT INTO BOOK VALUES ('B130', 'P002', 'easy oracle sql', 'miscellaneous', 9.95, 1000, 15, 0, '', to_date ('02-20-2005','MM-DD-YYYY')); 1 row created. SQL> rollback to in_author; Rollback complete.

37 Commit and Rollback If you want to make your update permanent, use COMMIT; The COMMIT command is the command used to save changes invoked by a transaction to the database. The COMMIT statement erases any savepoints you marked since the last commit or rollback. You can see the changes when you query the tables you modified, but other users cannot see the changes until you COMMIT the work. If you change your mind or need to correct a mistake, you can use the ROLLBACK statement to roll back (undo) the changes. The ROLLBACK statement is the inverse of COMMIT statement. It undoes some or all database changes made during the current transaction. Lecture7 37

38 Example SQL> DELETE FROM CUSTOMER WHERE AGE = 25; 1 rows deleted SQL> COMMIT; SQL> DELETE FROM CUSTOMER WHERE AGE = 30; 1 rows deleted SQL> ROLLBACK; SQL> SELECT * FROM CUSTOMER; Lecture7 38 custNocustNamecustStcustCityage 1C1Olaya StJeddah20 2C2Mains StRiyadh30 3C3Mains RdRiyadh25 4C4Mains RdDammam 5C5Mains RdRiyadh custNocustNamecustStcustCityage 1C1Olaya StJeddah20 2C2Mains StRiyadh30 4C4Mains RdDammam 5C5Mains RdRiyadh

39 Lecture7 39

40 Inserting Data Using Queries You can insert the result of a query into a table For example, if you have a table Briscustomer which has the same structure as Customer, then you can use insert into Briscustomer select * from customer where custcity =‘Riyadh'; Lecture7 40 custNocustNamecustStcustCityage 1C1Olaya StJeddah20 2C2Mains StRiyadh30 3C3Mains RdRiyadh25 4C4Mains RdDammam 5C5Mains RdRiyadh custNocustNamecustStcustCityage 2C2Mains StRiyadh30 3C3Mains RdRiyadh25 5C5Mains RdRiyadh customer table Riyadhustomer table

41 Create Table Using Queries You can create a new table using the result of a query. Example: create table Briscustomer AS select custno, custName, custSt, age from customer where custcity =‘Riyadh'; will create a table Briscustomer which contains the custno, custName, custSt and age of customers from Riyadh. Lecture7 41

42 SQL data loader* For a table containing a large data set, INSERT command is not efficient to populate the table Oracle provides a data loader utility SQLLOADER which can be used to load data The data can be loaded from any text file and inserted into the database. SQL*Loader reads a data file and a description of the data which is defined in the control file Lecture7 42

43 SQL data loader* runs in OS, not in SQLplus table must be created first A typical SQL*Loader session takes as input a control file, which controls the behavior of SQL*Loader, and one or more datafiles. The output of SQL*Loader is an Oracle database (where the data is loaded), and a log file Lecture7 43

44 Table names and Column names Table name can be prefixed with the owner name. eg, if table product is owned by user John, you can use SELECT * FROM John.product; Column names can be prefixed with table name, eg SELECT product.prodNo FROM product; Lecture7 44

45 Alias ( important note ) SQL aliases are used to temporarily rename a table or a column heading. Syntax for Columns Syntax for Tables Lecture7 45 SELECT column_name AS alias_name FROM table_name; SELECT column_name(s) FROM table_name [AS] alias_name;

46 Alias ( important note ) Columns Alias: For example, you might wish to know how the combined total salary of all employees whose salary is above $25,000 / year. SELECT SUM(salary) AS "Total Salary" FROM employees WHERE salary > 25000; In this example, we've aliased the sum(salary) field as "Total Salary". As a result, "Total Salary" will display as the field name when the result set is returned. Table Alias: SELECT o.OrderID, o.OrderDate FROM Orders AS o; Lecture7 46

47 Exercise create table count_null ( a number, b number ); insert into count_null values ( 1, 5); insert into count_null values ( null, 7); insert into count_null values ( null, null); insert into count_null values ( 8, 2); select count(a) as "count_a_not_null", count(b) as "count_b_not_null", count(*) as "count_all“ from count_null ; Output : Lecture7 47

48 Lecture7 48

49 References “Database Systems: A Practical Approach to Design, Implementation and Management.” Thomas Connolly, Carolyn Begg. 5 th Edition, Addison-Wesley, 2009. Lecture7 49


Download ppt "Lecture7:Data Manipulation in SQL Advanced Queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture7 1."

Similar presentations


Ads by Google