Вы находитесь на странице: 1из 16

Data Manipulation in SQL I

Example
tables

Slumlords

ID
1
2
3

Name
John Sleeze
Jenny Doohickey
Nicki Minaj

Slums

ID
1
2
3
4

Address
1355 Pearl Street
1500 East Colfax
2323 Federal
2982 Kais Rd.

SlumlordID
1
1
3

Joins
Three types:
Inner
Both sides have to match
Otherwise, no rows returned

Left Outer
Show everything thats on left, and
leave right side null if nothing there

Full Outer
Show everything on the left AND on
the right, fill in with nulls if nothing
is there.

Structure of Joins
INNER JOIN:

LEFT OUTER JOIN:

SELECT fields
FROM table1
INNER JOIN table2
ON table1.id = table2.id

SELECT fields
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id

FULL OUTER JOIN:


SELECT fields
FROM table1
FULL OUTER JOIN table2
ON table1.id = table2.id

LEFT
RIGHT

SELECT fields FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id

Inner Join
Slumlords

ID
1
2
3

Name
John Sleeze
Jenny Doohickey
Nicki Minaj

Slums

ID
1
2
3
4

Address
1355 Pearl Street
1500 East Colfax
2323 Federal
2982 Kais Rd.

SlumlordID
1
1
3

SELECT slumlords.id, slumlords.name, slums.address


FROM slumlords
INNER JOIN slums
ON slumlords.id = slums.slumlordid

Results

ID
1
1
3

Name
John Sleeze
John Sleeze
Nicki Minaj

Address
1355 Pearl Street

1500 East Colfax


2982 Kais Rd.

Left Outer Join


Slumlords

ID
1
2
3

Name
John Sleeze
Jenny Doohickey
Nicki Minaj

Slums

ID
1
2
3
4

Address
1355 Pearl Street
1500 East Colfax
2323 Federal
2982 Kais Rd.

SELECT slumlords.id, slumlords.name, slums.address


FROM slumlords
LEFT OUTER JOIN slums
Results
ON slumlords.id = slums.slumlordid

ID
1
1
2
3

Name
John Sleeze
John Sleeze
Jenny Doohickey
Nicki Minaj

SlumlordID
1
1
3

Address
1355 Pearl Street

1500 East Colfax


<NULL>
2982 Kais Rd.

Full Outer Join


Slumlords

ID
1
2
3

Name
John Sleeze
Jenny Doohickey
Nicki Minaj

Slums

ID
1
2
3
4

Address
1355 Pearl Street
1500 East Colfax
2323 Federal
2982 Kais Rd.

SELECT slumlords.id, slumlords.name, slums.address


FROM slumlords
FULL OUTER JOIN slums
Results
ON slumlords.id = slums.slumlordid

ID Name
1 John Sleeze
1 John Sleeze
2 Jenny Doohickey
3 Nicki Minaj
<> <NULL>

SlumlordID
1
1
3

Address
1355 Pearl Street

1500 East Colfax


<NULL>
2982 Kais Rd.
2323 Federal

Open Excel file

Thank You SQL Server


SQL Server carefully maintains referential
integrity, so the three types of joins will
often yield the same results.
Inner joins will generally be all you need in
a carefully maintained SQL server DB.
Left Outer and Full Outer will be needed
when you try to massage data for data
mining.

Connecting to the database server


Write busmorpheus\teamroom in the Server
name area.
Select Windows Authentication
Insert your identikey login name
Insert your password
Click on Connect

Northwind example

Question: Are customers shipping to their billing address? If not, these may
be gifts. Join the customers and orders tables.

select orders.customerid, customers.address, orders.shipaddress


from orders
inner join customers
on orders.customerid = customers.customerid

This should yield 830 rowstry it.


Now, try to focus in on only those that are different
select orders.customerid, customers.companyname, orders.shipname
from orders
inner join customers
on orders.customerid = customers.customerid
WHERE orders.shipaddress <> customers.address

Try this1
Nowhow do you simplify the previous
query so that you do not get duplicate
rows?

Try this 2
Who ordered a specific type of product?
Create a query where you can easily
change the type of product youre looking
for:
Chai
Tofu
Pavlova

Try This 2: Solution


SELECT *
FROM orders
INNER JOIN [order details]
ON orders.orderid = [order details].orderid
INNER JOIN products
ON [order details].productid = products.productid
WHERE products.productname = 'chai'
NOW: Change this query so that you can see the Customer Names of those who bought Pavlova. What about those who bought either Chai or Pavlova?

Homework (individual HW)


Turn in a SQL statement and its results based on the
following requirements:
In Northwind, find all orders placed after May 1st 1998 with
one or more units that cost less than or equal to $10. Show
only all of those items that fit the above criteria, and make
sure the report contains only customerid, companyname,
orderid, orderdate, quantity, and unitprice. (This is an
individual homework).
Show screenshot of query and resulting rows. Due next class.

Answer Page
1:
select distinct orders.customerid, customers.companyname,
orders.shipname
from orders
inner join customers
on orders.customerid = customers.customerid
WHERE orders.shipname <> customers.companyname

2:
SELECT orders.customerid, customers.companyname
FROM orders
inner join [order details]
ON orders.orderid = [order details].orderid
inner join products
ON [order details].productid = products.productid
inner join customers
ON customers.customerid = orders.customerid
WHERE products.productname = 'chai'

Вам также может понравиться