The SQL UNION Operator
The UNION operator is used to combine the result-set of two or
more SELECT statements.
Notice that each SELECT statement within the UNION must have the
same number of columns. The columns must also have similar data types. Also,
the columns in each SELECT statement must be in the same order.
SQL
UNION Syntax
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
Note: The UNION
operator selects only distinct values by default. To allow duplicate values,
use UNION ALL.
SQL
UNION ALL Syntax
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
PS: The
column names in the result-set of a UNION are always equal to the column
names in the first SELECT statement in the UNION.
|
Introduction
Stored procedures are special objects
available in sql server. Its a precompiled statements where all the
preliminary parsing operations are performed and the statements are ready for
execution.
Its very fast when compared to ordinary sql
statements where the sql statements will undergone a sequence of steps to
fetch the data
Stored procedure involves various syntax
based on the parameters passed. Let me show you a generic syntax for a stored
procedure.
Code:
Create procedure procName
as
begin
----Your query should be return here
end
CREATE PROCEDURE SPWITHPARAMETER_AND_RETURN_VALUE
(
@EMPID INT,
@IDVAL INT,
@RETURNVALUE INT =0 OUT
)
AS
DECLARE @COUNT INT
BEGIN
SELECT @COUNT=COUNT(*) FROM JOINTABLE WHERE EMPID=@EMPID AND IDVAL=@IDVAL
IF(@COUNT >0)
BEGIN
SET @RETURNVALUE = 1;
PRINT @RETURNVALUE
RETURN @RETURNVALUE
END
ELSE
BEGIN
SET @RETURNVALUE = 1;
PRINT @RETURNVALUE
RETURN @RETURNVALUE
END
END
|
A view contains rows and columns, just like a real table. The
fields in a view are fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view
and present the data as if the data were coming from one single table.
SQL
CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Note: A view
always shows up-to-date data! The database engine recreates the data, using
the view's SQL statement, every time a user queries a view.
SQL CREATE VIEW Examples
If you have the Northwind database you can see that it has
several views installed by default.
The view "Current Product List" lists all active
products (products that are not discontinued) from the "Products"
table. The view is created with the following SQL:
CREATE VIEW [Current Product List] AS
SELECT ProductID,ProductName
FROM Products
WHERE Discontinued=No
|
How to Add Column in Existing Table
ALTER TABLE supplier
MODIFY supplier_name varchar2(100)
not null;
This will modify the column called supplier_name to be a data type of varchar2(100)
and force the column to not allow null values.
|
SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all rows from the left table
(table_name1), even if there are no matches in the right table (table_name2).
SQL
LEFT JOIN Syntax
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
|
SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all the rows from the right table
(table_name2), even if there are no matches in the left table (table_name1).
SQL
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
|
SQL FULL JOIN Keyword
The FULL JOIN keyword return rows when there is a match in one
of the tables.
SQL
FULL JOIN Syntax
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
|
The SQL SELECT INTO Statement
The SELECT INTO statement selects data from one table and
inserts it into a different table.
The SELECT INTO statement is most often used to create backup
copies of tables.
SQL
SELECT INTO Syntax
We can select all columns into the new table:
SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_tablename
Or we can select only the columns we want into the new table:
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_tablename
SQL SELECT INTO Example
Make a Backup Copy - Now
we want to make an exact copy of the data in our "Persons" table.
We use the following SQL statement:
SELECT *
INTO Persons_Backup
FROM Persons
We can also use the IN clause to copy the table into another
database:
SELECT *
INTO Persons_Backup IN 'Backup.mdb'
FROM Persons
We can also copy only a few fields into the new table:
SELECT LastName,FirstName
INTO Persons_Backup
FROM Persons
SQL SELECT INTO - With a WHERE Clause
We can also add a WHERE clause.
The following SQL statement creates a "Persons_Backup"
table with only the persons who lives in the city "Sandnes":
SELECT LastName,Firstname
INTO Persons_Backup
FROM Persons
WHERE City='Sandnes'
SQL SELECT INTO - Joined Tables
Selecting data from more than one table is also possible.
The following example creates a "Persons_Order_Backup"
table contains data from the two tables "Persons" and
"Orders":
SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
|
SQL
Constraints
Constraints are used to limit the type of data that can go into
a table.
Constraints can be specified when a table is created (with the
CREATE TABLE statement) or after the table is created (with the ALTER TABLE
statement).
We will focus on the following constraints:
- NOT NULL
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
- CHECK
- DEFAULT
The next chapters will describe each constraint in detail.
|
SQL NOT
NULL Constraint
The NOT NULL constraint enforces a column to NOT accept NULL
values.
The NOT NULL constraint enforces a field to always contain a
value. This means that you cannot insert a new record, or update a record
without adding a value to this field.
The following SQL enforces the "P_Id" column and the
"LastName" column to not accept NULL values:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
|
SQL
UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a
database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee
for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint
defined on it.
Note that you can have many UNIQUE constraints per table, but
only one PRIMARY KEY constraint per table.
SQL
UNIQUE Constraint on CREATE TABLE
The following SQL creates a UNIQUE constraint on the
"P_Id" column when the "Persons" table is created:
MySQL:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (P_Id)
)
|
SQL
PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a
database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have
only ONE primary key.
SQL PRIMARY
KEY Constraint on CREATE TABLE
The following SQL creates a PRIMARY KEY on the "P_Id"
column when the "Persons" table is created:
MySQL:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
|
|
SQL FOREIGN KEY Constraint
A FOREIGN KEY in one table points to a PRIMARY KEY in another
table.
Let's illustrate the foreign key with an example. Look at the
following two tables:
The "Persons" table:
SQL
FOREIGN KEY Constraint on CREATE TABLE
The following SQL creates a FOREIGN KEY on the "P_Id"
column when the "Orders" table is created:
MySQL:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)
SQL Server / Oracle / MS Access:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)
To allow naming of a FOREIGN KEY constraint, and for defining a
FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
|
The DROP INDEX Statement
The DROP INDEX statement is used to delete an index in a table.
DROP
INDEX Syntax for MS Access:
DROP INDEX index_name ON table_name
DROP
INDEX Syntax for MS SQL Server:
DROP INDEX table_name.index_name
DROP
INDEX Syntax for DB2/Oracle:
DROP INDEX index_name
DROP
INDEX Syntax for MySQL:
ALTER TABLE table_name DROP INDEX index_name
The DROP TABLE Statement
The DROP TABLE statement is used to delete a table.
DROP TABLE table_name
The DROP DATABASE Statement
The DROP DATABASE statement is used to delete a database.
DROP DATABASE database_name
The TRUNCATE TABLE Statement
What if we only want to delete the data inside the table, and
not the table itself?
Then, use the TRUNCATE TABLE statement:
TRUNCATE TABLE table_name
|
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table.
SQL
ALTER TABLE Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in a table, use the following syntax (notice
that some database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
To change the data type of a column in a table, use the
following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
|
SQL
Dates
The most difficult part when working with dates is to be sure
that the format of the date you are trying to insert, matches the format of
the date column in the database.
As long as your data contains only the date portion, your
queries will work as expected. However, if a time portion is involved, it
gets complicated.
Before talking about the complications of querying for dates, we
will look at the most important built-in functions for working with dates.
MySQL
Date Functions
The following table lists the most important built-in date
functions in MySQL:
Function
|
Description
|
|
Returns the current date and time
|
|
Returns the current date
|
|
Returns the current time
|
|
Extracts the date part of a date or date/time expression
|
|
Returns a single part of a date/time
|
|
Adds a specified time interval to a date
|
|
Subtracts a specified time interval from a date
|
|
Returns the number of days between two dates
|
|
Displays date/time data in different formats
|
SQL
Server Date Functions
The following table lists the most important built-in date
functions in SQL Server:
Function
|
Description
|
|
Returns the current date and time
|
|
Returns a single part of a date/time
|
|
Adds or subtracts a specified time interval from a date
|
|
Returns the time between two dates
|
|
Displays date/time data in different formats
|
SQL
Date Data Types
MySQL comes
with the following data types for storing a date or a date/time value in the
database:
- DATE - format YYYY-MM-DD
- DATETIME -
format: YYYY-MM-DD HH:MM:SS
- TIMESTAMP -
format: YYYY-MM-DD HH:MM:SS
- YEAR - format
YYYY or YY
SQL Server comes
with the following data types for storing a date or a date/time value in the
database:
- DATE - format
YYYY-MM-DD
- DATETIME - format:
YYYY-MM-DD HH:MM:SS
- SMALLDATETIME
- format: YYYY-MM-DD HH:MM:SS
- TIMESTAMP -
format: a unique number
Note: The
date types are chosen for a column when you create a new table in your
database!
|
The BETWEEN Operator
The BETWEEN operator selects a range of data between two values.
The values can be numbers, text, or dates.
SQL
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
|
The SQL SELECT DISTINCT Statement
In a table, some of the columns may contain duplicate values.
This is not a problem, however, sometimes you will want to list only the
different (distinct) values in a table.
The DISTINCT keyword can be used to return only distinct
(different) values.
SQL
SELECT DISTINCT Syntax
SELECT DISTINCT column_name(s)
FROM table_name
|
The WHERE Clause
The WHERE clause is used to extract only those records that
fulfill a specified criterion.
SQL
WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
his is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove
|
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SQL
SELECT Syntax
SELECT column_name(s)
FROM table_name
and
SELECT * FROM table_name
|
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.
SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'
SELECT * FROM Persons WHERE
LastName='Svendson'
AND (FirstName='Tove' OR FirstName='Ola')
|
The ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by a
specified column.
The ORDER BY keyword sort the records in ascending order by
default.
If you want to sort the records in a descending order, you can
use the DESC keyword.
SQL
ORDER BY Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
SELECT * FROM Persons
ORDER BY LastName DESC
|
The UPDATE Statement
The UPDATE statement is used to update existing records in a
table.
SQL
UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
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!
|
The DELETE Statement
The DELETE statement is used to delete rows in a table.
SQL
DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value
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!
DELETE FROM table_name
or
DELETE * FROM table_name
DELETE FROM Persons
WHERE LastName='Tjessem' AND FirstName='Jakob'
|
The BETWEEN Operator
The BETWEEN operator selects a range of data between two values.
The values can be numbers, text, or dates.
SQL
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
|
SQL Alias
You can give a table or a column another name by using an alias.
This can be a good thing to do if you have very long or complex table names
or column names.
An alias name could be anything, but usually it is short.
SQL
Alias Syntax for Tables
SELECT column_name(s)
FROM table_name
AS alias_name
SQL
Alias Syntax for Columns
SELECT column_name AS alias_name
FROM table_name
Alias Example
Assume we have a table called "Persons" and another
table called "Product_Orders". We will give the table aliases of
"p" and "po" respectively.
Now we want to list all the orders that "Ola Hansen"
is responsible for.
We use the following SELECT statement:
SELECT po.OrderID, p.LastName, p.FirstName
FROM Persons AS p,
Product_Orders AS po
WHERE p.LastName='Hansen' AND p.FirstName='Ola'
The same SELECT statement without aliases:
SELECT Product_Orders.OrderID, Persons.LastName, Persons.FirstName
FROM Persons,
Product_Orders
WHERE Persons.LastName='Hansen' AND Persons.FirstName='Ola'
As you'll see from the two SELECT statements above; aliases can
make queries easier to both write and to read.
|
SQL
Aggregate Functions
SQL aggregate functions return a single value, calculated from
values in a column.
Useful aggregate functions:
- AVG() -
Returns the average value
- COUNT() -
Returns the number of rows
- FIRST() -
Returns the first value
- LAST() -
Returns the last value
- MAX() -
Returns the largest value
- MIN() -
Returns the smallest value
- SUM() -
Returns the sum
SQL
Scalar functions
SQL scalar functions return a single value, based on the input
value.
Useful scalar functions:
- UCASE() -
Converts a field to upper case
- LCASE() -
Converts a field to lower case
- MID() -
Extract characters from a text field
- LEN() -
Returns the length of a text field
- ROUND() -
Rounds a numeric field to the number of decimals specified
- NOW() -
Returns the current system date and time
- FORMAT() -
Formats how a field is to be displayed
Tip: The
aggregate functions and the scalar functions will be explained in details in
the next chapters.
|
The AVG() Function
The AVG() function returns the average value of a numeric
column.
SQL
AVG() Syntax
SELECT AVG(column_name) FROM table_name
SELECT AVG(OrderPrice) AS OrderAverage FROM Orders
|
SQL
COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of values
(NULL values will not be counted) of the specified column:
SELECT COUNT(column_name) FROM table_name
SQL
COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name
SQL
COUNT(DISTINCT column_name) Syntax
The COUNT(DISTINCT column_name) function returns the number of
distinct values of the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name
Note: COUNT(DISTINCT)
works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.
|
The FIRST() Function
The FIRST() function returns the first value of the selected
column.
SQL
FIRST() Syntax
SELECT FIRST(column_name) FROM table_name
SELECT
FIRST(OrderPrice) AS FirstOrderPrice FROM Orders
The LAST() Function
The LAST() function returns the last value of the selected
column.
SQL
LAST() Syntax
SELECT LAST(column_name) FROM table_name
Tip: Workaround if FIRST() function is not supported:
SELECT OrderPrice FROM
Orders ORDER BY O_Id LIMIT 1
SELECT
LAST(OrderPrice) AS LastOrderPrice FROM Orders
Tip: Workaround
if LAST() function is not supported:
SELECT OrderPrice FROM
Orders ORDER BY O_Id DESC LIMIT 1
|
The MAX() Function
The MAX() function returns the largest value of the selected
column.
SQL
MAX() Syntax
SELECT MAX(column_name) FROM table_name
SELECT MAX(OrderPrice)
AS LargestOrderPrice FROM Orders
|
The MIN() Function
The MIN() function returns the smallest value of the selected
column.
SQL
MIN() Syntax
SELECT MIN(column_name) FROM table_name
SELECT MIN(OrderPrice) AS SmallestOrderPrice
FROM Orders
|
The SUM() Function
The SUM() function returns the total sum of a numeric column.
SQL
SUM() Syntax
SELECT SUM(column_name) FROM table_name
SELECT SUM(OrderPrice) AS OrderTotal FROM
Orders
|
The 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.
SQL
GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
SELECT
Customer,SUM(OrderPrice) FROM Orders
The result-set will look like this:
Customer
|
SUM(OrderPrice)
|
Hansen
|
5700
|
Nilsen
|
5700
|
Hansen
|
5700
|
Hansen
|
5700
|
Jensen
|
5700
|
Nilsen
|
5700
|
|
The HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.
SQL
HAVING Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
Now we want to find if the customers "Hansen" or
"Jensen" have a total order of more than 1500.
We add an ordinary WHERE clause to the SQL statement:
SELECT
Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
|
The UCASE() Function
The UCASE() function converts the value of a field to uppercase.
SQL
UCASE() Syntax
SELECT UCASE(column_name) FROM table_name
Syntax
for SQL Server
SELECT UPPER(column_name) FROM table_name
Now we want to select the content of the "LastName"
and "FirstName" columns above, and convert the "LastName"
column to uppercase.
We use the following SELECT statement:
SELECT UCASE(LastName)
as LastName,FirstName FROM Persons
The result-set will look like this:
LastName
|
FirstName
|
HANSEN
|
Ola
|
SVENDSON
|
Tove
|
PETTERSEN
|
Kari
|
|
The LCASE() Function
The LCASE() function converts the value of a field to lowercase.
SQL
LCASE() Syntax
SELECT LCASE(column_name) FROM table_name
Syntax
for SQL Server
SELECT LOWER(column_name) FROM table_name
Now we want to select the content of the "LastName"
and "FirstName" columns above, and convert the "LastName"
column to lowercase.
We use the following SELECT statement:
SELECT LCASE(LastName)
as LastName,FirstName FROM Persons
The result-set will look like this:
LastName
|
FirstName
|
hansen
|
Ola
|
svendson
|
Tove
|
pettersen
|
Kari
|
|
The MID() Function
The MID() function is used to extract characters from a text
field.
SQL
MID() Syntax
SELECT MID(column_name,start[,length]) FROM table_name
Parameter
|
Description
|
column_name
|
Required. The field to
extract characters from
|
start
|
Required. Specifies the
starting position (starts at 1)
|
length
|
Optional. The number of
characters to return. If omitted, the MID() function returns the rest of
the text
|
ELECT MID(City,1,4) as
SmallCity FROM Persons
The result-set will look like this:
|
The LEN() Function
The LEN() function returns the length of the value in a text field.
SQL
LEN() Syntax
SELECT LEN(column_name) FROM table_name
SELECT LEN(Address) as LengthOfAddress FROM
Persons
|
The ROUND() Function
The ROUND() function is used to round a numeric field to the
number of decimals specified.
SQL
ROUND() Syntax
SELECT ROUND(column_name,decimals) FROM table_name
Parameter
|
Description
|
column_name
|
Required. The field to
round.
|
decimals
|
Required. Specifies the
number of decimals to be returned.
|
SELECT ProductName, ROUND(UnitPrice,0) as
UnitPrice FROM Products
|
The NOW() Function
The NOW() function returns the current system date and time.
SQL
NOW() Syntax
SELECT NOW() FROM table_name
SELECT ProductName, UnitPrice, Now() as PerDate
FROM Products
|
The FORMAT() Function
The FORMAT() function is used to format how a field is to be
displayed.
SQL
FORMAT() Syntax
SELECT FORMAT(column_name,format) FROM table_name
SELECT ProductName, UnitPrice,
FORMAT(Now(),'YYYY-MM-DD') as PerDate
FROM Products
|
SQL Statement
|
Syntax
|
AND / OR
|
SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
|
ALTER TABLE
|
ALTER TABLE table_name
ADD column_name datatype
or
ALTER TABLE table_name
DROP COLUMN column_name
|
AS (alias)
|
SELECT column_name AS column_alias
FROM table_name
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
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
|
CREATE VIEW
|
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
|
DELETE
|
DELETE FROM table_name
WHERE some_column=some_value
or
DELETE FROM table_name
(Note: Deletes
the entire table!!)
DELETE * FROM table_name
(Note: Deletes
the entire table!!)
|
DROP DATABASE
|
DROP DATABASE database_name
|
DROP INDEX
|
DROP INDEX table_name.index_name (SQL Server)
DROP INDEX index_name ON table_name (MS Access)
DROP INDEX index_name (DB2/Oracle)
ALTER TABLE table_name
DROP INDEX index_name (MySQL)
|
DROP TABLE
|
DROP TABLE table_name
|
GROUP BY
|
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
|
HAVING
|
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
|
IN
|
SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
|
INSERT INTO
|
INSERT INTO table_name
VALUES (value1, value2, value3,....)
or
INSERT INTO table_name
(column1, column2, column3,...)
VALUES (value1, value2, value3,....)
|
INNER JOIN
|
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
|
LEFT JOIN
|
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
|
RIGHT JOIN
|
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
|
FULL JOIN
|
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
|
LIKE
|
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
|
ORDER BY
|
SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
|
SELECT
|
SELECT column_name(s)
FROM table_name
|
SELECT *
|
SELECT *
FROM table_name
|
SELECT DISTINCT
|
SELECT DISTINCT column_name(s)
FROM table_name
|
SELECT INTO
|
SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_table_name
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C#
|
|
|