0% found this document useful (0 votes)
12 views2 pages

Exercise Set 3

Uploaded by

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

Exercise Set 3

Uploaded by

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

====== Exercise 3 - SQL Basics 2 ======

1 | Count how many customers have postalcode length of 5. Use aliasname


postalcode_count for this column.
--------------------------------------------------
select count(*) as postalcode_count
from customers
where length(PostalCode) = 5;
--------------------------------------------------

##################################################
2 | Select minimum and maximum price from products with product name starting with
letters A, E, K or M.
--------------------------------------------------
select min(price) as min_price, max(price) as max_price
from products
where left(productname, 1) in ('A', 'E', 'K', 'M');
--------------------------------------------------

##################################################
3 | Calculate average price for each categoryID. Present average prices with two
decimals and order the result set by average price in descending order.
--------------------------------------------------
select CategoryID, round(avg(Price), 2) as avg_price
from products
group by CategoryID
order by avg_price desc;
--------------------------------------------------

##################################################
4 | Present employees in the following format in one column: firstname lastname is
born in year. For example "Anna Davis is born in 1992".
Use aliasname employee_info for this column. Order the result set by birth year so
that youngest employee will be shown first.
--------------------------------------------------
select concat(FirstName, ' ', LastName, ' is born in ', year(BirthDate)) as
employee_info
from employees
order by BirthDate desc;
--------------------------------------------------

##################################################
5 | Select employees who have been in more than 20 orders. Show employeeID and
count of orders in the result set. Use order_count aliasname for the column.
--------------------------------------------------
select EmployeeID, count(*) as order_count
from orders
group by EmployeeID having order_count > 20;
--------------------------------------------------

##################################################
6 | Create usernames for the employees by using the following format: two first
letters from the firstname, three last letters from the lastname
and length of the employee notes. For example, Nancy Davolio would have a username
of Nalio167.
--------------------------------------------------
select concat(substring(FirstName, 1, 2), substring(LastName, -3), length(Notes))
as username
from employees;
--------------------------------------------------

##################################################
7 | Count how many pieces of each product have been sold. Present productID and
total amount of sold products in the result set.
Use alias name total for the column and order the result set by count in descending
order.
--------------------------------------------------
select ProductID, sum(Quantity) as total
from orderdetails
group by ProductID
order by total desc;
--------------------------------------------------

##################################################
8 | Categorise supplier phone numbers with the following criteria:
- If phone number has brackets and a dash included -> Valid
- If phone number has either brackets or dash (not both) -> Partly valid
- In any other case -> Not a phone number
Present only phone number and categorised value in the result set.
--------------------------------------------------
select Phone,
case
when Phone like '%(%)%-%' then 'Valid'
when Phone like '%(%)%' or Phone like '%-%' then 'Partly valid'
else 'Not a phone number'
end as phone_category
from suppliers;
--------------------------------------------------

##################################################
9 | Show number of orders for each customerID. Include only orders that have been
shipped by shipperID 1 or 3.
Only those customers should be presented in the result set who have done 3 or more
orders. Result set should include
customerID and order count (use aliasname number_of_orders for the column).
--------------------------------------------------
select CustomerID, count(*) as number_of_orders
from orders
where ShipperID in (1, 3)
group by CustomerID having number_of_orders >= 3;
--------------------------------------------------

##################################################
10 | Do the following calculations for each supplierID:
- total price of products (use alias total_price)
- average price of products (use alias average_price and present without
decimals)
- amount of products (use alias amount_of_products)
- amount of missing product price values (use alias null_count)
For example, supplierID 1 has the following values (total_price: 85, average_price:
17, amount_of_products: 5, null_count: 0)
--------------------------------------------------
select SupplierID, sum(Price) as total_price, round(avg(Price)) as average_price,
count(*) as amount_of_products, sum(case when Price is null then 1 else 0 end) as
null_count
from products
group by SupplierID;
--------------------------------------------------

You might also like