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

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

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

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

Lec

ture

7Lecture7:Data Manipulation in SQLAdvanced Queries

Prepared by L. Nouf Almujally

Ref. Chapter5

1

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

Lec

ture

7

The Process of Database Design

Real World Domain

Conceptual model (ERD)

Relational Data Model

Create schema

(DDL)

Load Data(DML)

2

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

Lec

ture

7

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 stringsordDate is dateOthers are numbers 3

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

Lec

ture

7

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

4

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

Lec

ture

7

Sample Data in Product Table

prodNo prodNam

eprodDes 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 3005

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

Lec

ture

7

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

6

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

Lec

ture

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

7

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

Lec

ture

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

8

SELECT COUNT(column_name) FROM table_name;

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

Lec

ture

7

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

9

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

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

Lec

ture

7

Use of COUNT(*)

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

• Syntax

10

SELECT COUNT(*) FROM table_name;

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

Lec

ture

7

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

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

11

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

Lec

ture

7

Use of COUNT(DISTINCT column_name)

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

• Syntax

12

SELECT COUNT(DISTINCT column_name) FROM table_name;

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

Lec

ture

7

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

313

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

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

Lec

ture

7

Use of SUM

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

• Syntax

14

SELECT SUM(column_name) FROM table_name;

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

Lec

ture

7

Use of SUM Example

Example 1: How many products 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 did he order?

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

4

3 415

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

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

Lec

ture

7

Use of Avg

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

• Syntax

16

SELECT AVG(column_name) FROM table_name;

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

Lec

ture

7

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

17

SELECT MIN(column_name), MAX (column_name) FROM table_name;

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

Lec

ture

7

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 300 200

18

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

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

Lec

ture

7

19

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

Advanced queries (GROUP BY)

• General Syntax of SELECT command

SELECT [DISTINCT | ALL] {* | [columnExpression [AS newName]] [,...] }

FROM TableName [alias] [, ...][WHERE condition][GROUP BY columnList] [HAVING condition][ORDER BYcolumnList]

• Order of the clauses cannot be changed.

• Only SELECT and FROM are mandatory

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

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;

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

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.

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

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_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

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

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 2000Sara 1700Yara 2000

Customer SUM(OrderPrice)

Nora 5700Sara 5700Nora 5700Nora 5700Yara 5700Sara 5700

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

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;

prodNo Sum ( quantity )

100 4

101 2

102 1

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

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

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_id MIN(salary), MAX (salary) D1 44000 60000D2 45000 58000

Employee No. First Name Last Name Dept Number SalaryE1 Mandy Smith D1 50000E2 Daniel Hodges D2 45000E3 Shaskia Ramanthan D2 58000E4 Graham Burke D1 44000E5 Annie Nguyen D1 60000

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

SELECT count(*)FROM EMPLOYEE;

Example 1 : no grouping

Employee No. First Name Last Name Dept Number SalaryE1 Mandy Smith D1 50000E2 Daniel Hodges D2 45000E3 Shaskia Ramanthan D2 58000E4 Graham Burke D1 44000E5 Annie Nguyen D1 60000

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

COUNT(*) = 5

Example 4

Grouping Output from Queries

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

Employee No. First Name Last Name Dept Number SalaryE1 Mandy Smith D1 50000E4 Graham Burke D1 44000E5 Annie Nguyen D1 60000E2 Daniel Hodges D2 45000E3 Shaskia Ramanthan D2 58000

Dept Number Count(*)D1 3D2 2

Example 2 : group by

SELECT deptNumber, count(*) FROM EMPLOYEE

GROUP BY deptNumber ORDER BY deptNumber;

Grouping Output from Queries

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

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 ;

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

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

Customer SUM(OrderPrice)

Sara 1700

Customer SUM(OrderPrice)

Nora 2000

Sara 1700

Yara 2000

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

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

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

Customer SUM(OrderPrice)Nora 2000Yara 2000

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

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

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;

prodNo sum(quantity) 100 4

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

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

Example 4

SELECT deptNumber, count(*)FROM EMPLOYEE GROUP BY deptNumber

HAVING count(*)>2 ORDER BY deptNumber;

deptNumber count(*)D1 3

Employee No. First Name Last Name Dept Number SalaryE1 Mandy Smith D1 50000E2 Daniel Hodges D2 45000E3 Shaskia Ramanthan D2 58000E4 Graham Burke D1 44000E5 Annie Nguyen D1 60000

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

Lec

ture

7

34

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

Lec

ture

7

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 35

COMMIT;

ROLLBACK;

SAVEPOINT SAVEPOINT_NAME;

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

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.

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

Lec

ture

7

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.

37

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

Lec

ture

7

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;

38

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

custNo custName custSt custCity age

1 C1 Olaya St Jeddah 20

2 C2 Mains St Riyadh 30

4 C4 Mains Rd Dammam

5 C5 Mains Rd Riyadh

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

Lec

ture

7

39

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

Lec

ture

7

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

40

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

custNo custName custSt custCity age

2 C2 Mains St Riyadh 30

3 C3 Mains Rd Riyadh 25

5 C5 Mains Rd Riyadh

customer table

Riyadhustomer table

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

Lec

ture

7

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.

41

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

Lec

ture

7

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

42

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

Lec

ture

7

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

43

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

Lec

ture

7

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;

44

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

Lec

ture

7

Alias ( important note )

• SQL aliases are used to temporarily rename a table or a column heading.

• Syntax for Columns

• Syntax for Tables

45

SELECT column_name AS alias_nameFROM table_name;

SELECT column_name(s)FROM table_name [AS] alias_name;

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

Lec

ture

7

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;

46

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

Lec

ture

7

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 :

47

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

Lec

ture

7

48

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

Lec

ture

7

References

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

49