0% found this document useful (0 votes)
180 views8 pages

Query 1

This document contains 56 SQL queries that: 1) Select, join, and filter data from tables like Salespeople, Customers, and Orders 2) Perform aggregation using functions like COUNT, SUM, AVG, and MAX 3) Use operators like BETWEEN, IN, LIKE, and > to filter results 4) Group and sort data using GROUP BY and ORDER BY clauses The queries analyze sales data by finding highest ratings, largest orders, salesperson commissions, and more.

Uploaded by

Dori Water
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views8 pages

Query 1

This document contains 56 SQL queries that: 1) Select, join, and filter data from tables like Salespeople, Customers, and Orders 2) Perform aggregation using functions like COUNT, SUM, AVG, and MAX 3) Use operators like BETWEEN, IN, LIKE, and > to filter results 4) Group and sort data using GROUP BY and ORDER BY clauses The queries analyze sales data by finding highest ratings, largest orders, salesperson commissions, and more.

Uploaded by

Dori Water
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

--1.List all the column of the salespeople table.

Select * from Salespeople

--2 List all customers with a rating of 100.

select CNUM,CNAME from Customers where RATING=100

--3.Find the largest order taken by each salesperson on each date.

select SNUM,ODATE,max(AMT) from Orders group by ODATE,SNUM

--4.Arrange the Order table by descending customer number

select * from Orders order by CNUM desc

--5.Find which salespeople currently have orders in the order table.

select SNAME from Salespeople where SNUM in(select SNUM from Orders)

--6List names of all customers matched with the salespeople serving them.

select CNAME from Customers where SNUM in(select SNUM from Salespeople)

--7Count the orders of each of the salespeople and output the results in
descending order

select count(ONUM),SNUM from Orders

group by SNUM order by count(ONUM) desc

--8.Match salespeople to customers according to what city they live in.

select SNAME,CNAME,s.CITY from Salespeople s,Customers c

where s.CITY=c.CITY

--9Find all the customers in SanJose who have a rating above 200.
select * from Customers where RATING>200 and CITY='SanJose'

--10List the names and commissions of all salespeople in London

select distinct SNAME,COMM,CITY from Salespeople where CITY='London'

--11.List all the orders of Salesperson Motika from the orders table

select ONUM from orders where SNUM=(select SNUM from salespeople where
SNAME='Motika')

--12 Find all customers who booked orders on October 3.

select * from Customers where SNUM in(select SNUM from Orders where
ODATE='03/10/90')

--13.Give the sums of the amounts from the Orders table, grouped by date,

--eliminating all those dates where the SUM was not at least 2000 above the
maximum Amount.

select ODATE,sum(AMT) from Orders group by ODATE

--14 Select all orders that had amounts that were greater than at least one of the
orders from

--October 6..

select * from Orders where AMT>any(select AMT from Orders where ODATE='06/10/90')

--15 List all the largest orders for October 3, for each salesperson.

select max(AMT),SNUM,ODATE from Orders group by (ODATE),SNUM having


ODATE='03/10/90'

--16 Find all customers located in cities where Serres has customers.

--17 Select all customers with a rating above 200.

select *from Customers where RATING>200


--18 Find salespeople with customers located in their own cities.

select s.sname,c.cname,s.city from Salespeople s,Customers c where s.CITY=c.CITY

--19 Find salespeople whose name starts with P and fourth character is I

select * from Salespeople where SNAME like 'P__L%'

--20.Write a query that uses a subquery to obtain all orders for the customer
named Cisneros.Assume you do not know his customer number

select ONUM from Orders where SNUM=(select SNUM from Customers where
CNAME='Cisneros')

--21.Find the largest orders for Serres and Rifkin.

select SNUM,max(AMT) from orders group by SNUM

having SNUM in (select SNUM from Salespeople where SNAME='Serres' or


SNAME='Rifkin')

--22.Sort the salespeople table in the following order : snum, sname, commission,
city.

select SNAME,SNAME,COMM,CITY from Salespeople

--23 Select all customers whose names fall in between A and G alphabetical
range

select CNAME from Customers where CNAME between 'A' and 'G'order by CNAME asc

--24 Write a query that totals the orders for each day and places the results in
descending order.

select sum(amt) from orders group by odate order by sum(amt) desc

--or

select sum(amt) as amt,odate from orders group by odate order by sum(amt) desc;
--25Write a select command that produces the rating followed by the name of each
customer in SanJose.

select RATING,CNAME,CITY from Customers where CITY='SanJose'

--26 Find all orders with above average amounts for their customers.

select onum,AMT from orders where amt >(select avg(amt) from orders)

--27.Write a query that selects the highest rating in each city.

select city,max(rating) from Customers group by CITY

