SQL QUERY
SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY SQL QUERY
SQL QUERY SQL QUERY
SQL QUERY
SQL QUERY
PRACTICAL 01
Consider the following Schema of Shopping Database;
CUSTOMER (CustomerId, FirstName, Last Name,City,Country,Phone)
ORDERS(OrderId, OrderDate, Quantity, ProductId, TotalAmount ,CustomerID)
PRODUCT (ProductId, ProductName, SupplierId, UnitPrice)
SUPPLIER (SupplierId, CompanyName ,ContactName,City,Country)
CUSTOMER
ORDERS
PRODUCT
SUPPLIER
1. List all customers
SELECT * FROM customers;
2. List the customers’first and last name as full name in Mexico
SELECT CONCAT(FirstName," ",LastName) AS FullName
FROM customers WHERE Country = "México";
3. List all Company Name of suppliers in alphabetical order.
SELECT CompanyName FROM Supplier ORDER BY CompanyName ASC;
4. List all suppliers in reverse alphabetical order.
SELECT * FROM Supplier ORDER BY CompanyName DESC;
5. List all suppliers in the USA,and Japan, ordered by city,
then by company name in reverse order.
SELECT SupplierId,CompanyName,ContactName,City,Country FROM Supplier
WHERE Country IN('Japan','USA') ORDER BY City ASC,CompanyName DESC;
6. Show all orders, sorted by total amount, the largest first, within each year.
SELECT OrderId,OrderDate,Quantity,ProductId,TotalAmount,CustomerId FROM Orders
ORDER BY YEAR(OrderDate)ASC,TotalAmount DESC;
7. List top 10 most expensive products.
SELECT ProductId,ProductName,UnitPrice FROM product
ORDER BY UnitPrice DESC LIMIT 10;
8. List all supplier countries in alphabetical order(column should have distinct value ).
SELECT DISTINCT Country FROM supplier ORDER BY Country ASC;
9. Find the largest amount order placed in 2012.
SELECT MAX(Quantity) FROM orders WHERE YEAR(OrderDate)=2012;
10. List the number of customers in each country.
SELECT COUNT(CustomerId), Country FROM customers GROUP BY Country;
11. Compute the total amount sold in 2012.
SELECT SUM(TotalAmount) FROM orders WHERE YEAR(OrderDate)=2012;
12. List all Id, FirstName, LastName, City,and Country of the customers that are not from the
Germany
SELECT CustomerId,FirstName,LastName,City,Country FROM customers WHERE NOT
country='Germany';
13. List all customers’details from Spain or France.
SELECT * FROM customers WHERE country='Spain' OR country='France';
14. List all products between 10 and 20.
SELECT * FROM product WHERE UnitPrice BETWEEN 10 AND 20;
15. Get the number of orders and amount sold between 01/05/2012 and 01/12/2012
for each customer.
SELECT SUM(TotalAmount),COUNT(OrderId) FROM orders WHERE OrderDate
BETWEEN '2012-05-01' AND '2012-12-01' GROUP BY CustomerId;
16. List all suppliers from the USA, UK, OR Japan.
SELECT SupplierId,CompanyName,ContactName,City,Country FROM supplier
WHERE Country IN('USA','UK','Japan');
17. List all products PRICE that are not exactly 10, 20, 30, 40, or 50.
SELECT ProductId,ProductName,SupplierId,UnitPrice FROM product
WHERE NOT UnitPrice IN(10,20,30,40,50);
18. List the number of customers in each country. Only include countries with more than
10 customers.
SELECT COUNT(Country),Country FROM customers GROUP BY Country
HAVING COUNT(CustomerId) > 10;
19. List all orders with customer information.
SELECT Orders.OrderId, Orders.OrderDate, Orders.Quantity, Orders.ProductId,
Orders.TotalAmount, customers.CustomerId, customers.FirstName, customers.LastName,
customers.City, customers.Country, customers.Phone FROM Orders
JOIN customers ON orders.CustomerId = customers.CustomerId;
Example:
SELECT COUNT(Country),Country FROM customers GROUP BY Country HAVING COUNT(CustomerId) > 0;
20. List all orders with product names, quantities, and prices.
SELECT Orders.OrderId, Orders.OrderDate, Orders.Quantity, Orders.ProductId,
Orders.TotalAmount, product.ProductName, product.UnitPrice FROM Orders
JOIN product ON Orders.ProductId = Product.ProductId;
21. List the total amount ordered for each customer with customers’first name.
SELECT customers.CustomerId, customers.FirstName, COUNT(orders.OrderId),
SUM(orders.TotalAmount)FROM orders
INNER JOIN customers ON orders.CustomerId = customers.CustomerId
GROUP BY orders.CustomerId;
22. List top 3 most expensive products, unit price and country that produced.
SELECT product.ProductName, product.UnitPrice, supplier.Country FROM product
INNER JOIN supplier ON product.SupplierId = supplier.SupplierId
ORDER BY product.UnitPrice DESC LIMIT 3;
23. List the products’information that are produced in UK.
SELECT product.ProductId, product.ProductName, product.UnitPrice, supplier.Country
FROM product INNER JOIN supplier ON product.SupplierId = supplier.SupplierId
WHERE supplier.Country='UK';

