RSS

SQL Quick Reference

17 Dec

 

SQL Statement Syntax
AND / OR

SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition

ALTER TABLE
ALTER TABLE table_nameADD column_name datatype

or

ALTER TABLE table_name

DROP COLUMN column_name

AS (alias) SELECT column_name AS column_aliasFROM table_nameorSELECT column_nameFROM table_name  AS table_alias
BETWEEN SELECT column_name(s)FROM table_nameWHERE column_nameBETWEEN 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_nameON table_name (column_name)orCREATE UNIQUE INDEX index_nameON table_name (column_name)
CREATE VIEW CREATE VIEW view_name ASSELECT column_name(s)FROM table_nameWHERE condition
DELETE DELETE FROM table_nameWHERE some_column=some_valueorDELETE 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_nameDROP INDEX index_name (MySQL)
DROP TABLE DROP TABLE table_name
EXISTS IF EXISTS (SELECT * FROM table_name WHERE id = ?)BEGIN–do what needs to be done if existsENDELSEBEGIN

–do what needs to be done if not

END

GROUP BY SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name
HAVING SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value
IN SELECT column_name(s)FROM table_nameWHERE column_nameIN (value1,value2,..)
INSERT INTO INSERT INTO table_nameVALUES (value1, value2, value3,….)orINSERT INTO table_name(column1, column2, column3,…)VALUES (value1, value2, value3,….)
INNER JOIN SELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name=table_name2.column_name
LEFT JOIN SELECT column_name(s)FROM table_name1LEFT JOIN table_name2ON table_name1.column_name=table_name2.column_name
RIGHT JOIN SELECT column_name(s)FROM table_name1RIGHT JOIN table_name2ON table_name1.column_name=table_name2.column_name
FULL JOIN SELECT column_name(s)FROM table_name1FULL JOIN table_name2ON table_name1.column_name=table_name2.column_name
LIKE SELECT column_name(s)FROM table_nameWHERE column_name LIKE pattern
ORDER BY SELECT column_name(s)FROM table_nameORDER 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_nameorSELECT 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_name1UNIONSELECT column_name(s) FROM table_name2
UNION ALL SELECT column_name(s) FROM table_name1UNION ALLSELECT column_name(s) FROM table_name2
UPDATE UPDATE table_nameSET column1=value, column2=value,…WHERE some_column=some_value
WHERE SELECT column_name(s)FROM table_nameWHERE column_name operator value

 

 

 

kevalshah.in
Advertisements
 

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

 
%d bloggers like this: