Upload
javen
View
33
Download
2
Embed Size (px)
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 - PowerPoint PPT Presentation
Citation preview
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
Lab 2: Retriving Data From Multiple Tables
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 }
2
SELECT column_name(s)FROM table_name;
SELECT *FROM table_name;
DML: Queries: 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
3
SELECT column_name(s)FROM table_name AS alias_name
SELECT column_name AS alias_nameFROM table_name
DML: Queries: SELECT Statement
4
The following statement list all the staff name.
The following statement list all the department name.
staffNO name 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 name
0001 IT
0002 Network
0003 Management
staff department
SELECT nameFROM staff;
SELECT nameFROM department;
How do you list staff name and
which deparment they work with?
DML: Queries: SELECT Statement
• In order to list staff name with the corresponding department name they work with, the data must be retrieve from 2 table which is, table staff and department.
• In order to do so, both the table must be join, using either:– join predicate – JOIN keyword
5
DML: Queries: SELECT Statement with join predicate
The following statement list all the name with the coresponding deparment name they work with.
6
staffNO name 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 name
0001 IT
0002 Network
0003 Management
staff department
SELECT s.name, d.nameFROM staff AS s, department AS dWHERE d.departNO = s.departNO;
name name
Nadz IT
Aina Network
Halimaton IT
Norain Managementjoin predicate
DML: Queries: SELECT Statement with JOIN keyword
The following statement list all the name with the coresponding deparment name they work with.
7
staffNO name 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 name
0001 IT
0002 Network
0003 Management
staff department
SELECT s.name, d.nameFROM staff AS sJOIN department AS dON d.departNO = s.departNO;
name name
Nadz IT
Aina Network
Halimaton IT
Norain Management
JOIN keyword
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.
8
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
9
SQL INNER JOIN
SQL INNER JOIN Syntax
NOTE
INNER JOIN is the same as JOIN.
10
SELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name = table_name2.column_name
DML: Two-Table Joins
Besides INNER JOIN, you can also use the following statements to combines two tables with join conditions.
11
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;
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.
12
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“;
DML: Multiple-Table Joins
List down all customer name, biscuit name and order quantity that customer had order with confirm status.
13
tblcustomer(custNO, custNAME, custEMAIL)tblbiscuit(bisNO, bisNAME, bisFLAVOUR, bisPRICE)tblorder(orderNO,*custNO,*bisNO, orderadate, qty, status)