Professional Documents
Culture Documents
CHAPTER 7
An Introduction To SQL
Lab 2: Retriving Data From Multiple Tables
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
SELECT column_name(s) SELECT *
FROM table_name; FROM table_name;
Note
SQL is not case sensitive { SELECT is the same as select }
2
DML: Queries: SQL Alias
SELECT column_name(s)
FROM table_name AS alias_name
staff department
staffNO name city salary departNO departNO name
ABC987 Nadz Kuala 2300 0001 0001 IT
Lumpur 0002 Network
ABC988 Aina Jerantut 2500 0002 0003 Management
ABC989 Halimaton Jerantut 2200 0001
ABC990 Norain Johor 2000 0003
How do you list
staff name and
The following statement list all the staff name. which deparment
they work with?
SELECT name
FROM staff;
5
DML: Queries: SELECT Statement
with join predicate
staff department
staffNO name city salary departNO departNO name
ABC987 Nadz Kuala 2300 0001 0001 IT
Lumpur 0002 Network
ABC988 Aina Jerantut 2500 0002 0003 Management
ABC989 Halimaton Jerantut 2200 0001
ABC990 Norain Johor 2000 0003
JOIN keyword 7
SQL JOINs
8
SQL JOINs
9
SQL INNER JOIN
NOTE
INNER JOIN is the same as JOIN.
10
DML: Two-Table Joins
Besides INNER JOIN, you can also use the following statements to combines
two tables with join conditions.
SELECT s.staffNAME
FROM staff AS s, department AS d
WHERE (d.departNO=s.departNO) AND d.departNAME=“IT”;
SELECT staff.staffNAME,department.departNAME
FROM staff
INNER JOIN department
ON department.departNO=staff.departNO;
11
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.
SELECT custNAME
FROM 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“;
12
DML: Multiple-Table Joins
List down all customer name, biscuit name and order quantity that
customer had order with confirm status.
13