SQL Statement
Introduction
SQL stands for Structured Query Language.
SQL lets you access and manipulate database.
Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:
- An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
- To use a server-side scripting language, like PHP or ASP
- To use SQL to get the data you want
- To use HTML / CSS to style the page
DBMS
The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
A filed is a column in table that is designed to maintain specific information about every record in the table.
A record is a row that exists in the table.
Table
A database most often contain one or more tables. Tables contain records (rows) with data.
SQL Statements
SELECT- extracts data from a databaseUPDATE- updates data in a databaseDELETE- deletes data from a databaseINSERT INTO- inserts new data into a databaseCREATE DATABASE- creates a new databaseALTER DATABASE- modifies a databaseCREATE TABLE- creates a new tableALTER TABLE- modifies a tableDROP TABLE- deletes a tableCREATE INDEX- creates an index (search key)DROP INDEX- deletes an index
SELECT
The select allows you to select the filed(column) from the table. If you want to select all columns you should use select * from table_name
1 | SELECT column1, column2, ... |
select distinct statement can satisfy you to list distinct(different) values of the columns, which means it delete the repetitive values.
1 | SELECT DISTINCT column1, column2, ... |
WHERE
This statement add some conditions so that can extract the specify records you want.
1 | SELECT column1, column2, ... |
Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, etc.!
Text Fields vs. Numeric Fields
SQL requires single quotes around text values (most database systems will also allow double quotes).
However, numeric fields should not be enclosed in quotes:
1 | SELECT * FROM Customers |
Operators in The WHERE Clause
AND, OR, NOT
There can be contain more than one record by using the
and,or,notandIf the all the conditions separated by
andare turn, the result is true. That means the extracted records are display bywhereif they fulfill all conditions.1
WHERE condition1 AND condition2 AND condition3 ...;
orIf the any of the conditions separated by
orare turn, the result is true. That means the extracted records are display bywhereif they fulfill one of the conditions.1
WHERE condition1 OR condition2 OR condition3 ...;
notThe
NOToperator displays a record if the condition(s) is NOT TRUE.1
WHERE NOT condition;
The following operators can be used in the
WHEREclause:Operator Description = Equal > Greater than < Less than >= Greater than or equal <= Less than or equal <> Not equal. Note: In some versions of SQL this operator may be written as != BETWEEN Between a certain range LIKE Search for a pattern IN To specify multiple possible values for a column
LIKE
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character
Note: MS Access uses an asterisk (*) instead of the percent sign (%), and a question mark (?) instead of the underscore (_).
1 | SELECT column1, column2, ... |
Example
1 | SELECT * FROM Customers |
NOT LIKE
The following SQL statement selects all customers with a CustomerName that does NOT start with “a”:
1 | SELECT * FROM Customers |
IN
The IN operator is a shorthand for multiple OR conditions.
1 | SELECT column_name(s) |
NOT IN
1 | SELECT * FROM Customers |
BETWEEN
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
1 | SELECT column_name(s) |
NOT BETWEEN
1 | SELECT * FROM Products |
NULL
The IS NULL operator is used to test for empty values (NULL values).
1 | SELECT CustomerName, ContactName, Address |
The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
1 | SELECT CustomerName, ContactName, Address |
ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
ORDER BY Country DESC;
1 | SELECT column1, column2, ... |
- ORDER BY Several Columns
The following SQL statement selects all customers from the “Customers” table, sorted by the “Country” and the “CustomerName” column. This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName:
1 | SELECT * FROM Customers |
INSERT INTO
There are two ways you can insert new record into a table.
Specify both the column names and the values to be inserted:
1
2INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);If you add a whole record you needn’t specify the column name.
1
2INSERT INTO table_name
VALUES (value1, value2, value3, ...);
UPDATE
The UPDATE statement is used to modify the existing records in a table.
1 | UPDATE table_name |
Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!
DELETE
The DELETE statement is used to delete existing records in a table.
1 | DELETE FROM table_name WHERE condition; |
If you omit the WHERE clause, all records in the table will be deleted!
JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.




INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
1 | SELECT column_name(s) |
Let’s look at a selection from the “Orders“ table:
| OrderID | CustomerID | OrderDate |
|---|---|---|
| 10308 | 2 | 1996-09-18 |
| 10309 | 37 | 1996-09-19 |
| 10310 | 77 | 1996-09-20 |
Then, look at a selection from the “Customers“ table:
| CustomerID | CustomerName | ContactName | Country |
|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mexico |
Notice that the “CustomerID” column in the “Orders” table refers to the “CustomerID” in the “Customers” table. The relationship between the two tables above is the “CustomerID” column.
1 | SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate |
and it will produce something like this:
| OrderID | CustomerName | OrderDate |
|---|
LEFT JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2).
1 | SELECT column_name(s) |
RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1).
1 | SELECT column_name(s) |
FULL JOIN
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records.
1 | SELECT column_name(s) |
SELF JOIN
A self join is a regular join, but the table is joined with itself.
1 | SELECT column_name(s) |
GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows, like “find the number of customers in each country” and we group by the ‘country’. So the GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
1 | SELECT column_name(s) |
The following SQL statement lists the number of customers in each country:
1 | SELECT COUNT(CustomerID), Country |
Having
HAVING statement usually follows by the GROUP BY statement to show how many records in each group.
1 | SELECT column_name(s) |
The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:
1 | SELECT COUNT(CustomerID), Country |
Function
MIN()
Export the smallest value of the selected column.
1 | SELECT MIN(column_name) |
MAX()
Export the largest value of the selected column.
1 | SELECT MAX(column_name) |
COUNT()
The count function return the number of the rows of selected column.
1 | SELECT COUNT(column_name) |
AVG()
The AVG() function returns the average value of a numeric column.
1 | SELECT AVG(column_name) |
SUM()
The SUM() function returns the total sum of a numeric column.
1 | SELECT SUM(column_name) |
Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
An alias is created with the AS keyword.
Alias Column Syntax
1 | SELECT *column_name* AS *alias_name* |
Alias Table Syntax
1 | SELECT *column_name(s)* |
