sql query list

SQL Statement Syntax
AND / OR SELECT Column_Name
FROM Table_Name
WHERE condition
AND|OR condition
Example : SELECT * FROM mytable WHERE name1=’name1′ AND name2=’name2′ OR name3=’name3′
ALTER TABLE ALTER TABLE Table_Name
ADD Column_Name datatype
or
ALTER TABLE Table_Name
DROP COLUMN Column_Name
Example : ALTER TABLE `table1` ADD `name2` VARCHAR( 10 ) NOT NULL AFTER `name` ;
ALTER TABLE table1 DROP name2
AS (alias) SELECT Column_Name AS column_alias
FROM Table_Name
or
SELECT Column_Name
FROM Table_Name  AS table_alias
Example : SELECT t1.name, table2.name FROM `table1` AS t1 inner join table2 on t1.name=table2.name
BETWEEN SELECT Column_Name
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,

)
Example : CREATE TABLE `database`.`table22` (`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,`address` VARCHAR( 100 ) NOT NULL ,
PRIMARY KEY ( `id` ))
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
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
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
FROM Table_Name1
INNER JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
LEFT JOIN SELECT Column_Name
FROM Table_Name1
LEFT JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
RIGHT JOIN SELECT Column_Name
FROM Table_Name1
RIGHT JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
FULL JOIN SELECT Column_Name
FROM Table_Name1
FULL JOIN Table_Name2
ON Table_Name1.Column_Name=Table_Name2.Column_Name
LIKE SELECT Column_Name
FROM Table_Name
WHERE Column_Name LIKE pattern
ORDER BY SELECT Column_Name
FROM Table_Name
ORDER BY Column_Name [ASC|DESC]
SELECT SELECT Column_Name
FROM Table_Name
SELECT * SELECT *
FROM Table_Name
SELECT DISTINCT SELECT DISTINCT Column_Name
FROM Table_Name
SELECT INTO SELECT *
INTO new_Table_Name [IN externaldatabase]
FROM old_Table_Name
or
SELECT Column_Name
INTO new_Table_Name [IN externaldatabase]
FROM old_Table_Name
SELECT TOP SELECT TOP number|percent Column_Name
FROM Table_Name
TRUNCATE TABLE TRUNCATE TABLE Table_Name
UNION SELECT Column_Name FROM Table_Name1
UNION
SELECT Column_Name FROM Table_Name2
UNION ALL SELECT Column_Name FROM Table_Name1
UNION ALL
SELECT Column_Name FROM Table_Name2
UPDATE UPDATE Table_Name
SET column1=value, column2=value,…
WHERE some_column=some_value
WHERE SELECT Column_Name
FROM Table_Name
WHERE Column_Name operator value
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s