Professional Documents
Culture Documents
RDBMS is the basis for SQL, and for all modern database systems such as MS
SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
SELECT Syntax
Here, column1, column2, ... are the field names of the table you want to select
data from. If you want to select all the fields available in the table, use the
following syntax:
SQL SELECT
DISTINCT Statement
The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes
you only want to list the different (distinct) values.
The following SQL statement lists the number of different (distinct) customer
countries:
SQL WHERE Clause
The SQL WHERE Clause
The WHERE clause is used to filter records.
WHERE Syntax
SQL AND, OR and
NOT Operators
AND Syntax
OR Syntax
NOT Syntax
SQL ORDER BY Keyword
The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.
ORDER BY Syntax
SQL INSERT INTO Statement
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of
the values is in the same order as the columns in the table. Here, the INSERT
INTO syntax would be as follows:
SQL NULL Values
A field with a NULL value is a field with no value.
IS NULL Syntax
UPDATE Syntax
Example:
The following SQL statement will update the ContactName to "Juan" for all
records where country is "Mexico":
SQL DELETE Statement
The SQL DELETE Statement
The DELETE statement is used to delete existing records in a table.
DELETE Syntax
MySQL Syntax:
The following SQL statement shows the equivalent example for MySQL:
The following SQL statement shows the equivalent example for Oracle:
MIN() Syntax
MAX() Syntax
MIN() Example
The following SQL statement finds the price of the cheapest product:
MAX() Example
The following SQL statement finds the price of the most expensive product:
SQL COUNT(), AVG() and
SUM() Functions
The SQL COUNT(), AVG() and SUM()
Functions
The COUNT() function returns the number of rows that matches a specified
criterion.
COUNT() Syntax
AVG() Syntax
The percent sign and the underscore can also be used in combinations!
LIKE Syntax
The following SQL statement selects all customers with a CustomerName that
does NOT start with "a":
SQL Wildcards
SQL Wildcard Characters
A wildcard character is used to substitute one or more characters in a string.
Here are some examples showing different LIKE operators with '%' and '_'
wildcards:
SQL IN Operator
The SQL IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
IN Syntax
IN Operator Examples
The following SQL statement selects all customers that are located in
"Germany", "France" or "UK":
The following SQL statement selects all customers that are NOT located in
"Germany", "France" or "UK":
The following SQL statement selects all customers that are from the same
countries as the suppliers:
SQL BETWEEN Operator
The SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
BETWEEN Syntax
UNION Syntax
Note: The column names in the result-set are usually equal to the column
names in the first SELECT statement.
GROUP BY Syntax
The following SQL statement lists the number of customers in each country,
sorted high to low:
HAVING Syntax
The following SQL statement lists the number of customers in each country,
sorted high to low (Only include countries with more than 5 customers):
EXISTS Syntax
The following SQL statement returns TRUE and lists the suppliers with a product
price equal to 22:
SQL ANY and ALL Operators
The SQL ANY and ALL Operators
The ANY and ALL operators allow you to perform a comparison between a single
column value and a range of other values.
ANY means that the condition will be true if the operation is true for any of the
values in the range.
ANY Syntax
The following SQL statement lists the ProductName if it finds ANY records in the
OrderDetails table has Quantity larger than 99 (this will return TRUE because
the Quantity column has some values larger than 99):
The following SQL statement lists the ProductName if it finds ANY records in the
OrderDetails table has Quantity larger than 1000 (this will return FALSE
because the Quantity column has no values larger than 1000):
The following SQL statement lists the ProductName if ALL the records in the
OrderDetails table has Quantity equal to 10. This will of course return FALSE
because the Quantity column has many different values (not only the value of
10):
SQL SELECT INTO Statement
The SQL SELECT INTO Statement
The SELECT INTO statement copies data from one table into a new table.
The new table will be created with the column-names and types as defined in
the old table. You can create new column names using the AS clause.