0% found this document useful (0 votes)
2 views

SQL query excercise

The document contains a series of SQL queries designed to extract specific data from customer and salesman tables, as well as an orders table. It includes tasks such as identifying unique salespeople, locating customers based on various criteria, and calculating totals and maximums for orders. The queries also involve subqueries and joins to analyze relationships between customers and salespeople.

Uploaded by

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

SQL query excercise

The document contains a series of SQL queries designed to extract specific data from customer and salesman tables, as well as an orders table. It includes tasks such as identifying unique salespeople, locating customers based on various criteria, and calculating totals and maximums for orders. The queries also involve subqueries and joins to analyze relationships between customers and salespeople.

Uploaded by

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

Customer

customer_id | cust_name | city | grade | salesman_id

3002 | Nick Rimando | New York | 100 | 5001

3007 | Brad Davis | New York | 200 | 5001

3005 | Graham Zusi | California | 200 | 5002

3008 | Julian Green | London | 300 | 5002

3004 | Fabian Johnson | Paris | 300 | 5006

3009 | Geoff Cameron | Berlin | 100 | 5003

3003 | Jozy Altidor | Moscow | 200 | 5007

3001 | Brad Guzan | London | | 5005

Sample table: salesman


salesman_id | name | city | commission
-------------+------------+----------+------------
5001 | James Hoog | New York | 0.15
5002 | Nail Knite | Paris | 0.13
5005 | Pit Alex | London | 0.11
5006 | Mc Lyon | Paris | 0.14
5007 | Paul Adam | Rome | 0.13
5003 | Lauson Hen | San Jose | 0.12

Create a relationship between 2 tables primary Key and foreign


key.

1)From the following table, write a SQL query to identify the unique
salespeople ID. Return salesman_id.

2) From the following table, write a SQL query to locate salespeople who live
in the city of 'Paris'. Return salesperson's name, city.

3) From the following table, write a SQL query to locate the details of
customers with grade values above 100. Return customer_id, cust_name, city,
grade, and salesman_id.
4) From the following table, write a SQL query to find customers who are
either from the city 'New York' or who do not have a grade greater than 100.
Return customer_id, cust_name, city, grade, and salesman_id.

5) From the following table, write a SQL query to identify customers who do
not belong to the city of 'New York' or have a grade value that exceeds 100.
Return customer_id, cust_name, city, grade, and salesman_id.

6) From the following table, write a SQL query to find the details of those
salespeople whose commissions range from 0.10 to0.12. Return salesman_id,
name, city, and commission.

7) From the following table, write a SQL query to retrieve the details of all
customers whose ID belongs to any of the values 3007, 3008 or 3009. Return
customer_id, cust_name, city, grade, and salesman_id.

8) From the following table, write a SQL query to find salespeople who receive
commissions between 0.12 and 0.14 (begin and end values are included).
Return salesman_id, name, city, and commission.

9) From the following table, write a SQL query to retrieve the details of the
salespeople whose names begin with any letter between 'A' and 'L' (not
inclusive). Return salesman_id, name, city, commission.

10) From the following table, write a SQL query to retrieve the details of the
customers whose names begins with the letter 'B'. Return customer_id,
cust_name, city, grade, salesman_id..

11) From the following table, write a SQL query to find the details of those
salespeople whose names begin with ‘N’ and the fourth character is 'l'. Rests
may be any character. Return salesman_id, name, city, commission.

12) From the following table, write a SQL query to find all those customers
who does not have any grade. Return customer_id, cust_name, city, grade,
salesman_id.

13) From the following table, write a SQL query to calculate total purchase
amount of all orders. Return total purchase amount.

14) From the following table, write a SQL query to count the number of
customers. Return number of customers.

15) From the following table, write a SQL query to determine the number of
customers who received at least one grade for their activity.
16) From the following table, write a SQL query to find the highest grade of
the customers in each city. Return city, maximum grade.

17) From the following table, write a SQL query to find the highest purchase
amount ordered by each customer on a particular date. Return, order date and
highest purchase amount.

Sample table: orders


ord_no purch_amt ord_date customer_id salesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001

18) From the following table, write a SQL query to find the highest order
(purchase) amount by each customer on a particular order date. Filter the
result by highest order (purchase) amount above 2000.00. Return customer
id, order date and maximum purchase amount.

19) From the following table, write a SQL query to determine the maximum
order amount for each customer. The customer ID should be in the range
3002 and 3007(Begin and end values are included.). Return customer id and
maximum purchase amount.

Sample Output:
customer_id max
3002 5760.00
3007 2400.60
3004 1983.43
3003 75.29
3005 948.50

20) From the following table, write a SQL query to determine the maximum
order (purchase) amount generated by each salesperson. Filter the rows for
the salesperson ID is in the range 5003 and 5008 (Begin and end values are
included.). Return salesperson id and maximum purchase amount.

Sample Output:
salesman_id max
5005 270.65
5003 2480.40
5007 75.29
5006 1983.43

21) From the following table, write a SQL query to count the number of orders
based on the combination of each order date and salesperson. Return order
date, salesperson id

Sample Output:
ord_date salesman_id count
2012-07-27 5001 1
2012-08-17 5007 1
2012-04-25 5001 1
2012-09-10 5002 1
2012-10-05 5002 1
2012-10-10 5003 1
2012-09-10 5005 1
2012-08-17 5003 1
2012-06-27 5002 1
2012-09-10 5001 1
2012-10-05 5001 1
2012-10-10 5006 1

SQL SUBQUERIES

22) From the following tables, write a SQL query to find all the orders issued by
the salesman 'Paul Adam'. Return ord_no, purch_amt, ord_date, customer_id
and salesman_id.
23) From the following tables write a SQL query to find all orders generated by
the salespeople who may work for customers whose id is 3007. Return
ord_no, purch_amt, ord_date, customer_id, salesman_id.
24) From the following tables, write a SQL query to find all the orders
generated in New York city. Return ord_no, purch_amt, ord_date, customer_id
and salesman_id.

25) From the following tables, write a SQL query to find those salespeople
who earned the maximum commission. Return ord_no, purch_amt, ord_date,
and salesman_id.
26) From the following tables write a SQL query to find those orders, which
are higher than the average amount of the orders. Return ord_no, purch_amt,
ord_date, customer_id and salesman_id.
JOINS
27) From the following tables write a SQL query to find the salesperson and
customer who reside in the same city. Return Salesman, cust_name and city.

28) From the following tables write a SQL query to locate those salespeople
who do not live in the same city where their customers live and have received
a commission of more than 12% from the company. Return Customer Name,
customer city, Salesman, salesman city, commission.

29) From the following tables write a SQL query to find those orders where the
order amount exists between 500 and 2000. Return ord_no, purch_amt,
cust_name, city.

30) From the following tables write a SQL query to display the customer name,
customer city, grade, salesman, salesman city. The results should be sorted
by ascending customer_id.

You might also like