You are on page 1of 4

1) Get the maximum, minimum, total and average credit limit for all customers in

each country and each city and where the country and city are not null. Order the
result set by country in descending order and by city in ascending order.
(customers)

select max(creditLimit), min(creditLimit), avg(creditLimit), country, city from


customers
where country is not null and city is not null
group by country, city
order by country desc, city asc;

2) Get 10 customers which have the maximum amount (payments)

select customerNumber, amount from payments


order by amount desc
limit 10;

3) Write a query that will count how many orders were created starting from October
10th, 2003 and September 9th, 2004 for each status. (orders)

select count(orderNumber), `status` from orders


where orderDate between '2003-10-10' and '2004-9-9'
Group by `status`

4) Get the maximum payments and their average for each client. (payments)

select customerNumber, max(amount), avg(amount) from payments


group by customerNumber;

5) Get the maximum, minimum and average for quantityInStock and buyPrice columns,
where the product line is one of the following: (products)

-Classic Cars
-Trucks and Buses
-Motorcycles

select max(quantityInStock), avg(quantityInStock), max(buyPrice), avg(buyPrice)


from products
where productLine = 'Classic Cars' or productLine = 'Trucks and Buses' or
productLine = 'Motorcycles'

6) Get the product code, orderLineNumber and average of priceEach for every
product and every orderLineNumber where max priceEach is greater than 100. Order
the result set by product in descending order and orderLineNumber in ascending
order. (orderdetails)

select productCode, orderLineNumber, avg(priceEach) from orderdetails


group by productCode, orderLineNumber
having max(priceEach)>100
order by productCode desc, orderLineNumber asc;

7) Get a comma separated list of customerNames for every country. Order the result
set by country in descending order. (customers)

select group_concat(customerName order by customerName asc separator ', ') from


customers
group by country
order by country desc
8) Get customer name, phone number and column called
state_or_address_line2_or_country that will return first non null value out of
these three columns (state, addressLine2, country) where credit limit is greater
that 20000 and less than 100000; (customers)

select customerName, phone, coalesce(state, addressLine2, country) as


state_or_address_line2_or_country from customers
where creditLimit between 20000 and 100000

9) Get customer number, if amount is greater than 10000 divide it by 100 for all
payments where checkNumber starts with 'HQ'; (payments)

select customerNumber, checkNumber, amount / CASE WHEN checkNumber LIKE 'HQ%' AND
amount > 10000 THEN 100 ELSE 1 END AS updatedAmount
from payments
where checkNumber like 'HQ%' and amount > 10000;

10) Get city, phone number and addressLine2 from offices. If state is null return
'N/A' for all offices where country is USA (offices)

select city, phone, addressLine2, ifnull(state, 'N/A') from offices


where country='USA'

11) Get all orders where comment exist and status is 'Shipped'. We are interested
just in orders where shippedDate is between 2003-01-25 and 2003-03-20. (orders)

select orderNumber from orders


where comments is not null and `status` = 'Shipped' and shippedDate between 2003-
01-25 and 2003-03-20;

12) For all products calculate how much retailer will earn money by subracting buy
price from MSRP where product code starts with 'S12' OR 'S18' (products)

select productName, MSRP-buyPrice, productCode as moneyEarned from products


where productCode like 'S12%' or productCode like 'S18%';

13) For every product in order details table get product code and calculate
subtotal (quantity * price) for every product order where quantity is greater than
20 and price is less than 120. Round subtotal on two decimal places.
(orderdetails).

select productCode, round(quantityOrdered*priceEach, 2) as subtotal from


orderdetails
where quantityOrdered>20 and priceEach<120

14) Get first and last name, reportsTo and job title and email of all employees
whose job title is President or VP Sales or Sales Rep or those who reports to
office code 1. In email replace 'classicmodelcars.com' with 'ibu.edu.ba'. Result
set should be order by job title in descending order. (employees)

select firstName, lastName, reportsTo, jobTitle, replace(email,


'classicmodelcars.com', 'ibu.edu.ba') AS updatedEmail from employees
where jobTitle='President' or jobTitle='VP Sales' or jobTitle='Sales Rep' or
officeCode=1
order by jobTitle desc;

15) Get all product lines where productLine contains 'Cars' in its name.
(productlines)
select productLine from productlines
where productLine like '%Cars%'

16) Write a query that will retrieve the product number and the review related to
the product. (product, productreview)

select ProductNumber, ProductReviewID from product


inner join productreview on product.ProductID=productreview.ProductID

17) Write a query that will combine every product with every possible review
(product, productreview)

18) Get all product numbers and reviews, no matter is there a review for that
product (product, productreview)

19) Get all the reviews and product numbers no matter if that review has a product
or not. (product, productreview)

20) Write a query that will retrieve the product name, product line and the product
line text description (products, productlines) - classicmodels database

select productName, productlines.productLine, textDescription from productlines


inner join products on productlines.productLine=products.productLine

21) Write a query which will collect information on the quantity of every product
on each production location. The required data is located in three different
tables:
-Product (contains data about products)
-ProductInventory (contains data about quantity of every product on each location)
-Location (contains data about location of products) It is important to note which
common columns/keys are used to join these tables. (product, productinventory,
location)

Select product.Name, location.Name, Quantity from productinventory


inner join product on productinventory.ProductID=product.ProductID
inner join location on productinventory.LocationID=location.LocationID
group by product.ProductID, location.LocationID

22) Modify the previous query so that it returns the total quantity for every
product and every location. Order the result set by total quantity in descending
order.

Select product.Name, location.Name, sum(Quantity) from productinventory


inner join product on productinventory.ProductID=product.ProductID
inner join location on productinventory.LocationID=location.LocationID
group by product.ProductID, location.LocationID
order by Quantity desc

23) The company manager requires a list of all products, which next to the product
name should contain a name of the unit measure and the product vendor. We are
assigned to extract the data and present it to the manager. (product,
productvendor, vendor, unitmeasure)
select product.Name as ProductName, unitmeasure.Name as UnitMeasureName,
vendor.Name as VendorName from product
inner join productvendor on product.ProductID=productvendor.ProductID
inner join vendor on productvendor.VendorID=vendor.VendorID
inner join unitmeasure on productvendor.UnitMeasureCode=unitmeasure.UnitMeasureCode

24) Get total (each price * quantity) for all orders by status (orders,
orderdetails) - classicmodels

select priceEach*quantityOrdered as Total, `status` from orders


inner join orderdetails on orders.orderNumber=orderdetails.orderNumber
order by `status`

25) Write a query that will return distinct product model IDs and their date of
modification by using the UNION keyword (productmodelproductdescriptionculture)

26) Write a query that will return common product model IDs and their date of
modification by using the INTERSECT keyword (productmodelproductdescriptionculture)

You might also like