sql - select

25
1 SQL - Select • Join / Outer Join • Sub queries

Upload: tadita

Post on 17-Jan-2016

42 views

Category:

Documents


2 download

DESCRIPTION

SQL - Select. Join / Outer Join Sub queries. 1. Join. Join Outer join Left outer join Right outer join. 1.1 Join. List customer ACME’s orders record in January Select * from customers where cname = ‘ACME’ Select * from orders where cid = ‘c001’ and month = ‘jan’ - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SQL - Select

1

SQL - Select

• Join / Outer Join

• Sub queries

Page 2: SQL - Select

2

1. Join

• Join

• Outer join

• Left outer join

• Right outer join

Page 3: SQL - Select

3

1.1 Join

• List customer ACME’s orders record in January

• Select * from customers where cname = ‘ACME’• Select * from orders where cid = ‘c001’ and month = ‘jan’

• select o.* from customers c, orders o where c.cid = o.cid and cname = 'ACME' and month = 'jan'

Page 4: SQL - Select

4

1.1 Join (cont)

• List the names of all customers who have bought comb

• Select pid from products where pname = ‘comb’• Select cid from orders where pid = ‘p01’• Select cname from customers where cid = ….

• Select cname from customers c, orders o, products p where p.pid = o.pid and c.cid = o.cid and p.pname = ‘comb’

Page 5: SQL - Select

5

1.1 Join (cont)

• List the names of all customers who have bought comb

• Select distinct cname from customers c, orders o, products p where p.pid = o.pid and c.cid = o.cid and p.pname = 'comb'

Page 6: SQL - Select

6

1.1 Join (cont)

• List all customer pairs who live in the same city

• Select c1.cid, c2.cid from customers c1, customers c2 where c1.city = c2.city and c1.cid != c2.cid

• Select c1.cid, c2.cid from customers c1, customers c2 where c1.city = c2.city and c1.cid < c2.cid

Page 7: SQL - Select

7

1.1 Join (cont)

• List all the product names that have been ordered by at least two customers.

• select distinct p.pname, o1.pid from orders o1, orders o2, products p where o1.pid = p.pid and o1.pid = o2.pid and o1.cid < o2.cid

Page 8: SQL - Select

8

1.1 Join (cont)

• List the agent names and their total sale amount (orders.dollars).

• select aname, sum(dollars) total_sales from agents a, orders o where a.aid = o.aid group by a.aid, a.aname

Page 9: SQL - Select

9

1.2 Outer join

A B1 B2

a1 b1 b1

R lxl S

C

c1

a1 b1 b1 c2

a2 b1 b2 c3

A B1 B2

a1 b1 b1

a1 b2 b1

a2 b1 b2

B1 B2 C

b1 b1 c1

b1 b1 c2

b1 b2 c3

b2 b2 c4

R

S

A B1 B2

a1 b1 b1

R lxlo S

C

c1

a1 b1 b1 c2

a2 b1 b2 c3

a1 b2 b1 null

null b2 b2 c4

Page 10: SQL - Select

10

1.2 Outer Join (cont)• Modification to CAPS database.• 1. Add two more records• insert agents values ('a07', 'Tony', 'Boston', 8);• insert orders values (1030, 'mar', 'c006', 'a08', 'p01', 800, 400.00);

• 2. Create a new table “Sales”• create table Sales• (aid char(3) not null,• total money• );

• 3. Load data into table “Sales”• Insert into Sales• select aid, sum(dollars) from orders group by aid

Page 11: SQL - Select

11

1.2 Outer Join (cont)

Join (inner join)

• select aname, aid, total from sales s join agents a on s.aid = a.aid

• Select aname, a.aid, total from sales s join agents a on s.aid = a.aid

Page 12: SQL - Select

12

1.2 Outer Join (cont)

Outer Join

• select aname, a.aid, total from sales s full outer join agents a on s.aid = a.aid

Page 13: SQL - Select

13

1.3 Left outer join

A B1 B2

a1 b1 b1

