1 sql - select join / outer join sub queries. 2 1. join join outer join left outer join right outer...

Download 1 SQL - Select Join / Outer Join Sub queries. 2 1. Join Join Outer join Left outer join Right outer join

Post on 17-Dec-2015

216 views

Category:

Documents

1 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • 1 SQL - Select Join / Outer Join Sub queries
  • Slide 2
  • 2 1. Join Join Outer join Left outer join Right outer join
  • Slide 3
  • 3 1.1 Join List customer ACMEs 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'
  • Slide 4
  • 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
  • Slide 5
  • 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'
  • Slide 6
  • 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
  • Slide 7
  • 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
  • Slide 8
  • 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
  • Slide 9
  • 9 1.2 Outer join AB1B2 a1b1 R lxl S C c1 a1b1 c2 a2b1b2c3 AB1B2 a1b1 a1b2b1 a2b1b2 B1B2C b1 c1 b1 c2 b1b2c3 b2 c4 R S AB1B2 a1b1 R lxlo S C c1 a1b1 c2 a2b1b2c3 a1b2b1null b2 c4
  • Slide 10
  • 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
  • Slide 11
  • 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
  • Slide 12
  • 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
  • Slide 13
  • 13 1.3 Left outer join AB1B2 a1b1 a1b2b1 a2b1b2 B1B2C b1 c1 b1 c2 b1b2c3 b2 c4 R S AB1B2 a1b1 R left outer join S C c1 a1b1 c2 a2b1b2c3 a1b2b1null
  • Slide 14
  • 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
  • Slide 15
  • 15 1.4 Right outer join AB1B2 a1b1 a1b2b1 a2b1b2 B1B2C b1 c1 b1 c2 b1b2c3 b2 c4 R S AB1B2 a1b1 R right outer join S C c1 a1b1 c2 a2b1b2c3 nullb2 c4
  • Slide 16
  • 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
  • Slide 17
  • 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')
  • Slide 18
  • 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'))
  • Slide 19
  • 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
  • Slide 20
  • 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')
  • Slide 21
  • 21 2.2 Sub queries other key words Some\ any \ all Exists
  • Slide 22
  • 22 2.2 Sub queries other key words (cont) Find agents (aid) with a minimum commission. Select aid from agents where percentage

Recommended

View more >