Sei sulla pagina 1di 12

DATABASE EXERCISE 1 SOLUTIONS

1) Get first name, last name and city from customers.

SELECT first_name,last_name,city FROM `customer`

2) Get customer id, first name, last name, city , country and mobile of a customer who belongs to
Sweden.

Select * FROM `customer` WHERE country=’Sweden’

3) Modify the city of supplier whose company name id is Pavlova Ltd.

UPDATE `suppliers` SET city=’Ahmedabad’ WHERE company_name=`Pavlova Ltd.’

4) Remove those products who unit price is more than 50.

DELETE from products WHERE unit_price > 50

5) Modify the table to specify that products whose price is more than 50 are discontinued.

UPDATE `products` SET is_discontinued=1 WHERE unit_price>50

6) Write a query to specify that product with product id PA000112 is no more continued.
UPDATE `products` SET is_discontinued=1 WHERE prod_id=’ PA000112’

7) Modify the city and mobile number to Oslo and (0)1-953530 of the supplier who has id

SUPL8744.

UPDATE `suppliers` SET city=’Oslo’,mobile=’(0)1-953530’ WHERE supplier_id=’SUPL8744’

8) Remove all products whose unit_price would be more than 50.

Same as 4)

9) Get company name, contact name, city and country of suppliers and list them in order of company
name.

SELECT company_name,contact_name,city,country FROM `suppliers` ORDER BY company_name

10) Get company name, contact name, city and country of suppliers and list them in descending order of
company name

SELECT company_name,contact_name,city,country FROM `suppliers` ORDER BY company_name DESC


11)Get the name, city and country of customers and list them by their country and city.

SELECT first_name,city,country FROM `customer` ORDER BY country,city

12) Write a query to fetch supplier id, company name, city and country where country is USA, Japan or
Germany and list them in ascending order of country and descending order of company name.

SELECT supplier_id, company_name,city,country FROM suppliers WHERE country IN


('USA','Japan','Germany') ORDER BY country,company_name DESC

13) Write a query to get order details and list them in ascending order of their year and descending
order of total amount.

SELECT * FROM ‘orders’ ORDER BY YEAR(order_date) ASC, total_amount DESC


14) Write a query to find the first 10 products having highest price.

SELECT * FROM products ORDER BY unit_price DESC LIMIT 10

15) Write a query to get details of products other than the first 4 having highest unit price.

SELECT * FROM `products` WHERE unit_price NOT IN (SELECT * FROM (SELECT unit_price FROM
`products` ORDER BY unit_price DESC LIMIT 4)abc);
16) Write a query to get details of all products except the first 5 having highest unit price.

SELECT * FROM `products` WHERE unit_price NOT IN (SELECT * FROM (SELECT unit_price FROM
`products` ORDER BY unit_price DESC LIMIT 5)abc);

17) Write a query to get unique countries in ascending order.

SELECT country FROM (SELECT DISTINCT country FROM customer UNION SELECT DISTINCT country
FROM suppliers)abc ORDER BY country;
18) Write a query to get total number of unique countries.

SELECT COUNT(DISTINCT country) AS TOTAl_COUNTRIES FROM (SELECT country FROM customer UNION
SELECT country FROM suppliers)abc;

19) Write a query to get the least unit price of a product.

SELECT MIN(`unit_price`) AS LEAST_UNIT_PRICE FROM products;


20) Write a query to find the highest amount where year of order is 2014.

SELECT MAX(total_amount) AS HIGHEST_AMOUNT FROM orders WHERE YEAR(order_date) = 2014;

21) Write a query to find total number of customers.

SELECT COUNT(DISTINCT cust_id) AS TOTAL_CUSTOMERS FROM `customer`


22) Find the total amount of orders placed in the year 2013.

SELECT COUNT(*) AS TOTAL_ORDERS FROM `orders` WHERE YEAR(order_date) = 2013;

23) Find the average of total amount and show as average.

SELECT AVG(total_amount) AS AVERAGE FROM `orders`

24) Write a query to show use of AND, OR and NOT clause.

AND:-

SELECT * FROM `customer` WHERE city = 'New York' AND country='USA';

OR:-

SELECT * FROM `customer` WHERE city = 'New York' OR city = 'Berlin';


NOT:-

SELECT * FROM `customer` WHERE NOT city = 'New York' AND NOT city='Berlin';

25) Find out all products whose price is from 10 to 20.

SELECT * FROM `products` WHERE `unit_price` BETWEEN 0 AND 10;

26) Find all customers belonging to countries of suppliers.

SELECT customer.* FROM `customer` INNER JOIN suppliers ON customer.country=suppliers.country;


27) Find products whose name starts with c.

SELECT * FROM `products` WHERE `prod_name` LIKE 'c%';

28) Find total number of customers from each country.

SELECT country,COUNT(cust_id) AS TOTAL_CUSTOMERS FROM `customer` GROUP BY `country`;

29) Get all customers who have placed an order with order details.

SELECT customer.*,orders.order_id,orders.order_no,orders.order_date,orders.total_amount FROM


`customer` INNER JOIN orders ON customer.cust_id=orders.cust_id;
30) Find total amount spent by each customer in descending order.

SELECT C.*,SUM(O.total_amount) AS TOTAL_SPENT FROM `customer` C INNER JOIN orders O ON


C.cust_id=O.cust_id GROUP BY O.cust_id ORDER BY TOTAL_SPENT DESC;

31) Find total customers whose id is greater than 1.

32) Get details of customer with total amount spent in descending order. Concatenate first
name and last name in result.

SELECT C.cust_id,CONCAT(C.first_name,' ',C.last_name) AS FULL_NAME,SUM(O.total_amount) AS


TOTAL_SPENT FROM `customer` C INNER JOIN orders O ON C.cust_id=O.cust_id GROUP BY O.cust_id
ORDER BY TOTAL_SPENT DESC;
33) Get name of customer and products they ordered.

34) Explain left join, right join, full join.

Potrebbero piacerti anche