SQL Functions
SQL Functions
SQL aggregate functions return a single value, calculated from values in a column.
SQL scalar functions return a single value, based on the input value.
Tip: The aggregate functions and the scalar functions will be explained in details in the next chapters
OrderAverage
950
Now we want to find the customers that have an OrderPrice value higher then the average OrderPrice value.
Customer
Hansen
Nilsen
Jensen
The COUNT() function returns the number of rows that matches a specified criteria.
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.
The result of the SQL statement above will be 2, because the customer
Nilsen has made 2 orders in total:
CustomerNilsen
NumberOfOrders
NumberOfCustomers
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table.
The FIRST() function returns the first value of the selected column.
FirstOrderPrice
1000
The LAST() function returns the last value of the selected column.
LastOrderPrice
100
The MAX() function returns the largest value of the selected column.
LargestOrderPrice
2000
The MIN() function returns the smallest value of the selected column.
SmallestOrderPrice
100
SQL SUM() Function
OrderTotal
5700
SQL GROUP BY Statement
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one
or more columns.
Now we want to find the total sum (total order) of each customer.
Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
Customer SUM(OrderPrice)
Hansen 5700
Nilsen 5700
Hansen 5700
Hansen 5700
Jensen 5700
Nilsen 5700
Explanation of why the above SELECT statement cannot be used: The SELECT statement above has
two columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (that
is the total sum of the "OrderPrice" column), while "Customer" returns 6 values (one value for each row in
the "Orders" table). This will therefore not give us the correct result. However, you have seen that the
GROUP BY statement solves this problem.
We can also use the GROUP BY statement on more than one column, like
this:
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate
functions.
Now we want to find if any of the customers have a total order of less than 2000.
Customer SUM(OrderPrice)
Nilsen 1700
Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than 1500.
Customer SUM(OrderPrice)
Hansen 2000
Jensen 2000
LastName FirstName
HANSEN Ola
SVENDSON Tove
PETTERSEN Kari
Now we want to select the content of the "LastName" and "FirstName" columns above, and convert the
"LastName" column to lowercase.
LastName FirstName
hansen Ola
svendson Tove
pettersen Kari
Parameter Description
length Optional. The number of characters to return. If omitted, the MID() function returns
the rest of the text.
SmallCity
Sand
Sand
Stav
The LEN() function returns the length of the value in a text field.
Now we want to select the length of the values in the "Address" column above.
LengthOfAddress
12
The ROUND() function is used to round a numeric field to the number of decimals specified.
Parameter Description
Now we want to display the product name and the price rounded to the nearest integer.
ProductName UnitPrice
Jarlsberg 10
Mascarpone 33
Gorgonzola 16
The NOW() function returns the current system date and time.
Now we want to display the products and prices per today's date.
Parameter Description
Now we want to display the products and prices per today's date (with today's date displayed in the
following format "YYYY-MM-DD").
or
or
SELECT column_name
FROM table_name AS table_alias
BETWEEN SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
CREATE DATABASE CREATE DATABASE database_name
CREATE TABLE CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,
...
)
CREATE INDEX CREATE INDEX index_name
ON table_name (column_name)
or
or
or
or
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_table_name
SELECT TOP SELECT TOP number|percent column_name(s)
FROM table_name
TRUNCATE TABLE TRUNCATE TABLE table_name
UNION SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
UNION ALL SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
UPDATE UPDATE table_name
SET column1=value, column2=value,...
WHERE some_column=some_value
WHERE SELECT column_name(s)
FROM table_name
WHERE column_name operator value
Source : http://www.w3schools.com/sql/sql_quickref.asp
SQL Hosting
SQL Hosting
If you want your web site to be able to store and display data from a database, your web server should have
access to a database system that uses the SQL language.
If your web server will be hosted by an Internet Service Provider (ISP), you will have to look for SQL hosting
plans.
The most common SQL hosting databases are MySQL, MS SQL Server, and MS Access.
You can have SQL databases on both Windows and Linux/UNIX operating systems.
MS SQL Server
MySQL
To learn more about web hosting, please visit our Hosting tutorial.