SQL Server

Posted on 14/08/2019

1. SQL Syntax

1.1. SELECT Statement

SELECT column1, column2....columnN
FROM   table_name;

1.2. DISTINCT Clause

SELECT DISTINCT column1, column2....columnN
FROM   table_name;

1.3. WHERE Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION;

1.4. AND/OR Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION-1 {AND|OR} CONDITION-2;

1.5. IN Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name IN (val-1, val-2,...val-N);

1.6. BETWEEN Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name BETWEEN val-1 AND val-2;

1.7. LIKE Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  column_name LIKE { PATTERN };

1.8. ORDER BY Clause

SELECT column1, column2....columnN
FROM   table_name
WHERE  CONDITION
ORDER BY column_name {ASC|DESC};

1.9. GROUP BY Clause

SELECT SUM(column_name)
FROM   table_name
WHERE  CONDITION
GROUP BY column_name;

1.10. COUNT Clause

SELECT COUNT(column_name)
FROM   table_name
WHERE  CONDITION;

1.11. HAVING Clause

SELECT SUM(column_name)
FROM   table_name
WHERE  CONDITION
GROUP BY column_name
HAVING (arithematic function condition);

1.12. CREATE TABLE Statement

CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

1.13. DROP TABLE Statement

DROP TABLE table_name;

1.14. CREATE INDEX Statement

CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...columnN);

1.15. DROP INDEX Statement

ALTER TABLE table_name
DROP INDEX index_name;

1.16. DESC Statement

DESC table_name;

1.17. TRUNCATE TABLE Statement

TRUNCATE TABLE table_name;

1.18. ALTER TABLE Statement

ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_ype};

1.19. ALTER TABLE Statement (Rename)

ALTER TABLE table_name RENAME TO new_table_name;

1.20. INSERT INTO Statement

INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);

1.21. UPDATE Statement

UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE  CONDITION ];

1.22. DELETE Statement

DELETE FROM table_name
WHERE  {CONDITION};

1.23. CREATE DATABASE Statement

CREATE DATABASE database_name;

1.24. DROP DATABASE Statement

DROP DATABASE database_name;

1.25. USE Statement

USE database_name;

1.26. COMMIT Statement

COMMIT;

1.27. ROLLBACK Statement

ROLLBACK;