Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

100 SQL FORMULAS EACH STUDENT SHOULD KNOW

There are so many inbuilt SQL formulas that each student in programming should know. These formulas
are very important and as part of database assignment help, here is the list of these 100 formulas
divided into very unique categories:

Sql Aggregate Formulas


These formulas are used to calculate and return a single value from a given column in a table. Here they
are:
1. AVG()
This is the formula used to calculate the average value of a given set
2. COUNT()
You should use this formula to count the total number of rows in a given table in your data base.
3. FIRST()
The formula is designed to return the first value in a given column or row in a table
4. LAST()
This one is used to create and return the last value of a given row or a given column in a table
5. MAX()
The formula or function is used to return the largest value in a given set of tables, rows or columns
6. MIN()
This is the exact opposite of the MAX function and its job is to return the minimum value in a given set
of data
7. SUM()
The column is used to calculate the total sum of a set of values or entities in a column or a row.
http://www.w3schools.com/sql/sql_functions.asp

SQL Scalar Functions


The scalar functions are also unique and they are used to return a single value based on all the input
numbers or values keyed in. Here are some of the major scalar formulas:

8. UCASE()
The formula is used to convert given values into capital letters or upper case.
9. LCASE()
This is the exact opposite of the UCASE(), it simply converts given values into lower case or small letters.
10. MID()
The formula is extractive and is used to extract specific characters from a given field on the database.
11. LEN()
The formula is used to return the accurate length of the selected text field.
12. ROUND()
The formula creates an automatic round off of the numeric field to the number of the specified
decimals. It is one of the most important ones for people who want to learn how to create a SQL
database.
13. NOW()
You will use this formula to return the most current system date and time at that particular moment.
14. FORMAT()
The formula is used to format the display of a given field to the desired specifications you want.
http://www.w3schools.com/sql/sql_functions.asp

SQL Date Formulas


The date formulas are also important and they are used widely by any SQL database expert. Here are
some of the major date functions:
15. ADDDATE()
The formula is used to add date in a given field of the data base as quickly as possible.
16. CONVERT_TZ()
This is the formula you need to use to convert the time zone in the data base or in a given field.
17. DATE_ADD ()
The formula is used to add two dates in one location and its very easy to use indeed.

18. CURTIME ()
The formula is used to return the current data in the specified field of data base.
19. DATE_FORMAT
The formula is used to set the date format for a given field or table and there are many options you can
go for.
20. LOCALTIME()
The function is used to get the local time where the system is currently operating.
21. MAKEDATE()
The formula is used to create the exact data of the week, the month and the year at a given time.
22. TIME()
This formula is used to add time to a given field or data base.
23. TIMESTAMP()
This is used to return a specific date time expression of a given argument in a data base.
24. TIME_TO_SEC
The formula converts the time selected into seconds and returns the exact value.
25. TIMESTAMPADD()
The formula is used to add a unique interval to a date time expression.
26. WEEK()
The formula is used to track week numbers and will return the current week number in the database
27. WEEKOFYEAR()
The formula is used to return the most current week of the year at a given point in time.
28. WEEKDAY()
Returns the exact day of the week on which the user has requested.
29. YEAR()
Here the formula returns the exact year at a given point in time.
30. YEARWEEK()

Used to return the exact year and the exact week at a given point in time
http://www.tutorialspoint.com/sql/sql-date-functions.htm

