Home >Documents >Lecture9: Data Manipulation in SQL , Advanced SQL queries · Data Manipulation in SQL , Advanced...

Lecture9: Data Manipulation in SQL , Advanced SQL queries · Data Manipulation in SQL , Advanced...

Date post:11-Oct-2020
Category:
View:27 times
Download:3 times
Share this document with a friend
Transcript:
  • Lecture9: Data Manipulation in SQL , Advanced SQL

    queries

    Ref. Chapter5

    1

    Coll

    ege

    of

    Com

    pute

    r an

    d I

    nfo

    rmat

    ion S

    cien

    ces

    -In

    form

    atio

    n S

    yst

    ems

    Dep

    t.

    Prepared by L. Nouf Almujally & Aisha AlArfaj

    I S220 / I S 422 : Dat abase Fundam ent a l s

  • The Process of Database Design

    Real World Domain

    Conceptual model (ERD)

    Relational Data Model

    Create schema

    (DDL)

    Load Data

    (DML)

    Lec

    ture

    9

    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

    Lec

    ture

    9

    3

  • Sample Data in Customer Table

    custNo custName custSt custCity age

    1 C1 Olaya St Jeddah 20

    2 C2 Mains St Riyadh 30

    3 C3 Mains Rd Riyadh 25

    4 C4 Mains Rd Dammam

    5 C5 Mains Rd Riyadh

    Lec

    ture

    9

    4

  • Sample Data in Product Table

    prodNo prodName prodDes price

    100 P0 Food 100

    101 P1 healthy food 100

    102 P2 200

    103 P3 self_raising

    flour,80%wheat

    300

    104 P4 network 80x 300

    Lec

    ture

    9

    5

  • Sample Data in Orders Table

    ordNo ordDate custNo prodNo quantity

    1 01-jan-2003 1 100 2

    2 02-jan-2003 1 101 1

    3 01-jan-2003 2 102 1

    4 01-jan-2003 3 100 2

    5 03-jan-2003 1 101 1

    6 06-mar-2003 2 100 10

    Lec

    ture

    9

    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

    Lec

    ture

    9

    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

    Lec

    ture

    9

    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.

    Lec

    ture

    9

    9

    prodNo

    prodName

    prodDes price

    100 P0 Food 100

    101 P1 healthy food 100

    102 P2 200

    103 P3 self_raisingflour,80%wheat

    300

    104 P4 network 80x 300

    COUNT(PRODNO)

    -------------------------

    5

    COUNT(PRODDES)

    ------------------------

    4

  • Use of COUNT(*)

    • The COUNT(*) function returns the number of records in a table (NULL values will be counted)

    • Syntax

    Lec

    ture

    9

    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

    Lec

    ture

    9

    11

    prodNo

    prodName

    prodDes price

    100 P0 Food 100

    101 P1 healthy food 100

    102 P2 200

    103 P3 self_raisingflour,80%wheat

    300

    104 P4 network 80x 300

    COUNT(*)

    -------------------------

    5

    COUNT(*)

    ---------------

    2

  • Use of COUNT(DISTINCT column_name)

    • The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column:

    • Syntax

    Lec

    ture

    9

    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';

    Lec

    ture

    9

    13

    ordNo ordDate cust

    No

    prodNo quantit

    y

    1 01-jan-2003 1 100 2

    2 02-jan-2003 1 101 1

    3 01-jan-2003 2 102 1

    4 01-jan-2003 3 100 2

    5 03-jan-2003 1 101 1

    6 06-mar-2003 2 100 10

    COUNT(DISTINCT CUSTCITY)

    -----------------------------------------

    3

    COUNT(DISTINCT CUSTNO)

    ------------------------------------

    3

    custNo custNam

    e

    custSt custCity age

    1 C1 Olaya St Jeddah 20

    2 C2 Mains St Riyadh 30

    3 C3 Mains Rd Riyadh 25

    4 C4 Mains Rd Dammam

    5 C5 Mains Rd Riyadh

  • Use of SUM

    The SUM() function returns the total sum of a numeric column.

    • Syntax

    Lec

    ture

    9

    14

    SELECT SUM(column_name)FROM table_name;

  • Use of SUM Example

    Example 1: How many products pieces were ordered by customer 1?

    SELECT SUM(quantity) FROM ordersWHERE custNo =1;

    Example 2: How many orders were made by customer 1 and how many products pieces did he order?

    SELECT count(ordNo), SUM(quantity) FROM ordersWHERE custNo =1;

    Lec

    ture

    9

    15

    ordNo ordDate cust

    No

    prodNo quantit

    y

    1 01-jan-2003 1 100 2

    2 02-jan-2003 1 101 1

    3 01-jan-2003 2 102 1

    4 01-jan-2003 3 100 2

    5 03-jan-2003 1 101 1

    6 06-mar-2003 2 100 10

    SUM(QUANTITY)

    -----------------------

    4

    COUNT(ORDNO) SUM(QUANTITY)

    --------------------- ----------------------

    3 4

  • Use of Avg

    • The AVG() function returns the average value of a numeric column.

    • Syntax

    Lec

    ture

    9

    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

    Lec

    ture

    9

    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.

    Lec

    ture

    9

    18

    prodNo prodNam

    e

    prodDes price

    100 P0 Food 100

    101 P1 healthy food 100

    102 P2 200

    103 P3 self_raising

    flour,80%wh

    eat

    300

    104 P4 network 80x 300

    MIN(PRICE) MAX(PRICE) AVG(PRICE)

    ---------------- ---------------- ----------------

    100 300 200

  • Lec

    ture

    9

    19

  • Advanced queries (GROUP BY)

    • General Syntax of SELECT command

    SELECT [DISTINCT]

    {* | [columnExpression, ……. }

    FROM TableName

    [WHERE condition]

    [GROUP BY columnList]

    [HAVING condition]

    [ORDER BY columnList]

    • Order of the clauses cannot be changed.

    • Only SELECT and FROM are mandatory

    Lec

    ture

    9

    20

  • 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;

    Lec

    ture

    9

    21

  • Use of GROUP BY

    • Use GROUP BY clause to get sub-totals.

    • SELECT and GROUP BY closely integrated: each item in SELECTlist must be single-valued per group, and SELECT clause mayonly 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, thengroups are formed from rows satisfying condition.

    Lec

    ture

    9

    22

  • 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 OrdersGROUP BY Customer;

    O_Id OrderDate OrderPrice Customer

    1 2008/11/12 1000 Nora

    2 2008/10/23 1600 Sara

    3 2008/09/02 700 Nora

    4 2008/09/03 300 Nora

    5 2008/08/30 2000 Yara

    6 2008/10/04 100 Sara

    Nora

    Sara

    Yara Lec

    ture

    9

    23

  • Example 1

    • The result ( output ):

    what happens if we omit the GROUP BY statement

    SELECT Customer,SUM(OrderPrice) FROM Orders;

    The result

    Customer SUM(OrderPrice)

    Nora 2000

    Sara 1700

    Yara 2000

    Customer SUM(OrderPrice)

    Nora 5700

    Sara 5700

    Nora 5700

    Nora 5700

    Yara 5700

    Sara 5700

    Lec

    ture

    9

    24

  • 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

  • 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;

    Employee No. First Name Last Name Dept Number Salary

    E1 Mandy Smith D1 50000

    E2 Daniel Hodges D2 45000

    E3 Shaskia Ramanthan D2 58000

    E4 Graham Burke D1 44000

    E5 Annie Nguyen D1 60000D1

    D2

    DEPTNUMBER MIN(SALARY) MAX(SALARY)

    --------------------- ------------------ -------------------

    D1 44000 60000

    D2 45000 58000

    Lec

    ture

    9

    26

  • SELECT count(*)FROM EMPLOYEE;

    Example 1 : no grouping

    Employee No. First Name Last Name Dept Number Salary

    E1 Mandy Smith D1 50000

    E2 Daniel Hodges D2 45000

    E3 Shaskia Ramanthan D2 58000

    E4 Graham Burke D1 44000

    E5 Annie Nguyen D1 60000

    Without group by COUNT(*) returns the number of rows in the table

    COUNT(*)

    -------------

    5

    Example 4

    Grouping Output from Queries

    Lec

    ture

    9

    27

  • DEPTNUMBER COUNT(*)

    ---------------- -------------

    D1 3

    D2 2

    Employee No. First Name Last Name Dept Number Salary

    E1 Mandy Smith D1 50000

    E4 Graham Burke D1 44000

    E5 Annie Nguyen D1 60000

    E2 Daniel Hodges D2 45000

    E3 Shaskia Ramanthan D2 58000

    Example 2 : group by

    SELECT deptNumber, count(*)FROM EMPLOYEE

    GROUP BY deptNumberORDER BY deptNumber;

    Grouping Output from Queries

    Lec

    ture

    9

    28

  • 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 whereasHAVING filters groups.

    • Column names in HAVING clause must also appear in theGROUP BY list or be contained within an aggregate function.

    • SYNTAX

    SELECT column_name, aggregate_function(column_name)FROM table_name

    WHERE column_name operator valueGROUP BY column_name

    HAVING aggregate_function(column_name) operator value ;

    Lec

    ture

    9

    29

  • EXAMPLE 1

    • find if any of the customers have a total order of less than 2000

    SELECT Customer,SUM(OrderPrice)

    FROM OrdersGROUP BY Customer

    HAVING SUM(OrderPrice)

  • Example 2

    • find if the customers “Nora" or “Yara" have a total order of more than 1500

    SELECT Customer,SUM(OrderPrice)

    FROM OrdersWHERE Customer=‘Nora' OR Customer=‘Yara'

    GROUP BY CustomerHAVING SUM(OrderPrice)>1500 ;

    O_Id OrderDate OrderPrice Customer

    1 2008/11/12 1000 Nora

    2 2008/10/23 1600 Sara

    3 2008/09/02 700 Nora

    4 2008/09/03 300 Nora

    5 2008/08/30 2000 Yara

    6 2008/10/04 100 Sara

    CUSTOMER SUM(ORDERPRICE)

    ----------------- -------------------------

    NORA 2000

    YARA 2000

    Lec

    ture

    9

    31

  • Example 3

    • List the product number and the quantity ordered for each product which has a total quantity of more than 2 during Jan 2003.

    SELECT prodNo, sum(quantity)

    FROM orders

    WHERE ordDate>='01-jan-2003' AND ordDate2;

    ordNo ordDate custNo prodNo quantity

    1 01-jan-2003 1 100 2

    2 02-jan-2003 1 101 1

    3 01-jan-2003 2 102 1

    4 01-jan-2003 3 100 2

    5 03-jan-2003 1 101 1

    6 06-mar-2003 2 100 10

    100= 4

    101=2

    102 = 1

    PRODNO SUM(QUANTITY)

    ------------- ----------------------

    100 4

    Lec

    ture

    9

    32

  • Example 4• List the department number and the total number of employee in that

    department for each department that has more than two employees

    SELECT deptNumber, count(*)

    FROM EMPLOYEE

    GROUP BY deptNumber

    HAVING count(*)>2

    ORDER BY deptNumber;

    DEPTNUMBER COUNT(*)

    ---------------- --------------

    D1 3

    DEPTNUMBER COUNT(*)

    ---------------- -------------

    D1 3

    D2 2

    Employee No. First Name Last Name Dept Number Salary

    E1 Mandy Smith D1 50000

    E4 Graham Burke D1 44000

    E5 Annie Nguyen D1 60000

    E2 Daniel Hodges D2 45000

    E3 Shaskia Ramanthan D2 58000

    Lec

    ture

    9

    33

  • Lec

    ture

    9

    34

  • Inserting Data Using Queries

    • You can insert the result of a query into a table

    For example, if you have a table Product_Expensive which has the same structure as Product, then you can use

    Lec

    ture

    9

    35

    SQL> insert into Product_Expensive

    select *

    from product

    where price>=200;

    3 rows created.

    PRODNO PRODNAME PRODDES PRICE

    ------------ ---------------- -------------------------------- ----------

    102 P2 200

    103 P3 self_raising flour,80%wheat 300

    104 P4 network 80x 300

  • 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.

    • table must be created first

    Lec

    ture

    9

    36

  • SQL data loader*

    Lec

    ture

    9

    37

    Discard file : contains

    records that were not

    inserted into any table in

    the database

    Bad file : contains

    records that were

    rejected because the

    input format is invalid.

    Log file : records of

    successful SQL*Loader

    execution

    Database : where the

    data is loaded

    output

    Datafiles:

    Contains Data

    Records

    Control file :

    text file tells

    SQL*Loader

    where to insert

    the data.

    input

  • Lec

    ture

    9

    38

  • Lec

    ture

    9

    39

  • References

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

    Lec

    ture

    9

    40

of 40/40
Lecture9: Data Manipulation in SQL , Advanced SQL queries Ref. Chapter5 1 College of Computer and Information Sciences - Information Systems Dept. Prepared by L. Nouf Almujally & Aisha AlArfaj IS220 / IS422 : Database Fundamentals
Embed Size (px)
Recommended