query

6

Click here to load reader

Upload: ujsh

Post on 10-Mar-2015

453 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Query

RDBMS ASSIGNMENT NO – 18

A. What are the names of customers who have sent packages (shipments) to Sioux City?

SELECT distinct cust_name FROM customer c,shipment swhere c.cust_id = s.cust_idand s.destination = 'Sioux City'

B. To what destinations have companies with revenue less than $1 million sent packages?

select destination from shipment s , customer cwhere s.cust_id = c.cust_idand annual_revenue < 1000000

C. What are the names and populations of cities that have received shipments weighing over 100 pounds?

select distinct c.city_name , population from city c,shipment swhere c.city_name = s.destinationand weight > 100

D. Who are the customers having over $5 million in annual revenue who have sent shipments weighing less than 1 pound?

SELECT cust_name FROM customer c,shipment swhere c.cust_id = s.cust_idand annual_revenue > 5000000and weight <= 1 ;

E. Who are the customers having over $5 million in annual revenue who have sent shipments weighing less than 1 pound or have sent a shipment to San Francisco?

SELECT cust_name FROM customer c,shipment swhere c.cust_id = s.cust_idand annual_revenue > 5000000and (weight <= 1 or destination = 'San Francisco')

Page 2: Query

F. Who are the drivers who have delivered shipments for customers with annual revenue over $20 million to cities with populations over 1 million?

SELECT distinct driver_name from truckn t,customer c,shipment s,city ciwhere t.truckid = s.truckidand s.cust_id = c.cust_idand ci.city_name = s.destinationand annual_revenue > 20000000and population > 1000000

G. List the cities that have received shipments from customers having over $15 million in annual revenue.

select city_name from city,customer c,shipment swhere city_name = destinationand c.cust_id = s.cust_idand annual_revenue > 15000000

H. List the names of drivers who have delivered shipments weighing over 100 pounds.

SELECT distinct driver_name from truckn t,shipment swhere t.truckid = s.truckidand weight > 100

I. List the name and annual revenue of customers who have sent shipments weighing over 100 pounds.

select distinct cust_name , annual_revenue from customer c,shipment swhere c.cust_id = s.cust_idand weight > 100

J. List the name and annual revenue of customers whose shipments have been delivered by truck driver Jensen.

SELECT distinct cust_name,annual_revenue from customer c,truckn t,shipment swhere t.truckid = s.truckidand c.cust_id = s.cust_idand driver_name = 'Jensen'

Page 3: Query

K. List customers who had shipments delivered by every truck. ( use NOT EXISTS)

select cust_name, cust_id from customerwhere not exists(select truckid from trucknwhere not exists(select * from shipmentwhere shipment.truckid= truckn.truckid and shipment.cust_id=customer.cust_id))

L. List cities that have received shipments from every customer. ( use NOT EXISTS)

select city_name from citywhere not exists(select * from customerwhere not exists(select destination from shipmentwhere shipment.destination = city.city_name and shipment.cust_id=customer.cust_id))

M. List drivers who have delivered shipments to every city. (use NOT EXISTS)

select driver_name from trucknwhere not exists(select * from citywhere not exists(select destination from shipmentwhere shipment.destination = city.city_name and shipment.truckid=truckn.truckid))

N. Customers who are manufacturers or have sent a package to St. Louis.

select distinct cust_name from customer c,shipment s where c.cust_id=s.cust_id and (cust_type = 'manufacturer' or destination = 'St. Louis')

O. Cities of population over 1 million which have received a 100-pound package From customer 311.

select city_name from city c,shipment s

Page 4: Query

where population > 1000000and c.city_name = s.destinationand s.weight = 100and s.cust_id = 311

P. Trucks driven by Jake Stinson which have never delivered a shipment to Denver.

SELECT t.truckid,t.driver_name From truckn t, shipment swhere s.truckid = t.truckidand t.driver_name='Jake Stinson'and s.destination != 'Denver'

Q. Customers with annual revenue over $10 million which have sent packages under 1 pound to cities with population less than 10,000.

select cust_name from customer c,shipment s,city ci

where annual_revenue > 10000000and c.cust_id = s.cust_idand ci.city_name = s.destinationand weight < 1and population < 1000

R. Create views for each of the following: a. Customers with annual revenue under $1 million. b. Customers with annual revenue between $1 million and $5 million. c. Customers with annual revenue over $5 million.

a)CREATE VIEW cust_under AS select * from customer where (annual_revenue < 1000000)

b)CREATE VIEW cust_bet AS select * from customer where (annual_revenue between 1000000 and 5000000)

c)CREATE VIEW cust_under AS select * from customer where (annual_revenue > 5000000)

S. Use these views to answer the following queries: a. Which drivers have taken shipments to Los Angeles for customers with revenue over $5 million?

Page 5: Query

b. What are the populations of cities which have received shipments from customers with revenue between $1 million and $5 million? c. Which drivers have taken shipments to cities for customers with revenue under $1 million, and what are the populations of those cities?

a)select driver_name from truckn t,shipment s,cust_over cwhere t.truckid = s.truckidand s.cust_id = c.cust_idand destination = 'Los Angeles'

b)select distinct population from city c,shipment s,cust_bet cuwhere c.city_name = s.destinationand s.cust_id = cu.cust_id

c)select distinct driver_name, population from city c,truckn t,shipment s,cust_view cuwhere t.truckid = s.truckidand s.destination = c.city_nameand s.cust_id = cu.cust_id