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

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

Aggregate Functions Presenter: Sushma Bandekar CS 157(A) – Fall 2006.
Virtual training week 4 structured query language (SQL)
9 Copyright © 2004, Oracle. All rights reserved. Managing Data.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Introduction to SQL J.-S. Chou Assistant Professor.
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
STRUCTURED QUERY LANGUAGE (SQL)
Lecture8:Data Manipulation in SQL Advanced SQL queries Ref. Chapter5 Lecture8 1.
INLS 623– S QL Instructor: Jason Carter. SQL SELECT DISTINCT SELECT DISTINCT column_name, column_name FROM table_name ;
Lecture6:Data Manipulation in SQL, Simple SQL queries Prepared by L. Nouf Almujally Ref. Chapter5 Lecture6 1.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
SQL FUNDAMENTALS SQL ( Structured Query Language )
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Database structure and space Management. Segments The level of logical database storage above an extent is called a segment. A segment is a set of extents.
Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values.
 Industry standard for interacting with relational databases  Statements to Enter, Retrieve, Modify, Remove & Display data stored in database tables.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL Query Getting to the data ……..
MySQL DML Commands By Prof. B.A.Khivsara
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
References: Text Chapters 8 and 9
Introduction to Oracle9i: SQL
Structured Query Language – The Basics
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
HAVING,INDEX,COMMIT & ROLLBACK
Contents Preface I Introduction Lesson Objectives I-2
Section 4 - Sorting/Functions
Chapter Name SQL: Data Manipulation
Concept of grouping SELECT statement have:
Presentation transcript:

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

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

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

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

Sample Data in Product Table prodNoprodNameprodDesprice 100P0Food P1healthy food P P3self_raising flour,80%wheat P4network 80x300 Lecture7 5

Sample Data in Orders Table ordNoordDatecustNoprodNoquantity 101-jan jan jan jan jan mar Lecture7 6

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

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;

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 100P0Food P1healthy food P P3self_raising flour,80%wheat P4network 80x300

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;

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 100P0Food P1healthy food P P3self_raising flour,80%wheat P4network 80x300 Lecture7 11

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;

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 jan jan jan jan mar

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

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 jan jan jan jan mar

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

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;

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 Lecture7 18 prodNoprodNam e prodDesprice 100P0Food P1healthy food P P3self_raising flour,80%wh eat P4network 80x300

Lecture7 19

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

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;

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.

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

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

Example 2 List the quantity of each product ordered during Jan SELECT prodNo, sum(quantity) FROM orders WHERE ordDate>='01-jan-2003' AND ordDate<'01-Feb-2003' GROUP BY prodNo; prodNoSum ( quantity ) ordNoordDatecustNoprodNoquantity 101-jan jan jan jan jan mar

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) D D

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

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

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 ;

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

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

Example 3 List the product number and the quantity ordered for each product which has a total order of more than 2 in Jan 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 jan jan jan jan mar

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

Lecture7 34

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;

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', ' ', '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 (' ','MM-DD-YYYY')); 1 row created. SQL> rollback to in_author; Rollback complete.

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

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

Lecture7 39

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

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

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

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

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

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;

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

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

Lecture7 48

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