Sunteți pe pagina 1din 3

1.

Display all columns for everyone whose customer last name equals Smith
o Select * from customer where lastname = Smith

2. Display all columns for everyone whose first name begins with the letter L

Select * from customer where firstname like L%

3. Give me a list of all the unique states and count of how many times they
occurred in the customer table
o Select state, count (*) from customer group by state

4. Give me the max age of the customer in the customer table


o Select max(age) from customer

5. Return all unique ages from the customer table and order it from oldest to youngest
o Select distinct age from customer order by age desc -ORo Select age from customer Group by age order by age desc

6. Give me a list of unique states and the no.of unique customers in that state
that have more than one customer. Select the state and display the number of how many people are in each if it's greater than 1.

Select state, count(distinct customerid) From Customers Group by state Having count(distinct customerid)>1

7. Select the date, item, and price from the items_ordered table for all of the rows that have a price value ranging from 10.00 to 80.00.
o Select date, item, price from items_ordered where price between (10.00 and 80.00) OR o Select date, item, price from items_ordered where price >=10.00 and price <=80.00

8. Write a query to list the name of the customer(in the format lastname,

firstname and name the column Name) and phone(Phone Number should be in the following format (xxx)xxx-xxxx).

Select lastname || , || firstname as Name, ( || substring(phone, 1,3) || ) || substring(phone, 4,3) || - || substring(phone, 7, 4) as Phone from Customers

9. Write a query that will give me a list of what each customer ordered and with
customer last name in the descending order (Show me only the fields customerid, lastname, firstname, order_date, item, quantity, and price)

Select customerid, lastname, firstname, order_date, item, quantity,price from customer, items_ordered where customers.customerid = items_ordered.customerid order by lastname DESC

10. List all customers and their orders but only give me customer who ordered
more than one item (Show customer name and item)
* there are couples of ways to do this but here is one

Select customer.firstname, customer.lastname, Items_Ordered.Item FROM Customer LEFT JOIN Items_ordered ON customers.customerid = items_ordered.customerid Group by 1,2,3 Having count (item) > 1

11. List the customer (first and last name) that spent the max amount and on what product (Show customer name and product)
o Select customerID, lastname, firstname, item from customer left join Items_ordered on customer.customerID=Items_ordered. CustomerID and price = (Select max(price) from Items_ordered)

12. Write a list of all of the customers (first and last name) that did not order anything off of the items_ordered table.

Select firstname, lastname FROM customer c Left outer join items_Ordered I On c.Customerid = i.customerID Where i.customerid is NULL

ORSelect firstname, lastname FROM customer Where customerid not in (Select distinct customerid from Items_ordered)

13. Write a query to get customer name, age and Age_Category. Age_Category

will be the new column name and should be set based on age per conditions below. o o o If Age less than 25 Age_Category should be set to Young If Age between 25 and 50 set to Old If Age more than 50 set to Senior

Select FirstName, LastName, Age , case when age<25 then Young when (age >=25 and age <=50) then old when age > 50 then Senior end as Age_Category From customers

14. Write a query that will return all items that have a Price that is higher than
the average Price of all the items_ordered.

SELECT Item, Price FROM items_ordered WHERE Price > (SELECT Avg(Price) as AvgPrice FROM items_ordered) - Or -

o -

SELECT Item, Price FROM (SELECT Avg(Price) as AvgPrice FROM items_ordered) as A, items_ordered WHERE Price > a.AvgPrice Or

SELECT Item, Price FROM items_ordered GROUP BY Item, Price HAVING Price > avg(Price)

15. Write a query to list customerID, lastname, firstname, order_date of all the

customers that ordered products between month February and September of current year
o Select customerID, lastname, firstname, order_date from Customer left join Items_ordered on customer.customerID=Items_ordered. CustomerID where year (order_date)=year(now()) and (month(order_date) between (2 and 9))
** It is acceptable if the date is hard coded

16. Write a query to list the customers that paid maximum price and the second maximum price. Show the name of the customer and the price
o Select top 2 customer.firstname, customer.lastname, price from Items_Ordered left join customers on Items_ordered. CustomerID = customer.customerID order by price desc;

**This one looks easy in sql, but tests the logical thinking

17. Write a query to update quantity from 1 to 5 for customer John Smith with the order_date of June 30, 1999.

Update Items_Ordered from Customer left join Items_ordered on customer.customerID=Items_ordered. CustomerID set quantity=5 Where customer.fristname=John and customer.lastname=Smith and order_Date=1999-06-30 And quanitiy=1

S-ar putea să vă placă și