Database Notes

Embed Size (px)

DESCRIPTION

Notes for SQL

Citation preview

Section 2Spring 2014First 2 weeks : EXAMPLE:SELECT (Retrival Comand [Field]) SELECT CustomerNameFROM (Retrival Comand [Table]) FROM CustomerWHERE (Retrival Comand [Filter]) WHERE CreditLimit= 10000GROUP BYHAVINGORDER BYCREATE TABLE (creates tables)INSERT (add rows to a table)DROP (drop table)UPDATE (change data in a table)ALTER (add remove or change a field in a existing table)DELETE (remove rows from a table)---------------------------------------------------------------------------------------------#(Dates)Date Functions:YEAR : MONTH : DAYWhat bikes were ordered in Dec of1994?SELECT *FROM BicycleWhere Year(OrderDate) = 1994 AND MONTH(OrderDate) = 12;Provide a listing of racing---------------------------------------------------------------------------------------------#(BETWEEN)SELECT *FROM BicycleWHERE SalePrice > 1000 AND SalePrice < 2000;SELECT *FROM BicycleWHERE SalePrice BETWEEN 1000 AND 2000______________SELECT *FROM PartWHERE Onhand BETWEEN 10 and 30;---------------------------------------------------------------------------------------------#(IN)SELECT *FROM PartWHERE Warehouse IN("1","2");----------------------------------------------------------------------------------------------#10SELECT SerialNumber, SalePrice + SalesTax + ShipPrice AS TotalPriceFROM Bicycle;Then Ordered the Data to most to least expensiveSELECT SerialNumber, ModelType, SalePrice + SalesTax + ShipPrice AS TotalPriceFROM Bicycle;ORDER BY SalePrice+SaleTax + ShipPrice DESC, SerialNumberLimited Display to only show the TOP 10 Results----------------------------------------------------------------------------------------------#11 Show part table and sort by descriptionSELECT *FROM PartORDER BY Description;----------------------------------------------------------------------------------------------WILDCARDS and how to use them? * is full wildcard (anything after)? is single wildcard (single character)Look for The"T*" = (That, The, Then, Tie, Tim, Toast, Top)"T??" = (The, Tie, Tim, Top)"T?e" = (The, Tie)SELECT *FROM PartWHERE PartNum Like "k*";___________SELECT *FROM PartWHERE PartNum Like "k?6*";---------------------------------------------------------------------------------------------------2/11/14 Aggragate functions (MIN,Max,AVG,SUM,COUNT)Built in functions for SQL#8SELECT COUNT(*) AS OrderCount, OrderDateFROM ordersWHERE OrderDate = #10/20/2013#GROUP BY OrderDate#9SELECT Sum(Balance) AS SumOfBalanceFROM CustomerGROUP BY RepNumHAVING RepNum ="35";---Whats the average selling price of all of the bikesSELECT Avg(SalePrice) AS AverageSalePriceFROM Bicycle;---What bikes have sole more than the average price?SELECT *FROM BicycleWHERE SalePrice > (SELECT Avg(SalePrice) AS AverageSalePrice FROM Bicycle)ORDER BY SalePrice; ---What is average salary for each type of emplyee?SELECT AVG(Salary) AS [Average Salary], TitleFrom EmployeeGROUP BY titleORDER BY AVG(Salary);---What is average salary for each type of employee, Excluding the chief executive officer SELECT AVG(Salary) AS [Average Salary], TitleFROM EmployeeWHERE Title "Chief Executive Officer"GROUP BY titleORDER BY AVG(Salary);---which employees are not assigned to a work area?SELECT *FROM EmployeeWHERE WorkArea IS NULL---what is the average salary of each type of employee excluding cheif executive office and for those types that average under 30,000SELECT AVG(Salary), TitleFROM EmployeeWHERE Title "Chief Executive Officer"GROUP BY TitleHAVING AVG(Salary) < 30000ORDER BY AVG(Salary);---What manufacture is owed the most money?SELECT *FROM ManufacturerWHERE BalanceDue = ( SELECT MAX(BalanceDue) FROM Manufacturer);ORSELECT TOP1, *FROM ManufacturerORDER BY BalanceDue DESC------(TABLE LINKING WITH 3 TABLES)---------FROM REP, Customer, OrdersWHERE Rep.RepNUM = Customer.RepNum AND Customer. CustomerNum = Orders.CustomerNumORFROM (Rep INNER JOIN Customer ON Rep.RepNum = Customer.RepNum) INNER JOIN Orders ON Customer.CustomerNum = Orders.CostomerNum-----------------------------------------------------------------------------------------------------------Joining tables more examplesSELECT Orders.*, CustomerNameFROM Orders, CustomerWHERE Customer.CustomerNum = Orders.CustomerNum;---------- (3 Ways to do the table linking)-----------------SELECT CustomerNum , CustomerNameFROM Rep, CustomerWHERE LastName.Rep = "Perez" and FirstName = "Juan" AND Rep.RepNum = Customer.RepNumSELECT CustomerNum , CustomerNameFROM Rep INNER JOIN Customer ON Rep.RepNum = Customer.RepNumWHERE LastName.Rep = "Perez" AND FirstName = "Juan"; SELECT CustomerNum , CustomerNameFROM CustomerWHERE RepNum IN (SELECT RepNum FROM Rep WHERE LastName.Rep = "Perez" AND FirstName = "Juan");--------------------------------------------------------------------------------------------------------------------2/13/2014 --------------------What stores are in MN?SELECT StoreName, State, CityFROM RetailStore, CityWHERE State= "MN" AND RetailStore.CityID = City.CityID;-------What Stores have sold bikes in MN?SELECT StoreName, State, CityFROM RetailStore, CityWHERE State= "MN" AND RetailStore.CityID = City.CityID AND StoreID IN (SELECT StoreID FROM Bicycle);-------SELECT StoreName, State, CityFROM RetailStore, CityWHERE State= "MN" AND RetailStore.CityID = City.CityID AND StoreID NOT IN (SELECT StoreID FROM Bicycle);-------What Bikes has Sixto Ballen ??????? ?? ?? ?SELECT SerialNumberFROM Bicycle, EmployeeWhere FirstName = "Sixto" AND LastName = "Ballen" AND Bicycle.EmployeeID = Employee.EmployeeID;--------Who bought the bikes that Sixto Ballen ????? ?? ?? ? .... ??SELECT SerialNumber, C.FirstName, C.LastNameFROM Bicycle AS B, Employee AS E, Customer AS CWhere E.FirstName = "Sixto" AND E.LastName = "Ballen" AND B.EmployeeID = E.EmployeeID AND B.CustomerID = C.CustomerID;(ALIESES CAN BE USED IN TABLES TO MAKE IDENTIFING FIELDS EASIER) ^^^^--------How many Times are Each of the Paint Colors Used? SELECT Paint.PaintID, ColorName, Count(*) AS [# of Occurences]FROM Bicycle, PaintWHERE Paint.PaintID = Bicycle.PaintIDGROUP BY Paint.PaintID, ColorNameORDER BY Count(*) DESC;-----------Create TableCREATE TABLE Employee(EmployeeID COUNT PRIMARY KEY,LastName Text(15) NOT NULL,FirstName CHAR(15) NOT NULL,HireDate DATE NOT NULL,Salary CURRENCY--------------------------------(Number stuff for Field Formats)http://office.microsoft.com/en-us/access-help/format-property-number-and-currency-data-types-HA001232740.aspx-------------------------------Create a Table with a FOREIGN KEYCREATE TABLE Dependent(DepID COUNTER PRIMARY KEY,First Name Text(15),LastName Text(15),EmpID Number NOT NULL,CONSTRAINT efk FOREIGN KEY(EmpID) REFERENCES Employee(EmployeeID));--------------------------------2/27/2014================#3 SELECT ordernum, sum(numorder*quotedprice) FROM orderlineGROUP BY ordernumSaved query as ordtot--------#4CREATE INDEX PartIndex1ON OrderLine(PartNum);CREATE INDEX PartIndex2ON Part(Warehouse);CREATE INDEX PartIndex3ON Part(Warehouse,Class);#5 (Droping indexsDROP Index PartIndex3 ON Part;============================#7ALTER TABEL PARTADD Allocation numberUpdate PartSET Allocation =0;how many of KV29 are on order?SELECT SUM(NumbOrdered)FROM OrderlineWHERE Partnum= "KV29"UPDATE PartSET Allocation = 2WHERE PartNum = "KV29"#8ALTER TABLE PartALTER COLUM Warehouse Text(2);ALTER TABLE PartDROP COLUM Allocation;