Built in Functions Massaging the data
Functions SQL includes several built in functions They all operate with the same pattern SELECT FunctionName(Function arguments) FROM Tablename Function can be nested inside of each other SELECT MAX(SUM(Rate*Hours)
ALIASING You can give a return column generated by a function a name or Alias SELECT MAX(Rate) AS Highest SELECT MAX(Rate) “Highest”
Most Common Functions COUNT MAX MIN SUM AVG
Count COUNT counts the number of rows that meet a certain criteria. It can be used on textual as well as numeric data SELECT Count(*) FROM Orders SELECT COUNT(CarrierCode) as ALASKA FROM ItinerayDetail WHERE CarrierCode=‘ALA’
MAX MAX returns the largest value in a column. It can only be used with numeric fields and dates SELECT MAX(Price) FROM Inventory
MIN Returns the lowest value in a column (again numeric or date) SELECT MIN(RATE) FROM HourlyRate
SUM SUM Returns the total of numeric column SELECT SUM(points) FROM Assignment WHERE StudentID=‘980001111’
AVG AVG Returns the mean Average of a numeric column SELECT AVG(heartrate) FROM Excercise
Aggregate Functions All the functions that we have looked at are called aggregate functions That means they work on groups of rows or values, rather than just one row or one value
GroupBy If you include any columns in the SELECT of a query with an aggregate function you must GROUP BY those columns SELECT StudentID, SUM(points) FROM Assignments GROUP BY StudentID
Having Like the WHERE clause, but WHERE operates on individual records HAVING operates on Aggregrate (grouped) records SELECT EmployeeID, COUNT(OrderID) AS NumOrders FROM ORDERS GROUP BY EmployeeID HAVING COUNT(OrderID)>100
Example SubQuery SELECT Orderid, unitprice FROM SaleDetail WHERE Unitprice= (SELECT MAX (UnitPrice) FROM SaleDetail