Numeric Formulas
Well, Numeric Formulas might be a little complicated but they are very important especially for people
who are looking for SQL database help. Here are some of the major ones:
31. ABS (m)
The formula is used to return the absolute value of M where the letter represents a data type or a
number
32. MOD(m, n)
The formula is used to divide the remainder of M by N whereby M is the value and N the determined
divisor.
33. SIN(n)
Used to calculate the sine of N whereby N is an angle expressed in radians
34. ACOS(N)
The formula is used to calculate the cosine of N in the range of between 0 to and N is a range of
between -1 and +1.
35. ROUND (M [, N]
Rounds off M to the Nth decimal place where M is a value or a number
36. SQRT(n)
Used to calculate the positive square root of n where N is a value or a number already.
37. EXP(n)
The formula is used to calculate or express a number or a value to the power of N
38. TAN(n)
Used to calculate the Tan of N whereby N is an angle expressed in radians.
39. FLOOR(n)
Used to return the smallest integer that is greater than N or equal to the N at a given point
40. CEIL(n)

The exact opposite of the formula above, it is used to return the smallest integer greater than or equal
to N.
41. LN (n)
The formula is simple enough and it returns the most natural logarithm of N where N is an integer a
value or a number.
42. TRUNC(M [,n])
The formula is opposite to the one for rounding off and it simply truncates M to the Nth decimal point
set.
43. LOG(N2,N1)
The formula returns the Logarithm of N2 where N1 is the base of the whole calculation
44. ASIN(N)
The formula basically returns the arc sin of N whereby N is an angle expressed in radians.
45. TANH(n)
The formula is used to return the hyperbolic Tangent of n where N is an angle expressed in radians.
46. COSH(N)
The formula returns the hyperbolic cosine of N where N is an angle expressed in radians.
47. SINH(n)
The formula is used to return the hyperbolic sine of N where N is the angle expressed in unit radians.
48. SING(N)
The formula returns -1 if less than 0, returns 0 if n is equal to zero and 1 if N is greater than zero.
49. COS (n)
The formula is used to get the cosine of n whereby n is the exact angle expressed in unit radians.
50. ATAN(n)
The formula is used to return the arc tangent of n between the range of -/2 to + /2
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions001.htm

Character Functions
Well, there are also character functions in SQL that instead of retuning numerical values they return
character values. As a database expert, you must learn them and here are some of the major ones:
51. INITCAP (s)
The formula changes each letter of a given word in a sentence into a capital letter automatically
52. LOWER (s)
This is a character string formula that changes all letters in a given sentence into lower case
53. UPPER (s)
The formula transforms all letters in a given string or a specific sentence into upper case
54. CONCAT()
This is the concatenation formula in SQL and is commonly used by experts in this area
55. LPAD()
The formula is used to return the predefined string right justified and also padded to the left with a set
number of characters separating two strings
56. RPAD()
The exact opposite of RPAD and it is used to return a set string left justified and padded to the right with
a set number of characters in between.
57. LTRIM()
Returns a string with characters removed up to a predetermined level. It could be right to the start of
the sentence or somewhere in between.
58. REPLACE()
The formula is used to replace specific characters in a string by other specific ones in the same sentence.
59. SUBSTR()
Returns the characters in a string from a determined section to another section in a given sentence
60. LENGTH(s)
The formula is used to return the total number of characters in a given sentence or string
61. INSTR()

The formula determines the total occurrence of a given character in a sentence or a set or sentences or
strings.
62. NLSSORT()
The formula is used to return the total string of bytes used to sort a given set of characters.
63. TRANSLATE()
The formula is used to translate or return the total number of occurrences a given character has been
replaced in a string.
64. TREAT()
The formula is used to change a declared type of expression at a given point in time
65. SOUNDEX()
The formula is used to return the phonetic expression of a given character in a specified string.
66. CHR()
The formula is used to return the binary equivalent of a set character in a chosen field or string
67. TRIM()
The formula is used to trim characters in a sentence or a string using a predefined method
68. REGEXP_REPLACE ()
This is an extended function of the Replace formula and it allows you to search for regular patterns in a
string
69. ASCII()
The formula is used to return the decimal representation of a given character on the data base
70. SPACE()
The formula is used to return a string of total spaces in a sentence.
https://technet.microsoft.com/en-us/library/ms181984(v=sql.105).aspx

Advanced Aggregate or Group Formulas


Group functions are used to operate on a set of values and return a specific value once executed. Here
are some of the most advanced group functions in SQL

71. AVG(s1.s2)
The formula is used to calculate the average of a given set of numbers in an argument
72. BIT_AND()
The formula is an expression that returns Bitwise AND as a form of argument in a string
73. BIT_OR()
The same as the one above but here it returns the expression bitwise OR in an argument or string
74. BIT_XOR
Similar to the two above but the difference is that it returns the expression bitwise XOR in a given string
or set of values.
75. COUNT()
You can use this formula to count the total number of rows in a given database or table
76. COUNT(DISTINCT)
Counts the total number of values in a given set of string that are being worked on
77. GROUP_CONCAT()
The formula is used to return a specific concatenated string in a predetermined set of values
78. STD()
The formula is used to calculate the standard deviation in a given group of figures or values
79. STDDEV()
A common variation that is used to also calculate the standard deviation in a given set of values
80. STDDEV_POP()
The formula returns the standard deviation in a given predetermined set of values in a data base
81. STDDEV_SAMP()
The formula is used to calculate the standard deviation sample from a given set of data or population
82. VAR_POP()
The formula is used to calculate the standard variance in a given set of data of population
83. VAR_SAMP()

Here the formula calculates the sample variance of a given set of data of population.
84. VARIANCE()
Calculates the standard variance of a given population of a determined set of data in a database
85. CHECKSUM_AGG()
The formula is used to calculate the checksum value of a group but all null values are not included in the
list
86. COUNT_BIG()
The formula works just like the count function and it returns the total number of items in a group
87. GROUPING()
The formula is used to determine aggregated list and should check whether a set of data is aggregated
or not
88. MEDIAN()
Used to calculate the median in a given set of values in a given data base or table selected
89. GROUPID()
The formula is used to identify a unique key that identifies a predetermined group of values or
characters
90. SUM()
The formula is used to calculate the sum of all values in a set group of values or characters
91. CORR()
The formula is used to calculate the correlation coefficient in a set of values without including the Nulls
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html

Date Conversion Functions


These formulas are used to convert data formats to the desired style and they arent many. Well, here
are all of them:
92. TO_CHAR(d [, fmt])
The formula converts the date in a string format whereby d is used to represent the quoted date
93. TO_DATE (s [fmt])

Reverses the string date to a specific date value, the s stands for the string date that needs to be
changed
94. ROUND (d [,fmt])
The formula is used to round off dates as specified by the set format within the database
95. TRUNC (d [, fmt])
The formula truncates a date as selected in a given format. Its the exact opposite of the one above
96. LEAST(d1,.d2)
The formula is used to return the earliest date in a given set of dates grouped together in a table
97. GREATEST(d1,..d2)
The formula is used to return the latest date in a group of dates specified in a set.
98. DATEDIFF()
The formula is used to return the difference between two dates in a given set and the total number of
hours, days or months between them.
99. DATEPART()
The formula is used to return a subset of a small part of a given date in a table or database
100.

SYSDATE()

The formula is used to return the most current date and time at any chosen period.
http://www.cs.utexas.edu/~mitra/csFall2009/cs329/lectures/sqlFunc.html
There you go, these are the most common formulas in SQL today. dont forget to also explore sql
database help today.

You might also like