AVG() - Returns the average value COUNT() - Returns the number of rows MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum
BY Rupal Chavda(Msc.I.T.)
Now we want to find the average value of the "OrderPrice" fields. We use the following SQL statement:
Orders
BY Rupal Chavda(Msc.I.T.)
Now we want to count the number of orders from "Customer Nilsen". We use the following SQL statement:
BY Rupal Chavda(Msc.I.T.)
AS
CustomerNilsen
FROM
Orders
The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total: CustomerNilsen 2
BY Rupal Chavda(Msc.I.T.)
Now we want to find the smallest value of the "OrderPrice" column. We use the following SQL statement:
BY Rupal Chavda(Msc.I.T.)
Now we want to find the largest value of the "OrderPrice" column. We use the following SQL statement:
BY Rupal Chavda(Msc.I.T.)
Now we want to find the sum of all "OrderPrice" fields". We use the following SQL statement:
BY Rupal Chavda(Msc.I.T.)
2)Scalar functions:
2.1 Numeric functions Sql abs() function
Description This SQL ABS() returns the absolute value of a number passed as argument. Syntax Select ABS(expression) from <tableName>; Example of Sql abs() function SELECT ABS(-17.36) FROM dual; o/p : 17.36
SQL ROUND() Example We have the following "Products" table: Prod_Id ProductName Unit 1 2 3 Jarlsberg Mascarpone Gorgonzola UnitPrice 1000 g 10.45 1000 g 32.56 1000 g 15.67
Now we want to display the product name and the price rounded to the nearest integer. We use the following SELECT statement: SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products The result-set will look like this: ProductName Jarlsberg Mascarpone Gorgonzola UnitPrice 10 33 16
BY Rupal Chavda(Msc.I.T.)
The SQL SQRT() returns the square root of given value in the argument. Syntax Select SQRT( expression ) from <tableName>; Example of Sql sqrt() function SELECT SQRT(36) FROM dual; o/p: 6
Description Returns the natural logarithm of the specified float expression. Syntax LOG ( Number ) Example of Sql log() function SELECT LOG(10) FROM dual; o/p: 2.30259
Now we want to select the content of the "LastName" and "FirstName" columns above, and convert the "LastName" column to lowercase. We use the following SELECT statement: SELECT LCASE(LastName) as LastName,FirstName FROM Persons;
BY Rupal Chavda(Msc.I.T.)
10
The result-set will look like this: LastName hansen svendson pettersen FirstName Ola Tove Kari
Now we want to select the content of the "LastName" and "FirstName" columns above, and convert the "LastName" column to uppercase. We use the following SELECT statement: SELECT UCASE(LastName) as LastName,FirstName FROM Persons The result-set will look like this: LastName HANSEN SVENDSON PETTERSEN FirstName Ola Tove Kari
Output
ab
BY Rupal Chavda(Msc.I.T.)
11
SQL RIGHT() Syntax RIGHT ( character_expression , integer_expression ); SQL RIGHT() Example SELECT RIGHT('abcdefg',2);
Output
fg
SQL MID() Example We have the following "Persons" table: P_Id LastName FirstName Address 1 2 3 Hansen Svendson Pettersen Ola Tove Kari Borgvn 23 Storgt 20 City Sandnes Stavanger Timoteivn 10 Sandnes
Now we want to extract the first four characters of the "City" column above. We use the following SELECT statement: SELECT MID(City,1,4) as SmallCity FROM Persons The result-set will look like this: SmallCity Sand Sand Stav
BY Rupal Chavda(Msc.I.T.)
12
2 3
Svendson Pettersen
Tove Kari
Borgvn 23 Storgt 20
Sandnes Stavanger
Now we want to select the length of the values in the "Address" column above. We use the following SELECT statement: SELECT LEN(Address) as LengthOfAddress FROM Persons The result-set will look like this: LengthOfAddress 12 9 9
Output
FullName Rupal Chavda Dhruti Mehta Sejal Chavda
BY Rupal Chavda(Msc.I.T.)
13
=>datepart Is the part of date to which an integer number is added. The following table lists all valid datepart arguments. User-defined variable equivalents are not valid. datepart Abbreviations year yy , yyyy quarter qq , q month mm , m dayofyear dy , y day dd , d week wk , ww weekday dw , w hour hh minute mi , n second ss , s millisecond ms microsecond mcs nanosecond ns =>number Is an expression that can be resolved to an int that is added to a datepart of date. User-defined variables are valid. If you specify a value with a decimal fraction, the fraction is truncated and not rounded. =>date Is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value. date can be an expression, column expression, user-defined variable, or string literal. If the expression is a string literal, it must resolve to a datetime. To avoid ambiguity, use fourdigit years. Example SELECT DateAdd("m",1,"01-28-2012"); Output 2/28/2012
BY Rupal Chavda(Msc.I.T.)
14
DateDiff() function
Returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate. Syntax DATEDIFF ( datepart , startdate , enddate ) =>datepart Is the part of startdate and enddate that specifies the type of boundary crossed. The following table lists all valid datepart arguments. User-defined variable equivalents are not valid. datepart Abbreviations year yy, yyyy quarter qq, q month mm, m dayofyear dy, y day dd, d week wk, ww hour hh minute mi, n second ss, s millisecond ms microsecond mcs nanosecond ns =>startdate Is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value. date can be an expression, column expression, user-defined variable or string literal. startdate is subtracted from enddate. To avoid ambiguity, use four-digit years =>enddate Same as startdate. Example SELECT DateDiff("m","01-28-2012","02-28-2012"); Output 1
BY Rupal Chavda(Msc.I.T.)
15
Day() function
Returns an integer representing the day (day of the month) of the specified date. Syntax DAY ( date ) Example SELECT Day("01-18-2012"); Output 18
Month() function
Returns an integer that represents the month of the specified date. Syntax Month( date ) Example SELECT Month("01-18-2012"); Output 1
Year() function
Returns an integer that represents the year of the specified date. Syntax Year( date ) Example SELECT Year("01-18-2012"); Output 2012
MonthName() function
Returns Name of the specified month Syntax MonthName( Month number ) Example SELECT MonthName(7); Output July
BY Rupal Chavda(Msc.I.T.)
16