Sql query

  • 1.
    SQL QUERY SQL QUERYSQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY SQL QUERY PRACTICAL 01 Consider the following Schema of Shopping Database; CUSTOMER (CustomerId, FirstName, Last Name,City,Country,Phone) ORDERS(OrderId, OrderDate, Quantity, ProductId, TotalAmount ,CustomerID) PRODUCT (ProductId, ProductName, SupplierId, UnitPrice) SUPPLIER (SupplierId, CompanyName ,ContactName,City,Country) CUSTOMER ORDERS PRODUCT SUPPLIER
  • 2.
    1. List allcustomers SELECT * FROM customers; 2. List the customers’first and last name as full name in Mexico SELECT CONCAT(FirstName," ",LastName) AS FullName FROM customers WHERE Country = "México"; 3. List all Company Name of suppliers in alphabetical order. SELECT CompanyName FROM Supplier ORDER BY CompanyName ASC; 4. List all suppliers in reverse alphabetical order. SELECT * FROM Supplier ORDER BY CompanyName DESC; 5. List all suppliers in the USA,and Japan, ordered by city, then by company name in reverse order. SELECT SupplierId,CompanyName,ContactName,City,Country FROM Supplier WHERE Country IN('Japan','USA') ORDER BY City ASC,CompanyName DESC;
  • 3.
    6. Show allorders, sorted by total amount, the largest first, within each year. SELECT OrderId,OrderDate,Quantity,ProductId,TotalAmount,CustomerId FROM Orders ORDER BY YEAR(OrderDate)ASC,TotalAmount DESC; 7. List top 10 most expensive products. SELECT ProductId,ProductName,UnitPrice FROM product ORDER BY UnitPrice DESC LIMIT 10; 8. List all supplier countries in alphabetical order(column should have distinct value ). SELECT DISTINCT Country FROM supplier ORDER BY Country ASC; 9. Find the largest amount order placed in 2012. SELECT MAX(Quantity) FROM orders WHERE YEAR(OrderDate)=2012; 10. List the number of customers in each country. SELECT COUNT(CustomerId), Country FROM customers GROUP BY Country;
  • 4.
    11. Compute thetotal amount sold in 2012. SELECT SUM(TotalAmount) FROM orders WHERE YEAR(OrderDate)=2012; 12. List all Id, FirstName, LastName, City,and Country of the customers that are not from the Germany SELECT CustomerId,FirstName,LastName,City,Country FROM customers WHERE NOT country='Germany'; 13. List all customers’details from Spain or France. SELECT * FROM customers WHERE country='Spain' OR country='France'; 14. List all products between 10 and 20. SELECT * FROM product WHERE UnitPrice BETWEEN 10 AND 20; 15. Get the number of orders and amount sold between 01/05/2012 and 01/12/2012 for each customer. SELECT SUM(TotalAmount),COUNT(OrderId) FROM orders WHERE OrderDate BETWEEN '2012-05-01' AND '2012-12-01' GROUP BY CustomerId; 16. List all suppliers from the USA, UK, OR Japan. SELECT SupplierId,CompanyName,ContactName,City,Country FROM supplier WHERE Country IN('USA','UK','Japan');
  • 5.
    17. List allproducts PRICE that are not exactly 10, 20, 30, 40, or 50. SELECT ProductId,ProductName,SupplierId,UnitPrice FROM product WHERE NOT UnitPrice IN(10,20,30,40,50); 18. List the number of customers in each country. Only include countries with more than 10 customers. SELECT COUNT(Country),Country FROM customers GROUP BY Country HAVING COUNT(CustomerId) > 10; 19. List all orders with customer information. SELECT Orders.OrderId, Orders.OrderDate, Orders.Quantity, Orders.ProductId, Orders.TotalAmount, customers.CustomerId, customers.FirstName, customers.LastName, customers.City, customers.Country, customers.Phone FROM Orders JOIN customers ON orders.CustomerId = customers.CustomerId; Example: SELECT COUNT(Country),Country FROM customers GROUP BY Country HAVING COUNT(CustomerId) > 0;
  • 6.
    20. List allorders with product names, quantities, and prices. SELECT Orders.OrderId, Orders.OrderDate, Orders.Quantity, Orders.ProductId, Orders.TotalAmount, product.ProductName, product.UnitPrice FROM Orders JOIN product ON Orders.ProductId = Product.ProductId; 21. List the total amount ordered for each customer with customers’first name. SELECT customers.CustomerId, customers.FirstName, COUNT(orders.OrderId), SUM(orders.TotalAmount)FROM orders INNER JOIN customers ON orders.CustomerId = customers.CustomerId GROUP BY orders.CustomerId; 22. List top 3 most expensive products, unit price and country that produced. SELECT product.ProductName, product.UnitPrice, supplier.Country FROM product INNER JOIN supplier ON product.SupplierId = supplier.SupplierId ORDER BY product.UnitPrice DESC LIMIT 3; 23. List the products’information that are produced in UK. SELECT product.ProductId, product.ProductName, product.UnitPrice, supplier.Country FROM product INNER JOIN supplier ON product.SupplierId = supplier.SupplierId WHERE supplier.Country='UK';