SQL Is A Standard Language For Accessing Databases
SQL Is A Standard Language For Accessing Databases
Our SQL tutorial will teach you how to use SQL to access and manipulate data
in:
MySQL, SQL Server, Access, Oracle, Sybase, DB2, and other database systems.
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
However, to be compliant with the ANSI standard, they all support at least the major commands (such
as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
Note: Most of the SQL database programs also have their own proprietary extensions in addition to
the SQL standard!
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2,
Oracle, MySQL, and Microsoft Access.
A table is a collections of related data entries and it consists of columns and rows.
Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g.
"Customers" or "Orders"). Tables contain records (rows) with data.
The table above contains three records (one for each person) and five columns (P_Id, LastName,
FirstName, Address, and City).
SQL Statements
Most of the actions you need to perform on a database are done with SQL statements.
The following SQL statement will select all the records in the "Persons" table:
In this tutorial we will teach you all about the different SQL statements.
Semicolon is the standard way to separate each SQL statement in database systems that allow more
than one SQL statement to be executed in the same call to the server.
We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL
statement, but some database programs force you to use it.
The query and update commands form the DML part of SQL:
The DDL part of SQL permits database tables to be created or deleted. It also define indexes (keys),
specify links between tables, and impose constraints between tables. The most important DDL
statements in SQL are:
This chapter will explain the SELECT and the SELECT * statements.
and
Now we want to select the content of the columns named "LastName" and "FirstName" from
the table above.
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table.
Navigation in a Result-set
Most database software systems allow navigation in the result-set with programming
functions, like: Move-To-First-Record, Get-Record-Content, Move-To-Next-Record, etc.
Programming functions like these are not a part of this tutorial. To learn about accessing data
with function calls, please visit our ADO tutorial or our PHP tutorial.
The DISTINCT keyword can be used to return only distinct (different) values.
Now we want to select only the distinct values from the column named "City" from the table
above.
City
Sandnes
Stavanger
Now we want to select only the persons living in the city "Sandnes" from the table above.
This is correct:
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove
This is correct:
This is wrong:
Operator Description
= Equal
IN If you know the exact value you want to return for at least one of the columns
The AND & OR operators are used to filter records based on more than one
condition.
The AND & OR Operators
The AND operator displays a record if both the first condition and the second condition is true.
The OR operator displays a record if either the first condition or the second condition is true.
Now we want to select only the persons with the first name equal to "Tove" AND the last name equal
to "Svendson":
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to
"Ola":
Now we want to select only the persons with the last name equal to "Svendson" AND the first name
equal to "Tove" OR to "Ola":
If you want to sort the records in a descending order, you can use the DESC keyword.
ORDER BY Example
The "Persons" table:
P_Id LastName FirstName Address City
Now we want to select all the persons from the table above, however, we want to sort the persons by
their last name.
The first form doesn't specify the column names where the data will be inserted, only their values:
The second form specifies both the column names and the values to be inserted:
The following SQL statement will add a new row, but only add data in the "P_Id", "LastName" and the
"FirstName" columns:
5 Tjessem Jakob
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or
records that should be updated. If you omit the WHERE clause, all records will be updated!
5 Tjessem Jakob
Now we want to update the person "Tjessem, Jakob" in the "Persons" table.
We use the following SQL statement:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or
records that should be deleted. If you omit the WHERE clause, all records will be deleted!
Now we want to delete the person "Tjessem, Jakob" in the "Persons" table.
or
Note: Be very careful when deleting records. You cannot undo this statement!
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 than the average OrderPrice
value.
Customer
Hansen
Nilsen
Jensen
The COUNT() function returns the number of rows that matches a specified criteria.
The COUNT(column_name) function returns the number of values (NULL values will not be counted) of
the specified column:
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified
column:
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
3
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table.
FirstOrderPrice
1000
100
LargestOrderPrice
2000
The MIN() Function
The MIN() function returns the smallest value of the selected column.
SmallestOrderPrice
100
The SUM() Function
The SUM() function returns the total sum of a numeric column.
OrderTotal
5700
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.
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
Now we want to select the content of the "LastName" and "FirstName" columns above, and convert
the "LastName" column to uppercase.
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
Now we want to extract the first four characters of the "City" column above.
SmallCity
Sand
Sand
Stav
Now we want to select the length of the values in the "Address" column above.
LengthOfAddress
12
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
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
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
SQL Summary
This SQL tutorial has taught you the standard computer language for accessing and manipulating
database systems.
You have learned how to execute queries, retrieve data, insert new records, delete records and update
records in a database with SQL.
You have also learned how to create databases, tables, and indexes with SQL, and how to drop them.
You have learned the most important aggregate functions in SQL.
You now know that SQL is the standard language that works with all the well-known database systems
like MS SQL Server, IBM DB2, Oracle, MySQL, and MS Access.