SQL database : sales
https://www.w3schools.com/sql/sql_select.asp
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_columns
employee
customer s Products Shippers Suppliers
CustomerID EmployeeID ProductID ShipperID SupplierID
CustomerNam LastName ProductName ShipperNam SupplierNam
e e e
ContactName FirstName SupplierID Phone ContactName
Address BirthDate CategoryID Address
City Photo Unit City
PostalCode Notes Price PostalCode
Country Country
Phone
OrderDetail
Orders s
OrderID OrderDetailID
CustomerID OrderID
EmployeeID ProductID
OrderDate Quantity
Narra Raghavendra Rao , IT Faculty :: 9849523151
ShipperID
Narra Raghavendra Rao , IT Faculty :: 9849523151
SQL commands
Select * from Customers;
Select * from Employees;
Select * from Products
Select CustomerID, CustomerName, City, Country from Customers;
SELECT * FROM Customers WHERE Country='USA';
Select CustomerID, CustomerName, City from Customers WHERE country = ”GERMANY”
Select * From Products where Price > 100;
SELECT TOP 3 * FROM Products; -- first 3 records in product data
SELECT TOP 3 price,Productid,productname,unit FROM products order by price desc; -- top 3 price product info
Select all customers from Spain that starts with the letter 'G':
SELECT * FROM Customers
WHERE Country = 'Spain' AND CustomerName LIKE 'G%';
Select all customers from Germany or Spain:
SELECT * FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';
Select only the first 3 records of the Customers table:
SELECT TOP 3 * FROM Customers;
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
The percent sign % represents zero, one, or multiple characters
The underscore sign _ represents one, single character
Select all customers that starts with the letter "a":
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them. For example to get data from orders and customer tables.
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Customer wise orders
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
Narra Raghavendra Rao , IT Faculty :: 9849523151
Product wise sales
SELECT products.productid,Productname,sUM(Quantity) as saleQty,sum(quantity*price)as Amount FROM
OrderDetails inner join products on orderdetails.productid=products.productid group by
products.productId,productname;
SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all records from the left table (table1), and the matching
records from the right table (table2). The result is 0 records from the right side, if there is
no match.
select all customers, and any orders they might have:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no
match.
SQL statement will return all employees, and any orders they might have placed:
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
SQL Aggregate Functions
MIN() - returns the smallest value within the selected column
MAX() - returns the largest value within the selected column
COUNT() - returns the number of rows in a set
SUM() - returns the total sum of a numerical column
AVG() - returns the average value of a numerical column
SELECT MIN(Price),max(price),avg(price) FROM Products;
SELECT productid,sUM(Quantity) as saleQty FROM OrderDetails group by productId; -- product wise sales qty
SELECT Country ,COUNT(CustomerID) FROM Customers GROUP BY Country;
SQL GROUP BY Statement
GROUP BY statement groups rows that have the same values into summary rows, like "find
the number of customers in each country".
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
lists the number of customers in each country:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
Narra Raghavendra Rao , IT Faculty :: 9849523151
GROUP BY With JOIN Example
The following SQL statement lists the number of orders sent by each shipper:
Example
SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM O
rders
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;
SQL CASE Examples
he CASE expression goes through conditions and returns a value when the first condition
is met (like an if-then-else statement). So, once a condition is true, it will stop reading and
return the result. If no conditions are true, it returns the value in the ELSE clause.
SQL goes through conditions and returns a value when the first condition is met:
Example
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
Output:
OrderID Quantity QuantityText
10248 12 The quantity is under 30
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
SQL UPDATE Statement
UPDATE Customers SET city = ‘New Jersy’ where CustomerID = 1;
UPDATE Products SET price = price + 10 WHERE CategoryID in (1,3,4);
SQL DELETE Statement
Delete from products where productid=77 -- its remove 77 product record from product table
Delete from orderdetails where ordered >=100
Narra Raghavendra Rao , IT Faculty :: 9849523151