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

/* Chapter 14 */

/* simple join */

select vend_name, prod_name, prod_price


from vendors, products
where vendors.vend_id = products.vend_id
order by vend_name, prod_name;

/* importance of the where clause (cartesian product/ cross join) */

select vend_name, prod_name, prod_price


from vendors, products
order by vend_name, prod_name;

/* inner join, matches the id's with each other */

select customers.cust_id, customers.cust_name


from customers, orders
where datediff(month, order_date, '2005-09-01') = 0
and customers.cust_id = orders.cust_id;

/* when joining this is the right to use, always match the first table in the select
statement with the second etc. */
select vend_name, prod_name, prod_price
from vendors inner join products
on vendors.vend_id = products.vend_id

/* joining multiple tables, in the where statement you can track where the tables have
to be matched to each other */
select prod_name, vend_name, prod_price, quantity
from orderitems, products, vendors
where products.vend_id = vendors.vend_id
and orderitems.prod_id = products.prod_id
and order_num = 20005;

/* revision subqueries */

select cust_name, cust_contact


from customers
where cust_id in (select cust_id
from orders
where order_num in (select order_num
from
orderitems
where prod_id
= 'TNT2'));

select cust_name, cust_contact


from customers, orders, orderitems
where customers.cust_id = orders.cust_id
and orders.order_num = orderitems.order_num
and prod_id = tnt2

select customers.cust_id, orders.order_num


from customers, orders
order by cust_id, order_num;
/* chapter 15 */

/* the rtrim removes spaces left or right of a string */


select rtrim(vend_name) + '(' + rtrim(vend_country) + ')'
as vend_title
from vendors
order by vend_name;

/* using table aliases */


select cust_name, cust_contact
from customers as c, orders as o, orderitems as oi
where c.cust_id = o.cust_id
and o.order_num = oi.order_num
and prod_id = 'TNT2';

/* using a subquery repetition */


select prod_id, prod_name
from products
where vend_id = (select vend_id
from products
where prod_id = 'DTNTR');

/* to prevent sql from not knowing to which to compare, 2 aliases are used to compare the
table to it's own */
select p1.prod_id, p1.prod_name
from products as p1, products as p2
where p1.vend_id = p2.vend_id
and p2.prod_id = 'DTNTR';

/* Natural join, the * prevents a column name from appaering multiple times */
select c.*, o.order_num, o.order_date,
oi.prod_id, oi.quantity, oi.item_price
from customers as c, orders as o, orderitems as oi
where c.cust_id = o.cust_id
and o.order_num = oi.order_num
and prod_id = 'FB';

select customers.cust_id, orders.order_num


from customers inner join orders
on customers.cust_id = orders.cust_id;

/* outer join, combines another table but takes all the rows from the left or right table
*/
select customers.cust_id, orders.order_num
from customers left outer join orders
on customers.cust_id = orders.cust_id;

select customers.cust_id, orders.order_num


from customers right outer join orders
on orders.cust_id = customers.cust_id;

/* joins with aggregate functions */


select customers.cust_name,
customers.cust_id,
count(orders.order_num) as num_ord
from customers inner join orders
on customers.cust_id = orders.cust_id
group by customers.cust_name,
customers.cust_id;

select customers.cust_name,
customers.cust_id,
count(orders.order_num) as num_ord
from customers left outer join orders
on customers.cust_id = orders.cust_id
group by customers.cust_name,
customers.cust_id;

select cust_name
from customers
left join orders on customers.cust_id = orders.cust_id
where orders.cust_id is null;

select oi.*, c.*


from orders as o
full outer join customers as c on c.cust_id = o.cust_id
full outer join orderitems as oi on oi.order_num = o.order_num

select c.*, p.*


from vendors as v
full outer join customers as c on c.cust_id = o.cust_id
full outer join product as p on p.vend_id =

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