--28.Find all salespeople that are located in either Barcelona or London.

select SNAME,CITY from Salespeople where CITY='Barcelona' or CITY='London'

--29.Write a query that joins the customer table to itself to find all pairs or
customers served by a single salesperson.

select c.cname,c2.cname from customers c,customers c2 where c.snum=c2.snum

and c.cname!=c2.cname;

select a.cname,b.cname,c.sname from Customers a,Customers b,salespeople c where


a.snum=b.snum and a.snum=c.snum;

--30.Write a query that will give you all orders for more than $1000.00

select * from Orders where AMT>1000

--31.Write a query that lists each order number followed by the name of customer
who made that order.

select a.onum,b.cname from orders a,Customers b where a.cnum=b.cnum

--32.Write two queries that will produce all orders taken on October 3 or October
4.

select* from Orders where ODATE='03/10/90'

select* from Orders where ODATE='04/10/90'


select * from orders where odate='03/10/90' or odate='04/10/90'

--33.Find all rows from the customers table for which the sales person number is
1001.

select * from Customers where SNUM=1001

--34.Find all salespeople name and comminssion.

select SNAME,COMM from Salespeople

--35.Find all sales people for whom there are customers that follow them in
alphabetical order.

select a.sname,b.cname from salespeople a,Customers b where a.snum=b.snum order by


a.sname,b.CNAME

--36.Write a query that produces the names and ratings of all customers who have
average orders..

select CNAME,RATING from Customers where RATING>(select AVG(RATING) from


Customers)

--37. Find the sum of all Amounts from the orders table.

select sum(amt) as amount from Orders

--38. Write a query that gives the names of both the salesperson and the customer
for each order after the order number.

select s.sname,c.cname,o.ONUM from Salespeople s,Customers c,Orders o where


s.SNUM=c.SNUM and c.CNUM=o.CNUM

--39.List the commission of all salespeople services customers in London.

select a.sname,a.comm,b.city,b.cname from salespeople a,Customers b where


a.snum=b.snum and b.city='London'
--40.Find all orders attributed to sales people who live in London.

select b.sname,b.city,c.onum from Customers a,salespeople b,orders c where


a.snum=b.snum and a.cnum=c.cnum and b.city='London'

--41. Find all salespeople who have customers with more than one current order.

select distinct snum from orders where cnum in (select cnum from orders group by
cnum having count(cnum)>1)

--42.Find the average commission for salespeople in London.

select city,avg(comm) from Salespeople where CITY='London' group by CITY

--43.Find all orders credited to the same salesperson who services Hoffman. (cnum
2001).

select onum from orders where snum in(select snum from customers where
cname='Hoffman')

--or

select a.cname,b.sname,c.onum from Customers a,salespeople b,orders c where


a.snum=b.snum and a.cnum=c.cnum and a.cnum='2001'

--44.Find all salespeople whose commission is in between 0.10 and 0.12 (both
inclusive).

select * from Salespeople where COMM between 0.10 and 0.12

--or

select sname from salespeople where (comm>=0.10 and comm<=0.12)

--45.Write a query that will give you the names and cities of all salespeople in
London with acommission above 0.10.

select sname,city,comm from salespeople where (city='London' and comm>0.10)

--46.Write a query that selects each customers smallest order.

select cnum,min(amt) as amount from orders group by cnum

--47.Write a query that selects the first customer in alphabetical order whose
name begins withG.

select * from Customers where CNAME like 'G%' order by CNAME

--48.Write a query that counts the number of different non NULL city values in the
customerstable.

select count(distinct CITY) from Customers

--49. Find the average amount from the Orders Table..

select avg(amt) as avg from Orders

--50. Find all customers who are not located in SanJose and whose rating is above
200.

select * from Customers where CITY!='SanJose' and RATING>200

--51. Give a simpler way to write this query.SELECT snum, sname, city, comm. FROM
salespeopleWHERE (comm.>+0.12 OR comm.<0.14);

--52. Which salesperson has earned the maximum commission?

--53.List all customers in descending order of customer rating .

select cname,rating from Customers order by RATING desc

--54. On which days has Hoffman placed orders?.

select a.cname,b.onum,b.odate from Customers a,orders b where a.cnum=b.cnum and


a.cname='Hoffman'

--or

select onum,odate from orders where cnum in(select cnum from customers where
cname='Hoffman')

--55 Which salesmen have no orders between 10/03/1990 and 10/05/1990?


select b.sname,c.odate from Customers a,salespeople b,orders c where a.snum=b.snum
and a.cnum=c.cnum and odate not between '03/10/90' and '05/10/90'

--56. Who is the most successful salesperson?

select sum(comm*amt*0.01) as amt,sname from salespeople,orders group by sname


order by amt desc

You might also like