its232 introduction to database management systems
DESCRIPTION
ITS232 Introduction To Database Management Systems. Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah | A2-3039 | ext:2561 | [email protected] | 012-7760562 |. CHAPTER 7 An Introduction To SQL Part 2: DDL & DML. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/1.jpg)
ITS232Introduction To Database Management Systems
Siti Nurbaya Ismail
Faculty of Computer Science & Mathematics,
Universiti Teknologi MARA (UiTM), Kedah
| A2-3039 | ext:2561 | [email protected] | 012-7760562 |
CHAPTER 7An Introduction To SQL
Part 2: DDL & DML
![Page 2: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/2.jpg)
Objectives
To be able to apply SQL command. How to use DML in SQL
2 major components:1. Data Definition Language (DDL)
defining database structure. allows database objects such as schemas, domains,
tables, views and indexes to be created and destroyed.
2. Data Manipulation Language (DML) retrieving and updating data. used to populate and query the tables. data manipulation.
2
![Page 3: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/3.jpg)
Data Defination Language (DDL)Defining database structure/schema
3
![Page 4: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/4.jpg)
4
Chapter 7: An Introduction To SQL7.3 Data Definition Languages (DDL)
Data Definition SQL commands describe the database structure
or schema:COMMAND/OPTION
DESCRIPTION
CRATE SCHEMA AUTHORIZATION
Creates a new database schema
CREATE TABLE Create a new table in the user’s database schema
NOT NULL Constraints that ensures that a column doesn’t
have a null valueUNIQUE Constraints that ensures that a column
doesn’t have a duplicate values
ALTER TABLE Modify a table definition
DROP TABLE Permanently delete a table and its structure
![Page 5: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/5.jpg)
Chapter 7: An Introduction To SQL7.3 Data Definition Languages (DDL): Create Table
printerNO desc color staffNO
Create table ‘printer’ with printerNo as a primary key and staffNo as a foreign
key;Staff(staffNO,staffNAME,city,postcode)Printer(printerNO, desc, color, staffNO*)
CREATE TABLE printer(printerNO VARCHAR(5) NOT NULL PRIMARY KEY, desc VARCHAR(16), color VARCHAR(8), FOREIGN KEY (staffNO) REFERENCES staff(staffNO));
An empty table ‘printer’ is created with printerNO as primary key and staffNO as a foreign key:
![Page 6: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/6.jpg)
6
Chapter 7: An Introduction To SQL7.3 Data Definition Languages (DDL): Alter Table
ALTER command is used if there is a change on the table’s attribute.
The following SQL statement will add an attribute to table ‘staff’.ALTER TABLE staffADD telno char(11);
The following SQL statement will rename the column color in table ‘printer’.ALTER TABLE printer
RENAME COLUMN color TO printerColor;
The following SQL statement will change the width of desc’s colum to 25 digits in table ‘printer’.ALTER TABLE printerMODIFY (desc VARCHAR(25));
![Page 7: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/7.jpg)
Chapter 7: An Introduction To SQL7.3 Data Definition Languages (DDL): In Brief
7
SQL Statement
Descriptions
CREATE TABLE Create a new table- Primary Key
*NOT NULL:: a column does not have a null value
*UNIQUE :: a column does not have duplicate value- Foreign Key, attributes, data types of attributes, length of attributes
DROP TABLE Delete table and its structure-RESTRICT :: table with FK will not be deleted-CASCADE :: all attribute will be deleted
ALTER TABLE Change table attribute-ADD :: add table attribute-RENAME COLUMN :: rename an attribute -MODIFY ::modify attribute structure
![Page 8: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/8.jpg)
Data Manipulation Language (DML)Manipulate data
8
![Page 9: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/9.jpg)
Data Manipulation Language(DML)
Data Manipulation in SQL is an ability for manipulating the data to provide information needs
by users.
Manipulating the data referees to process of:selecting the datado some operation at the datauser view it or save it in the database
9
SELECT * FROM printerWHERE color = ‘red’ OR color = ‘blue’;
![Page 10: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/10.jpg)
DML: Queries: SELECT Statement
SELECT Statement• The SELECT statement allows you to find, retrieve, and display
data.
• To execute the SELECT statement on a table, you must be the table owner, have DBA or SYSADM security privileges, or have the SELECT privilege for that table.
• The result of SELECT statement is a set of rows known as the result set, which meets the conditions specified in the SELECT statement
SQL SELECT Syntax
NoteSQL is not case sensitive { SELECT is the same as select }
10
SELECT column_name(s)FROM table_name;
SELECT *FROM table_name;
![Page 11: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/11.jpg)
DML: Queries: SELECT Statement
printerNO description color price
P123 Dot-matrix Red 249.56
HP874 Laser Jet Blue 559.99
11
Select all data from table printer.
Select printerNO and price from table printer.
SELECT *FROM printer;
SELECT printerNO, priceFROM printer;
printerNO
price
P123 249.56
HP874 559.99
![Page 12: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/12.jpg)
DML: Queries: SELECT DISTINCT Statement
SELECT DISTINCT Statement• In a table, some of the columns may contain duplicate
values. • The DISTINCT keyword can be used to return only distinct
(different) values.
SQL SELECT DISTINCT Syntax
12
SELECT DISTINCT column_name(s)FROM table_name;
![Page 13: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/13.jpg)
DML: Queries: SELECT DISTINCT Statement
The following statement only will select the distinct values from the column named city from table staff.
13
SELECT DISTINCT cityFROM staff;
staffNO staffNAME city
ABC987 Nadz Kuala Lumpur
ABC988 Aina Jerantut
ABC999 Halimaton Jerantut
city
Kuala Lumpur
Jerantut
![Page 14: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/14.jpg)
SQL LIKE Operator
The LIKE operator is used in a WHERE statement to search for a specified pattern in a column.
SQL LIKE Syntax
Note
The LIKE operator is commonly used with SQL Wildcards
14
SELECT column_name(s)FROM table_nameWHERE column_name LIKE patern;
![Page 15: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/15.jpg)
SQL LIKE Operator
staffNO staffNAME city
ABC987 Nadz Kuala Lumpur
15
SELECT *FROM staffWHERE city LIKE “K%”;
The following select staffs living in a city start with “K” from table ‘staff’.
![Page 16: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/16.jpg)
SQL Wildcards
• SQL wildcards can be used when searching for data in a database.• SQL wildcards can substitute for one or more characters when
searching for data in a database.• SQL wildcards must be used with the SQL LIKE operator.• With SQL, the following wildcards can be used:
16
Wildcard Description
% A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist] or[!charlist]
Any single character not in charlist
![Page 17: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/17.jpg)
Wildcards: Using The % Wildcard
Select staff living in the city that start with ‘La’ from staff table.
Select staff living in the city that contain pattern ‘wi’ from staff table.
17
SELECT *FROM staffWHERE city LIKE "La%";
SELECT *FROM staffWHERE city LIKE "%wi%";
![Page 18: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/18.jpg)
Wildcards: Using The _ Wildcard
Select staff with a name that starts with any character, followed by ‘da’ from staff table.
Select staff with a name that starts with "S", followed by any character, followed by "ar", followed by any character, followed by "s" from staff table.
18
SELECT *FROM staffWHERE staffNANE LIKE "_da";
SELECT *FROM staffWHERE staffNANE LIKE "S_ar_s";
![Page 19: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/19.jpg)
Wildcards: Using The [charlist] Wildcard
Select staff with a name that starts with "a" or "s" or "p" from staff table.
Select staff name that do not starts with “"a" or "s" or "p" from staff table.
19
SELECT *FROM staffWHERE staffNANE LIKE "[asp]%";
SELECT *FROM staffWHERE staffNANE LIKE "[!asp]%";
![Page 20: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/20.jpg)
DML: Data Entry
The INSERT INTO command inserts new rows into a table.
When you insert data into a child table that has a foreign key linking it to a parent table, you must obey referential integrity rules.
This means you cannot insert a value into a child key that does not exist in the parent key unless it is a NULL value.
You must insert a new row into the parent key first.
To insert a string that contains a single quote, you must replace the single quote in the string with two consecutive single quotes.
20
![Page 21: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/21.jpg)
DML: Data Entry
SQL INSERT INTO Syntax• It is possible to write the INSERT INTO statement in two forms. • The first form doesn't specify the column names where the data will
be inserted, only their values:
• The second form specifies both the column names and the values to be inserted:
21
INSERT INTO table_nameVALUES (value1, value2,…);
INSERT INTO table_name (column1,column2,…)VALUES (value1, value2,…);
![Page 22: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/22.jpg)
DML: Data Entry
staffNO staffNAME city
ABC123 Norain Ipoh
22
The following insert values into table ‘staff’.
INSERT INTO staff VALUES (‘ABC123’, ‘Norain', ‘Ipoh’);
![Page 23: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/23.jpg)
DML: Deleting Table Rows
The DELETE command deletes all rows matching the search condition from a table.
You can only delete rows from a single table, and you cannot delete rows from the system tables.
To execute the DELETE command, you must be the table owner, have delete privilege on the table, or have DBA or SYSADM security privileges.
23
![Page 24: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/24.jpg)
DML: Deleting Table Rows
SQL DELETE Syntax
Note
Notice the WHERE clause in the DELETE syntax. The WHERE clause
specifies which record or records that should be deleted. If you omit the
WHERE clause, all records will be deleted!
24
DELETE FROM table_nameWHERE some column=some value
![Page 25: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/25.jpg)
DML: Deleting Table Rows
DELETE record as specified in WHERE clause.
The following example deletes all staff whose name begins with “Nisa" from the staff table.
The following example deletes staff number ABC125 from the staff table.
25
DELETE FROM staffWHERE staffNO=“ABC123” AND city=“Ipoh”;
DELETE FROM staffWHERE staffNAME LIKE “Nisa%”;
DELETE FROM staffWHERE staffNO =“ABC125”;
![Page 26: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/26.jpg)
DML: Making Changes to Data Items
The UPDATE command updates existing rows in a table.
When you update a column, the new column values must satisfy the column constraints and referential integrity.
If the column has a DEFAULT value defined, you can use the DEFAULT keyword to set the value of the column to the default value.
26
![Page 27: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/27.jpg)
DML: Making Changes to Data Items
SQL UPDATE Syntax
Note
Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
27
UPDATE table_nameSET column1=value,column2=value2,…WHERE some column=some value
![Page 28: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/28.jpg)
DML: Making Changes to Data Items
• Shows how to update the staff table and change the city value for staff named “Rosliza".
• Shows how to give a salary raise of 5% to staff named “Wafiy".
•Update description for printer with printerNO F380.
28
UPDATE staffSET city=“Bangi”WHERE staffNAME = “Rosliza”;
UPDATE staffSET salary=salary*1.05WHERE staffNAME = “Wafiy”;
UPDATE printerSET description=“KO”WHERE printerNO = “F380”;
![Page 29: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/29.jpg)
DML: COMMIT
The COMMIT statement terminates the current transaction and makes all
changes under the transaction persistent.
COMMIT is used for saving the data that has been changed permanently because whenever you perform any DML like UPDATE, INSERT or DELETE then you are required to write COMMIT at the end of all or every DML operation in order to save it permanently.
If you do not write COMMIT and you program crashes then your data will be restored into its previous condition.
The COMMIT statement has the following general format:
29
UPDATE printer SET indate = ‘15/01/2007' WHERE printerNO = 'F380'; COMMIT;
![Page 30: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/30.jpg)
DML: ROLLBACK
The ROLLBACK statement terminates the current transaction and cancel all changes made under the transaction.
ROLLBACK is used if you want to restore your data into its previous condition.
ROLLBACK can be write at any time after the DML queries has been written but remember once COMMIT has been written then you cannot rollback the data.
You can only rollback the DML queries that have been written after the last commit statement.
The ROLLBACK statement has the following general format:
30
INSERT INTO staffVALUES (“ABC990”, “Shafiza”, “Johor”)
ROLLBACK;
![Page 31: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/31.jpg)
DML: COMMIT & ROLLBACK
The concept of COMMIT and ROLLBACK is designed for data consistency
because many uses manipulate data of the same table, using the same database so the user must get updated data. That is why COMMIT and ROLLBACK are used for.
COMMIT to save whatever has been done. It is used to permanently store it in memory.
ROLLBACKto undo something. If we use roll-back, the particular changes made are undone.
31
![Page 32: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/32.jpg)
DML: Compound Statement with SELECT
Symbol
Meaning
= Equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
<> Not equal to*
*Some SQL version use != 32
– ANDto combine two search conditions which must be both true.
– ORto combine two search conditions when one or the other (or both) must be true
You can combine simple conditions with the logical operators
AND, OR, and NOT to form compound conditions.
![Page 33: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/33.jpg)
DML: Compound Statement with SELECT
Select only the staff live in Merbok AND age greater then or equal to 40 years old from table staff.
Select only the staff live in Penang OR age greater then or equal to 40 years old from table staff.
33
SELECT *FROM staffWHERE city=“Merbok” AND age >= 40;
SELECT *FROM staffWHERE city=“Penang” AND age >= 40;
![Page 34: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/34.jpg)
DML: Compound Statement with SELECT
Select only the staff live in Merbok OR Penang AND age greater then or equal to 40 years old from table staff.
34
SELECT *FROM staffWHERE (city=“Merbok” OR city=“Penang”) AND age >= 40;
![Page 35: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/35.jpg)
Example
Delete all biscuits with chocolate flavor from tblbiscuit.
Delete all order from customerC001 that have status cancel from tblorder.
35
tblcustomer(custNO, custNAME, custEMAIL)tblbiscuit(bisNO, bisNAME, bisFLAVOUR, bisPRICE)tblorder(orderNO,*custNO,*bisNO, orderadate, qty, status)
![Page 36: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/36.jpg)
Example
Change all status to confirm for biscuit BIS101 for customer C135.
Increase biscuit price 10% for biscuit with strawberry flavor.
36
tblcustomer(custNO, custNAME, custEMAIL)tblbiscuit(bisNO, bisNAME, bisFLAVOUR, bisPRICE)tblorder(orderNO,*custNO,*bisNO, orderadate, qty, status)
![Page 37: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/37.jpg)
Example
List down all customer name and email who order biscuit cornflakes.
List down all biscuit details being order by customer C010.
37
tblcustomer(custNO, custNAME, custEMAIL)tblbiscuit(bisNO, bisNAME, bisFLAVOUR, bisPRICE)tblorder(orderNO,*custNO,*bisNO, orderadate, qty, status)
![Page 38: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/38.jpg)
SQL Alias
• You can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.
• An alias name could be anything, but usually it is short.• Usually it is used in multiple table joint.
SQL Alias Syntax For Tables
SQL Alias Syntax For Columns
38
SELECT column_name(s)FROM table_nameAS alias_name
SELECT column_name AS alias_nameFROM table_name
![Page 39: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/39.jpg)
SQL Alias
The following statement list all the staff name that work at IT department.
39
staffNO staffNAME
city salary
departNO
ABC987 Nadz Kuala Lumpur
2300 0001
ABC988 Aina Jerantut 2500 0002
ABC989 Halimaton Jerantut 2200 0001
ABC990 Norain Johor 2000 0003
departNO departNAME
0001 IT
0002 Network
0003 Management
staff department
SELECT s.staffNAMEFROM staff AS s, department AS dWHERE (d.departNO=s.departNO) AND d.departNAME=“IT”;
staffNAME
Nadz
Halimaton
![Page 40: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/40.jpg)
SQL JOINs
• The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.
• Tables in a database are often related to each other with keys.
• A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.
40
![Page 41: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/41.jpg)
SQL JOINs
Different SQL JOINs
• JOIN / INNER JOINReturn rows when there is at least one match in both tables
• LEFT JOINReturn all rows from the left table, even if there are no matches in the right table
• RIGHT JOINReturn all rows from the right table, even if there are no matches in the left table
• FULL JOINReturn rows when there is a match in one of the tables
41
![Page 42: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/42.jpg)
SQL INNER JOIN
SQL INNER JOIN Syntax
NOTE
INNER JOIN is the same as JOIN.
42
SELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name = table_name2.column_name
![Page 43: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/43.jpg)
SQL INNER JOIN
The following statement list all the staff name that work at IT department.
43
staffNO staffNAME
city salary
departNO
ABC987 Nadz Kuala Lumpur
2300 0001
ABC988 Aina Jerantut 2500 0002
ABC989 Halimaton Jerantut 2200 0001
ABC990 Norain Johor 2000 0003
departNO departNAME
0001 IT
0002 Network
0003 Management
staff department
SELECT staff.staffNAME,depart.departNAMEFROM staffINNER JOIN departmentON department.departNO=staff.departNO;
staffNAME
Nadz
Halimaton
![Page 44: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/44.jpg)
DML: Two-Table Joins
Besides INNER JOIN, you can also use the following statements to combines two tables with join conditions.
44
staff(staffNO, staffNAME, city, salary, departmentNO*)department(departNO, departNAME)
SELECT s.staffNAMEFROM staff AS s, department AS dWHERE (d.departNO=s.departNO) AND d.departNAME=“IT”;
SELECT staff.staffNAME,department.departNAMEFROM staffINNER JOIN departmentON department.departNO=staff.departNO;
![Page 45: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/45.jpg)
DML: Multiple-Table Joins
A multiple table join is a join of more than two tables with join conditions for pairs of table. – A join condition is a comparison (relational operators) on two columns from
each table.
Following is a three table joins which selects all the customer name that order biscuit Tart Nenas Gunting.
45
customer(custNO, custNAME, custEMAIL)biscuit(bisNO, bisNAME, bisFLAVOUR, bisPRICE)order(orderNO,*custNO,*bisNO, orderadate, qty, status)
SELECT custNAMEFROM customer AS c, biscuit AS b, order AS o WHERE c.custNO=o.custNO AND o.bisNO=b.bisNO AND b.bisNAME=“Tart Nenas Gunting“;
![Page 46: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/46.jpg)
DML: Multiple-Table Joins
List down all customer name, biscuit name and order quantity that customer had order with confirm status.
46
tblcustomer(custNO, custNAME, custEMAIL)tblbiscuit(bisNO, bisNAME, bisFLAVOUR, bisPRICE)tblorder(orderNO,*custNO,*bisNO, orderadate, qty, status)
![Page 47: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/47.jpg)
DML: GROUP BY Statement
GROUP BY statement– produce summary data within a group.
a group is a set of rows that have the same values of group by columns.
a single row of aggregate results is produced for each group. the column you want to group results by is identified by its column
name.
– restrict what you can enter in the SELECT statement. – SELECT statement in GROUP BY statement must be one of the
following: An aggregate function, which produces a single value summarizing the rows in
the group. A grouping column which is listed in the GROUP BY statement. A constant. An expression involving a combination of the above. 47
![Page 48: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/48.jpg)
DML: GROUP BY Statement
SQL GROUP BY Syntax
48
SELECT column_name(s), aggregate_function(column_name)FROM table_nameGROUP BY column_name;
![Page 49: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/49.jpg)
DML: GROUP BY Statement
The following statement will output staff average salary group by city.
49
staffNO staffNAME city salary gender departNO
ABC987 Nadz Kuala Lumpur 2300 Male 0001
ABC988 Aina Jerantut 2500 Female 0002
ABC989 Halimaton Jerantut 2200 Female 0001
ABC990 Norain Johor 2000 Female 0003
staff
SELECT city, MIN(salary) AS minSALARYFROM staffGROUP BY city;
city minSALARY
Kuala Lumpur
2300
Jerantut 2200
Johor 2000
![Page 50: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/50.jpg)
DML: ORDER BY Keyword
The ORDER BY keyword • is used to sort the result-set by a specified column.• sort the records in ascending order by default.• By default the records are in ascending order or you can used ASC
keyword • If you want to sort the records in a descending order, you can use DESC
keyword.• The default order is ascending. NULL values are treated as larger that
non-null values for sorting purposes.
SQL ORDER BY Syntax
50
SELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESC
![Page 51: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/51.jpg)
DML: ORDER BY Keyword
The following statement will output staff average salary group by city.
51
staffNO staffNAME
city salary
gender departNO
ABC987 Nadz Kuala Lumpur
2300 Male 0001
ABC988 Aina Jerantut 2500 Female 0002
ABC989 Halimaton Jerantut 2200 Female 0001
ABC990 Norain Johor 2000 Female 0003
staff
SELECT city, MIN(salary) AS minSALARYFROM staffGROUP BY cityORDER BY city DESC;
city minSALARY
Jerantut 2200
Johor 2000
Kuala Lumpur
2300
![Page 52: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/52.jpg)
DML: HAVING Statement
HAVING statement is used to select or reject a group.
The following example shows the average salary for staff based on city where the total staff salary exceeds RM 2000.
52
staffNO staffNAME
city salary gender
departNO
ABC987 Nadz Kuala Lumpur
2300 Male 0001
ABC988 Aina Jerantut 2500 Female
0002
ABC989 Halimaton Jerantut 2200 Female
0001
ABC990 Norain Johor 2000 Female
0003
SELECT city, AVG(salary) AS avgSALARYFROM staffGROUP BY cityHAVING SUM(salary)> 2000 ;
city avgSALARY
Kuala Lumpur
2300
Jerantut 2350
![Page 53: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/53.jpg)
SQL Aggregate Functions
SQL aggregate functions return a single value, calculated from values in a column
COUNT( ) returns the number of rows
SUM( ) returns the sum
AVG( ) returns the average value
MIN( ) returns the smallest value
MAX( ) returns the largest value
FIRST( ) returns the first value
LAST( ) returns the last value
53
![Page 54: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/54.jpg)
SQL Aggregate Functions
Each operates on a single column of a table and returns a single value.
COUNT, MIN, and MAX apply to numeric and non-numeric fields
SUM and AVG may be used on numeric fields only.
Apart from COUNT(*), each function eliminates nulls first and operates only on remaining non-null values.
COUNT(*) counts all rows of a table, regardless of whether nulls or duplicate values occur.
Can use DISTINCT before column name to eliminate duplicates.
DISTINCT has no effect with MIN/MAX, but may have with SUM/AVG.
54
![Page 55: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/55.jpg)
MIN/MAX/AVG
55
penNO descr color qty
1001 Xtra-Fine Red 12
1002 Fine Blue 32
1003 Medium Pink 4
pen
SELECT MIN(qty)AS minQTY, MAX(qty) AS maxQTY, AVG(qty) AS avgQTYFROM pen;
minQTY maxQTY avgQTY
4 32 16
![Page 56: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/56.jpg)
SUM/COUNT
56
penNO descr color qty
1001 Xtra-Fine Red 12
1002 Fine Blue 32
1003 Medium Pink 4
pen
SELECT SUM(qty)AS totalQTYFROM pen;
totalQTY
48
SELECT COUNT(penNO)AS penNUMBERFROM pen;
penNUMBER
3
![Page 57: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/57.jpg)
FIRST/LAST
57
penNO descr color qty
1001 Xtra-Fine Red 12
1002 Fine Blue 32
1003 Medium Pink 4
pen
SELECT FIRST(color)AS firstCOLORFROM pen;
firstCOLOR
Red
SELECT LAST(color)AS lastCOLORFROM pen;
lastCOLOR
Pink
![Page 58: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/58.jpg)
Example
a. Find biscuit name that have the highest price.
b. List down all the biscuit order quantity for each biscuit type.
c. List down all the biscuit order quantity for each customer and sort according to the customer name.
d. List down how many customer order being made for each biscuit.
e. List down all customer name and email who order more then 5 biscuits.
f. List down biscuit information that never being order yet.58
tblcustomer(custNO, custNAME, custEMAIL)tblbiscuit(bisNO, bisNAME, bisFLAVOUR, bisPRICE)tblorder(orderNO,*custNO,*bisNO, orderadate, qty, status)
![Page 59: ITS232 Introduction To Database Management Systems](https://reader035.vdocument.in/reader035/viewer/2022062804/56814948550346895db694ff/html5/thumbnails/59.jpg)
Find biscuit name that have the highest price.
SELECT bisNAMEFROM tblbiscuitWHERE bisPRICE = (SELECT MAX(bisPRICE)
FROM tblbiscuit);
59