Weitere ähnliche Inhalte
Ähnlich wie Sql 2009 (20)
Kürzlich hochgeladen (20)
Sql 2009
- 1. SQL
2009
By
Cathie
Usher
1
SQL
2
©Chisholm Institute
- 2. Introduction to SQL
SQL stands for Structured Query Language
In simple terms it is a universal language that permits the
construction of tables and the manipulation of Data that lies within a
table. SQL is extensively used in many areas of i d
bl i i l di f industry and is fast
di f
becoming the backbone of Internet display and functionality.
In this section we concentrate on the construction and population of
tables. This area is referred as the Data Definition Language of SQL
(DDL). The following 2 sections concentrate on Data Manipulation
Language (DML) where we do the actual queries.
You will also be required to create tables yourself and perform some
fundamental exercises in the Tutorials
Tutorials.
3
Introduction to SQL
SQL consists of two sub languages:
DML (Data manipulation language)
DDL (Data definition language)
SQL is relatively easy to learn.
SQL is a non-procedural (declarative) language. Users need
only specify what data they want, not how this data is found.
ANSI prescribes a standard SQL.
Commercial Relational DBMS implement a
standard SQL core
core.
IBM initiated SQL in 1973. In 1983 the International
Standards Organization (ISO) began to develop a standard for
relational database languages based on SQL. The latest
standard is SQL2 or SQL-92, but SQL3 is now under
consideration and will include OO features.
4
©Chisholm Institute
- 3. Structured Query Language (SQL)
Data Definition Language (DDL)
D t D finiti n L n
involves
Data types
(Characters, Dates etc)
Creating tables and basic data management
(Create, identifying Primary and Foreign Keys etc)
Advanced Data Management
the variation and changes to tables that exist
5
Vocabulary
The following terminologies are used in this area of SQL
CREATE TABLE COMMIT
INSERT
DROP TABLE
DATA TYPE
6
©Chisholm Institute
- 4. DML, DDL and Transaction
Control
Data Manipulation Language
Query or manipulate the data in database tables
Commands: SELECT, INSERT, UPDATE, DELETE
Data Definition Language
Change the database structure
Commands: CREATE, ALTER, DROP, GRANT, REVOKE
Transaction Control
Organise commands into logical transactions and commit
them to the database or roll them back.
Commands: COMMIT, ROLLBACK, SAVEPOINT
7
Common Data Types
Before we can
create tables
Data Type Format Standard
we need to
know about
Numeric NUMBER(L,D)
NUMBER(L D) Oracle
data types. INTEGER ANSI
Here is a SMALLINT ANSI
summary of
the data types
DECIMAL(L,D) ANSI
and their
formats for Character CHAR(L) Oracle
both Oracle
and ANSI if
d
VARCHAR2(L) Oracle
different.
Date DATE Oracle
8
©Chisholm Institute
- 5. Values allowed by data
types
Here is a list of the values that are allowed and the method of
predefining them.
SMALLINT whole no -32,766 t 32 767
h l n 32 766 to 32,767
INTEGER whole no -2,147,483,647 to 2,147,483,647
DECIMAL(L,D) max of L digits, of which D digits follow the decimal
point
CHAR(L) L characters from 0 to 255, fixed storage length
VARCHAR2(L) L characters from 0 to 255, variable storage length
9
Data Definition
Commands
It is critical when creating the tables that we define the Primary
and Foreign Keys as displayed on the Database Schema and the
consequential Network diagram.
These commands relate in particular to the creation
of tables.
DDL implement Integrity Constraints
Entity Integrity
PRIMARY KEY is NOT NULL
Referential Integrity
FOREIGN KEY
10
©Chisholm Institute
- 6. Creating aTable (1)
The CREATE TABLE statement sets up a table
in which rows of data can be stored.
h h fd d
In its simplest form this is:
CREATE TABLE name
(column_name data_type
l d
column_constraint);
11
Creating a Table (1)
sql> CREATE TABLE DEPARTMENT
(DEPT_CODE CHAR(1) NOT NULL,
DNAME VARCHAR(25) NOT NULL,
MAIL_NO CHAR(2) NOT NULL,
PRIMARY KEY (DEPT_CODE));
12
©Chisholm Institute
- 7. Creating the Table (2)
sql> CREATE TABLE EMPLOYEE
(EMP_ID CHAR(3) NOT NULL,
ENAME VARCHAR(25) NOT NULL
NULL,
DEPT CHAR(1) NOT NULL,
MANAGER CHAR(3),
DATE_JOINED DATE,
DOB DATE,
PRIMARY KEY (EMP ID)
(EMP_ID),
FOREIGN KEY (MANAGER) REFERENCES EMPLOYEE,
FOREIGN KEY (DEPT) REFERENCE DEPARTMENT);
Note: This table cannot be created until the Department table
has been created, as it references the Department table. 13
SQL
A simple sql statement that retrieves data from a table has the
following key structure. Keywords are in capitals
SELECT data you want to retrieve - usually column names
FROM table-name
WHERE meeting this criteria – usually column-name matches a value(s)
SELECT empno, name, salary
FROM employee
WHERE salary > 50000
SELECT empno, name, salary FROM employee WHERE salary > 50000
14
©Chisholm Institute
- 8. Result Set
The result of a Select Statement is often referred to
as the Result Set. (That's we will call it anyway).
An SQL statement is said to 'return' some data via
Q m m
the result set.
The result set is often displayed on the screen but it
could be sent to a printer, saved to a file, passed to a
program or application etc.
People often mean 'the result set' when they say
"The SQL statement returns / displays / lists /
outputs some data"
15
SELECT item list FROM table-name
SELECT < item-list [ [AS] <alias>] >…
The item list is usually a list of column-names or formula
The sequence of columns in the result set is determined
by th
b the order of expressions i th it
d f i in the item-list
li t
SQL statement Output Col Sequence
SELECT empno, name, salary FROM employee In item-list sequence
SELECT empno, salary, name FROM employee In item-list sequence
SELECT * FROM employee As listed in table def
SELECT name, salary, salary * 1.1 FROM employee In item-list sequence
item list
SELECT name, (salary * 1.1) FROM employee "
SELECT name, (salary * 1.1) "New_Salary" FROM "
emplo
SELECT name AS "Employee Name", salary FROM "
emplo
16
©Chisholm Institute
- 9. Select Distinct
SELECT [ALL / DISTINCT] <item-list >… firstname surname dept
Ted Smith 1
ALL is the default. All possible records
are return in the result set. Ted Jones 2
DISTINCT eliminates duplicate
records in the result set. John Jones 1
Ted Jones 1
SELECT DISTINCT dept SELECT DISTINCT
FROM employee firstname, dept FROM …
firstname dept
dept
1
Ted 1
2
Ted 2
John 1
17
Order By
The Order By clause specifies the sequence of records in the result-
set.
• The order is specified by the column number or the
column name of the result-set.
• Multiple columns can be specified.
• Each column may be ordered in ASCending or DESCending
sequence (descending is the default)
If no order by clause is specified, the result set may be ordered by
specified
the physical sequence of records in the table. However, this would
produce different results if the physical order is ever changed.
18
©Chisholm Institute
- 10. Order By
SELECT … FROM …
[ ORDER BY <column-name / column-no> [ASC / DESC] [,…] ]
SELECT empno, name, salary FROM EMPLOYEE ORDER BY name
result set records are in ascending name sequence
s lt s t ds i s di m s
SELECT empno, name, salary FROM EMPLOYEE ORDER BY name ASC
result set records are in ascending name sequence
SELECT empno, name, salary FROM EMPLOYEE ORDER BY name DESC
result set records are in descending name sequence
SELECT empno, name, salary FROM EMPLOYEE ORDER BY salary DESC,
empno ASC
result set records are in descending salary sequence. Within each
sequence
salary, records are listed in ascending employee number sequence
SELECT empno, name, salary FROM EMPLOYEE ORDER BY 3, 1
result set records are in ascending salary sequence. Within each salary,
records are listed in ascending employee number sequence
19
WHERE clause
SELECT … FROM …
[ WHERE <search-condition> ]
[ ORDER BY …
The WHERE clause specifies the criteria that a record must meet to be
included in the result set.
…WHERE surname = "Smith"…
…WHERE salary > 50000…
…WHERE salary <> 50000…
20
©Chisholm Institute
- 11. Data Types
When specifying a value stored in a character field you must use
quotes around the value you search for.
e.g.
e g Select * from employee where empname = "Smith"
Smith
Some DBMSs require the use of " double quotes while others use
' single quotes. Some DBMSs will accept either.
Most DBMSs are case-sensitive. Ie. 'SMITH' is not equal to
'Smith'
There is usually a SQL setting to turn case-sensitivity on/off.
21
Data Types
When specifying a value stored in a numeric field you
should not use quotes around the value you search for. Do
no use commas between thousands. Decimals are OK.
e.g. select * from employee where salary > 29999.99
22
©Chisholm Institute
- 12. Summary
SELECT - FROM - WHERE Syntax
SELECT
* shows all rows
distinct d
d distinct shows only unique rows
h l
select list specifies what columns to display, usually a set of
column names
FROM table specification
can be more than one table name here
[WHERE search condition]
specifies which rows to display
[GROUP BY column name]
G P B l
specifies columns used to define groups
[HAVING search condition]
specifies which groups to exclude from display
[ORDER BY ordering specification]
specifies how to order results 23
Activity
Complete Tut 1.
24
©Chisholm Institute
- 13. Multiple criteria in the Where Clause
Use AND / OR when your query needs to meet multiple criteria.
Some simple examples:
SELECT * FROM employee
WHERE surname = 'SMITH' OR surname = 'JONES'
SELECT * FROM employee WHERE salary > 20000 AND salary < 30000
Confused about whether to use AND or OR or ranges?
Consider a single row. Can the surname equal Smith AND Jones. No.
Draw a simple graph when dealing
with ranges
ranges.
0 10000 20000 30000 40000
> 20000
< 30000
AND includes range common to both lines
OR includes range covered by either line
25
And Operator
The AND operator is used in the where clause.
It evaluates and merges two conditions
It returns results only when both conditons are TRUE.
AND Condition 1 True Condition 1 False
Condition 2 True TRUE FALSE
Condition 2 False FALSE FALSE
Consider the clause: Age Sex Result
…WHERE AGE >20 AND SEX = ‘M
WHERE M… 18 M F
FALSE
E
19 F FALSE
21 F FALSE
22 M TRUE
26
©Chisholm Institute
- 14. OR Operator
The OR operator is used in the where clause.
It evaluates and merges two conditions
It returns results only when either conditions are TRUE.
t e ther cond t ons E.
OR Condition 1 True Condition 1 False
Condition 2 True TRUE TRUE
Condition 2 False TRUE FALSE
Age Sex Result
18 M TRUE
19 F FALSE
Consider the clause: 21 F TRUE
…WHERE AGE >20 OR SEX = ‘M… 22 M TRUE
27
Multiple operators
Where multiple operators exist, evaluate one operator at a time then
the next, then the next…
Consider …
WHERE AGE >19 AND SEX = 'F' AND HEIGHT = '166' and WEIGHT
> 50
Age Sex Height Weight
Evaluate AGE > 19 AND SEX = 'F'
24 F 171 86
Evaluate TRUE AND HEIGHT > 166
Evaluate TRUE AND WEIGHT > 50
Age Sex Height Weight
Evaluate AGE > 19 AND SEX = 'F'
24 M 183 93
28
©Chisholm Institute
- 15. ANDs and ORs
When both AND and OR operators are used in the where clause,
the AND operators take precedence and are evaluated first.
Consider …
WHERE AGE <17 OR AGE > 23 AND SEX = 'F'
17
Age Sex Height
24 M 180
Evaluate AGE > 23 AND SEX = 'F' first
WHERE SEX = 'M' OR AGE > 22 AND SEX = 'F' OR
AGE <17 Age Sex Height
21 M 180
29
Parentheses
Operators within parentheses are always evaluated first.
Parentheses can also be used just for readability.
Consider …
WHERE AGE < 13 OR AGE > 19 AND SEX = 'M' M
Evaluate AGE > 19 AND SEX = 'M' first, then
Evaluate AGE < 13 OR …
WHERE ( AGE < 13 OR AGE > 19 ) AND SEX = 'M'
Evaluate ( AGE < 13 OR AGE > 19 ) first, then
Evaluate … AND SEX = 'M'
WHERE (SEX = 'M' OR SEX = 'F') AND ( AGE >18 AND AGE <= 20 )
Eval either (SEX = 'M' OR SEX = 'F') or ( AGE >18 AND AGE <= 20 )
first
The age checking doesn't need parentheses , but it makes reading
easier.
30
©Chisholm Institute
- 16. NOT
The NOT operator negates the experssion to its right.
If the <expression> is TRUE, then NOT <expression> is FALSE
If the <expression> is FALSE, then NOT <expression> is TRUE
Consider … AGE RESULT
WHERE AGE >= 20 AND AGE <= 29 24 TRUE
17 FALSE
WHERE NOT ( AGE >= 20 AND AGE <= 29 ) AGE RESULT
24 FALSE
17 TRUE
AGE RESULT
24
WHERE NOT AGE >= 20 AND AGE <= 29
17
31
Special Operators
WHERE <column-name> BETWEEN <value/exp> AND <value/exp>
WHERE age BETWEEN 30 and 40
WHERE < column-name > LIKE <string with % wildcards>
WHERE name LIKE 'SMI%'
WHERE name LIKE '%SMITH%'
WHERE < column-name > IN <expression1 [,…]>
WHERE name IN ('JONES', 'BROWN', 'LEE', 'SOO')
WHERE age IN (24, 26, 28, 30)
same as WHERE age = 24 OR age = 26 OR age = 28 OR age = 30
g g g g
WHERE < column-name > IS NULL / IS NOT NULL
WHERE height IS NULL
WHERE name IS NOT NULL
32
©Chisholm Institute
- 17. Constraints
Key Constraint or Primary Key Constraint
The key may have any value but cannot be identical to an existing key
The key (or part key if the key is a composite key) cannot be null
Referential Integrity Constraint or Foreign Key Constraint
A foreign key must match a primary key value in another relation or the
foreign key must be null
33
Obtain tutorial 2 from your tutor.
34
©Chisholm Institute
- 18. tablename.columnname and aliases
Each column–name used in any SQL statement may be prefixed by the
table name.
SELECT empno, name, salary FROM employee
WHERE sex = 'F' AND age => 50
SELECT employee.empno, employee.name, employee.salary FROM
employee
WHERE employee.sex = 'F' AND employee.age => 50
A table may be given an alias which can be used throughout the rest
of th SQL statement. The alias name is given immediately after
f the t t t Th li i i i di t l ft
the table name.
SELECT e.empno, e.name, e.salary FROM employee e
WHERE e.name LIKE 'DAV%' AND e.salary => 50000
35
FROM with multiple tables
SELECT … FROM <table1> [table1-alias], <table2> [table2-alias] [,…]
SQL SELECT statements can work with data from more than one
table.
t bl
The FROM clause specifies which tables will be used.
As column-names may be identical in two different tables,
column-names are usually prefixed by the table name
SELECT e.empno, e.name, e.salary b.branchaddr
FROM employee e, branch b
36
©Chisholm Institute
- 19. SQL JOIN
SELECT … FROM <table1> [table1-alias], <table2> [table2-alias] [,…]
[ WHERE … <foreign column-name> = <primary-key column-name>
]
or <primary-key column-name> = <foreign column-name>
Within the Where clause, you specify foreign key column must
= the primary key column
SELECT e.empno, e.name, e.salary, b.branchaddr
FROM employee e, branch b
WHERE e branch=b branch
e.branch=b.branch
This statement will display all employees and corresponding branch
addresses
37
Effects of a missing Join
If the join is missing, then the DBMS will merge data from each row in table
1 with every row in table two. Large tables create very large result sets
EMPLOYEE BRANCH
Branch BranchAddr
EmpNo Name Branch Salary
Box Hill 1 Station St
207 John Hawthorn 48000
Smith Hawthorn 1 John St
119 Jane Pitt Box Hill 37500
345 Carol Kent Hawthorn 55000
RESULT SET
EmpNo Name Branch Salary BranchCode BranchAddr
207 John Smith Hawthorn 48000 Box Hill 1 Station St
207 John Smith Hawthorn 48000 Hawthorn 1 John St
119 Jane Pitt Box Hill 37500 Box Hill 1 Station St
119 Jane Pitt Box Hill 37500 Hawthorn 1 John St
345 Carol Kent Hawthorn 55000 Box Hill 1 Station St
345 Carol Kent Hawthorn 55000 Hawthorn 1 John St
38
©Chisholm Institute
- 20. Multiple Foreign Keys
A relation/
table can EMPLOYEE
have multiple
foreign keys EmpNo Name Branch Salary Dept
207 John
J h Smith Kew
K 48000 1
119 Jane Pitt Box Hill 37500 2
345 Carol Kent Hawthorn 55000 2
BRANCH DEPARTMENT
Branch BranchAddr BranchPh DeptCode DeptDescr
Box Hill 1 Station St 9999-5678 1 Sales
Hawthorn 1 John St 9999-2222 2 Admin
Kew 1 High St 9999-1234 3 Reception
39
SQL JOIN (a table with 2 foreign keys)
SELECT d.deptdescr, e.branch, e.empno, e.name, b.branchph, e.salary,
FROM employee e, branch b, department d
WHERE e.branch = b.branch
AND e.dept = d.deptcode Jo s employee branch
Joins e p oyee to b a c
AND e.salary > 380000
Joins employee to department
RESULT SET
deptdescr branch EmpNo Name BranchPh Salary
Sales Kew 207 John Smith 9999-1234 48000
Admin
dm n Hawthorn 3 5
345 Carol Kent
arol 9999
9999-2222 55
55000
40
©Chisholm Institute
- 21. Multiple Foreign Keys (2)
EMPLOYEE
Each table
relation can
EmpNo Name Branch Salary
have foreign
key links to 207 John Smith Kew 48000
other tables. 119 Jane Pitt Box Hill 37500
667 John Dill Penrith 75000
BRANCH
Branch BranchAddr Region REGION
Kew 1 High St ME RegionCode RegionDescr
Hawthorn 1 John St ME ME Melbourne East
Penrith 1 Puckle St SW SW Sydney West
41
SQL JOIN (3 tables each with a FK )
SELECT e.name, reg.regiondescr
FROM employee e, branch b, region reg
WHERE e.branch = b.branch
AND b region = reg.regioncode Joins e p oyee to b a c
b.region reg regioncode Jo s employee branch
AND e.empno > 200 Joins branch to region
RESULT SET
Name RegionDescr
John Smith Melbourne East
John Dill Sydney West
Notice how this query does not display any columns from
the branch table
42
©Chisholm Institute
- 22. Drop Table
DROP TABLE <tablename>
You cannot create a table that already exists.
You will need to DROP a table first, if you want to re-create it.
DROP TABLE student;
CREATE TABLE student…
43
DELETE records
DELETE <table-name>
WHERE …
DELETE student will delete all rows from the student table
DELETE student WHERE stu_height < 150
will delete all rows fro the student table that meet the criteria.
44
©Chisholm Institute
- 23. COUNT
COUNT counts the number of records in a table (that meet a criteria).
PART ( partnumb, partdesc, unonhand, itemclss, wrhsnumb, unitprce )
SELECT COUNT(*) FROM part
SELECT COUNT(partnumb) FROM part
SELECT COUNT(*) FROM part WHERE itemclass = 'HW'
SELECT COUNT(partnumb) FROM part WHERE itemclass = 'HW'
45
SUM , AVERAGE , MAX , MIN
SUM totals a column (that meet a criteria).
ORDLNE (ordnumb, partnumb, numbord, quotprce )
SELECT SUM( numbord ) FROM ordlne
SELECT AVG( numbord ) FROM ordlne
SELECT MIN( numbord ) FROM ordlne
SELECT MAX( numbord ) FROM ordlne
SELECT SUM( numbord * quotprce ) FROM ordlne
SELECT SUM( numbord * quotprce ) FROM ordlne
WHERE partnumb = 'BT04'
46
©Chisholm Institute
- 24. GROUP BY
The following statement requires a part number in the criteria.
SELECT SUM( numbord * quotprce ) FROM ordlne
WHERE partnumb = 'BT04'
Imagine if you had 1000 products. You would have to type 1000 product codes.
The GROUP BY clause, will display an aggregate total for each 'group'
The following records are 'grouped' by order number.
SELECT ordnumb, SUM( numbord * quotprce ) ordnumb Sum
FROM ordlne 12489 164.45
GROUP BY ordnumb 12491 714.94
12494 700.00
12495 115.90
12498 65.70
12500 402.99
12504 217.98
47
GROUP BY rules
An aggregate expression contain aggregate words such as count, sum…
The select statement may contain any mixture of aggregate and non-aggregate
expressions.
Every non aggregate expression in the select clause must appear in the
Group By Clause
p y
Aggregate expressions in the Select clause do not appear in the Group By clause
A subtotal will be calculated for each change in the non-aggregate column values
SELECT wrhsnumb,
itemclss, count(*) wrhsnumb itemclss Part Table
FROM part 3 HW
2 SG Result Set
GROUP BY 1 SG
wrhsnumb,
wrhsnumb itemclss 3 HW wrhsnumb itemclss count
2 AP 1 SG 2
3 AP 2 AP 1
3 HW
1 SG 2 SG 2
3 HW 3 AP 1
2 SG 3 HW 4
48
©Chisholm Institute
- 25. HAVING
Having 'works on' the results of the Group By. wrhsnumb itemclss count
The previous Group By showed: 1 SG 2
2 AP 1
Suppose we only wanted to deta ls of product
details 2 SG 2
classes that had more than one item. 3 AP 1
3 HW 4
That means we want to specify a criteria based on the result of the Group By.
The HAVING clause is used to do this. Do not use the WHERE clause.
The HAVING clause must use a aggregate expression from the SELECT clause.
SELECT wrhsnumb, itemclss, count(*)
FROM part
t
GROUP BY wrhsnumb, itemclss
HAVING count(*) >= 2
49
Review
SELECT Column names / expressions / aggregate expressions
Column names should be preceded by table names
especially when multiple tables are used
FROM Table names (aliases may be used for Table Names)
WHERE Specifies criteria that rows must match to be selected
selected.
This is where 'joins' are specified in Oracle 8.
The join is usually foreign-key column = primary key column.
GROUP BY Calculate subtotals. All non-aggregate SELECT expressions
must be included here
HAVING Used in conjuction with Group By. Specifies criteria that
Group By aggregate expressions must match to be selected.
The criteria is based on the results of the Group By.
Only aggregate expressions from the SELECT clause may
appear here.
ORDER BY Specifies the sequence of rows in the RESULT SET
50
©Chisholm Institute
- 26. Sub Queries
Some queries cannot be completed in a simple single SQL statement.
Determine the average unit price of all parts.
OK SELECT AVG(unitprce) from part
ERROR SELECT * from price WHERE unitprce > AVG( unitprce )
Aggregate commands are not allowed in the WHERE clause.
However subqueries are allowed.
A subquery is a complete SQL statement WITHIN another SQL statement.
51
Sub Query Examples
SELECT *
FROM part
WHERE unitprce > ( SELECT AVG(unitprce) FROM part )
SELECT partnumb , partdesc
FROM part
WHERE partnumb NOT IN ( SELECT DISTINCT partnumb FROM ordlne )
52
©Chisholm Institute
- 27. Dates & related functions
Using today's date:
SELECT sysdate FROM dual 25-MAY-02
SELECT *
FROM orders, dual
orders
WHERE orddte < sysdate ---- No Join necessary
Convert Character String to Date
to_date('25-dec-02')
SELECT TO_DATE('25-dec-02') - sysdate FROM dual ----- 151.65480
SELECT ROUND (TO_DATE('25-dec-02') – sysdate) FROM dual ------ 152
53
References
A List of Oracle Functions
This site gives a list of the functions that are use the most in
Oracle.
SQL*Plus User's Guide and Reference, Release 8.1.5
Guide that explains all the concepts behind PL/SQL and illustrates
every facet of the language.
Oracle8i SQL Reference
Reference that contains a complete description of the (SQL)
Oracle Underground FAQ
A site giving a wide variety of topics on Oracle.
Oracle FAQ F
O l FAQs Forum
Post your questions on anything about Oracle Developer. You can
also learn a lot from others' experience and answers.
54
©Chisholm Institute
- 28. Self Joins
Suppose we have an Employee Table.
The Manager column contains the employee number of each
employee's manager.
The Manager column may contain nulls EMPLOYEE
as some managers will not have a EmpNo Name Salary Manager
manager of their own.
119 Jane Pitt 37500 543
To extract employee & manager details,
we must employee as 2 different tables.207 John 48000 543
Smith
SELECT emp.name Employee_Name, 345 Carol Kent 55000 543
mgr.name Manager_Name 543 Dave Bligh 70000 ┴
FROM employee AS emp,
employee AS mgr
l Column Heading
WHERE emp.manager = mgr.empno
Same table – 2 aliases
55
Inner Joins
The join syntax we have used so far in often referred to as a INNER JOIN.
The result set only includes rows that have matching data from both tables.
"Show me a list of all parts and show me quantities sold"
SELECT o.ordnumb, o.numbord, p.partnumb, p.partdesc
FROM ordlne o, part p
o
WHERE o.partnumb = p.partnumb
ordnumb partnumb numbord partdesc
12489 AX12 11 IRON
12491 BT04 1 STOVE
12491 BZ66 1 WASHER
12494 CB03 4 BIKE
12495 CX11 2 MIXER
12498 AZ52 2 SKATES
12498 BA74 4 BASEBALL
12500 BT04 1 STOVE
12504 CZ81 2 WEIGHTS
Parts BH22 & CA14 are not in the list – as they have not been ordered
56
©Chisholm Institute