a1 b2 b1

a2 b1 b2

B1 B2 C

b1 b1 c1

b1 b1 c2

b1 b2 c3

b2 b2 c4

R

S

A B1 B2

a1 b1 b1

R left outer join S

C

c1

a1 b1 b1 c2

a2 b1 b2 c3

a1 b2 b1 null

Page 14: SQL - Select

14

1.3 Left outer Join (cont)

Left outer Join:

List name, aid and total sale record of agents who have sale records in the sales table.

• select aname, a.aid, total from sales s left outer join agents a on s.aid = a.aid

Page 15: SQL - Select

15

1.4 Right outer join

A B1 B2

a1 b1 b1

a1 b2 b1

a2 b1 b2

B1 B2 C

b1 b1 c1

b1 b1 c2

b1 b2 c3

b2 b2 c4

R

S

A B1 B2

a1 b1 b1

R right outer join S

C

c1

a1 b1 b1 c2

a2 b1 b2 c3

null b2 b2 c4

Page 16: SQL - Select

16

1.4 Right outer Join (cont)

Right outer Join:

List name, aid and total sale record of agents who have records in the agents table.

• select aname, a.aid, total from sales s left outer join agents a on s.aid = a.aid

Page 17: SQL - Select

17

2.1 Sub queries

• List cid values of customers who place orders with agent in Dallas

• Select aid from agents where city = 'Dallas‘

• Select distinct cid from orders where aid in (select aid from agents where city = 'Dallas')

Page 18: SQL - Select

18

2.1 Sub queries (cont)

• List the names and discounts of all customers who place order through agents in Dallas

• Select aid from agents where city = 'Dallas‘

• Select distinct cid from orders where aid in (select aid from agents where city = 'Dallas')

• Select cname, discount from customers where cid in (select distinct cid from orders where aid in (select aid from agents where city = 'Dallas'))

Page 19: SQL - Select

19

2.1 Sub queries (cont)• List the names of customers who order product

p05.

• Select distinct cname from customers where cid in (select distinct cid from orders where pid = 'p05')

• Select distinct cname from customers where 'p05' in (select pid from orders where cid = customers.cid)

Uncorrelated subquery

Correlated subquery

Page 20: SQL - Select

20

2.1 Sub queries (cont)• List the names of customers who do not

order product p05.

• Select distinct cname from customers where cid in (select distinct cid from orders where pid != 'p05')

• Select distinct cname from customers where cid not in (select distinct cid from orders where pid = 'p05')

Page 21: SQL - Select

21

2.2 Sub queries – other key words

• Some\ any \ all

• Exists

Page 22: SQL - Select

22

2.2 Sub queries – other key words (cont)

• Find agents (aid) with a minimum commission.

• Select aid from agents where percentage <= all (select percentage from agents)

• Select aid from agents where percentage = (select min(percentage) from agents)

Page 23: SQL - Select

23

2.2 Sub queries – other key words (cont)

• Find agents (aid) with a minimum commission.

• Select aid from agents where percentage <= any (select percentage from agents)

• Select aid from agents where percentage <= some (select percentage from agents)

Page 24: SQL - Select

24

2.2 Sub queries – other key words (cont)

• Find all customers (cid and cname) who have the same discount as that of any of the customers in Dallas

• Select cid, cname from customers where discnt = some (select discnt from customers where city = 'Dallas')

• Select cid, cname from customers where discnt = any (select discnt from customers where city = 'Dallas')

Page 25: SQL - Select

25

2.2 Sub queries – other key words (cont)• Find all customer names where the customers places an order

through agent a05

• Select distinct cname from customers where cid in (select cid from orders where aid = 'a05')

• Select distinct c.cname from customers c, orders o where c.cid = o.cid and o.aid = 'a05'

• Select distinct c.cname from customers c where exists (select * from orders o where c.cid = o.cid and o.aid = 'a05')

• Select distinct c.cname from customers c join orders o on c.cid = o.cid and o.aid = 'a05'