Sql Server Statements and their Syntax


The below content contains Sql Server Statement and their syntax with sample code. Through this one can easily learn sql server and can query easily. This is mainly for the beginners to sql server. It explains many operator its and the syntax of the operator.

Some Sql Server Commands and their Syntax:


AND\OR Operator:

SYNTAX:

SELECT column_name(s) FROM table_name WHERE condition AND|OR condition

ALTER TABLE :

SYNTAX:

ALTER TABLE table_name ADD column_name datatype
or
ALTER TABLE table_name DROP COLUMN column_name

AS:

SYNTAX:

SELECT column_name AS column_alias FROM table_name
or
SELECT column_name FROM table_name AS table_alias

BETWEEN :

SYNTAX:

SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2

CREATE DATABASE:

SYNTAX:
 CREATE DATABASE database_name 

DROP DATABASE:

SYNTAX:

DROP DATABASE database_name

CREATE TABLE:

SYNTAX:

CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
...
)

CREATE INDEX:

SYNTAX:

CREATE INDEX index_name ON table_name (column_name)
or
CREATE UNIQUE INDEX index_name ON table_name (column_name)

DROP INDEX:

SYNTAX:

DROP INDEX table_name.index_name

CREATE VIEW:

SYNTAX:

CREATE VIEW view_name AS
SELECT column_name(s) FROM table_name WHERE condition

DELETE:

SYNTAX:

DELETE FROM table_name WHERE some_column=some_value
or
DELETE FROM table_name

GROUP BY:

SYNTAX:

SELECT column_name, aggregate_function(column_name)
FROM table_name WHERE column_name operator value
GROUP BY column_name


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

IN:

SYNTAX:

SELECT column_name(s) FROM table_name
WHERE column_name IN (value1,value2,..)

INSERT INTO:

SYNTAX:

INSERT INTO table_name
VALUES (value1, value2, value3,....)
or
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,....)

AS:

SYNTAX:


INNER JOIN:

SYNTAX:

SELECT column_name(s)
FROM table_name1 INNER
JOIN table_name2 ON table_name1.column_name=table_name2.column_name

LEFT JOIN:

SYNTAX:

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name

RIGHT JOIN:

SYNTAX:

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

FULL JOIN:

SYNTAX:

SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name

LIKE:

SYNTAX:

SELECT column_name(s)
FROM table_name WHERE column_name LIKE pattern

ORDER BY:

SYNTAX:

SELECT column_name(s) FROM table_name
ORDER BY column_name [ASC|DESC]

SELECT:

SYNTAX:

SELECT column_name(s) FROM table_name

SELECT * :

SYNTAX:
 SELECT * FROM table_name 

SELECT DISTINCT:

SYNTAX:

SELECT DISTINCT column_name(s) FROM table_name

SELECT INTO:

SYNTAX:

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:

SYNTAX:

SELECT TOP number column_name(s) FROM table_name

TRUNCATE TABLE:

SYNTAX:
TRUNCATE TABLE table_name 

UNION:

SYNTAX:

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

UNION ALL:

SYNTAX:

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

UPDATE:

SYNTAX:

UPDATE table_name SET column1=value, column2=value,...
WHERE some_column=some_value

WHERE:

SYNTAX:

SELECT column_name(s) FROM table_name
WHERE column_name operator value


Comments

Author: ghjjfgnj22 Apr 2011 Member Level: Silver   Points : 1

reply to Sql Server Statements and their Syntax"



DDL statements to:
Create Database
Create tables
Alter table definitions
Rename, Truncate, and drop tables
DML statements to:
Insert rows into a table
Update rows in a table
Delete rows from a table
TCL statements to:
COMMIT



CREATE TABLE Product ( product_id VARCHAR(5),
product_name VARCHAR(10),
product_rate DECIMAL)





FOREIGN KEY constraints:


create table k (id int primary key, ename varchar(10))
insert into k values(12,'a')
insert into k values(13,'d')
insert into k values(14,'t')
insert into k values(15,'r')
insert into k values(16,'w')
select * from k

create table fk (fid int references k(id), fname varchar(20))
insert into fk values(12,'a')
insert into fk values(13,'d')
insert into fk values(14,'t')
insert into fk values(15,'r')
insert into fk values(16,'w')
insert into fk values(18,'r')
insert into fk values(19,'y')

select * from fk



Op of fk




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: