You are on page 1of 4

BASIC SQL STATEMENTS

1. CREATE
Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
Example:
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20)
);
2. DESC
Syntax:
DESC tablename;
Example:
DESC Students;

3. INSERT
Syntax: Method 1 (Only values)
INSERT INTO table_name VALUES (value1, value2, value3,…);
table_name: name of the table.
value1, value2,.. : value of first column, second column,… for the new record

Example:
INSERT INTO Students VALUES (‘5′,’HARSH’,’DBS’);

Syntax: Method 2 (Columns and values both)


INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2,
value3,..);
table_name: name of the table.
column1: name of first column, second column …
value1, value2, value3 : value of first column, second column,… for the new record

Example:
INSERT INTO Students (ROLL_NO, NAME, SUBJECT) VALUES
(‘5′,’PRATIK’,’DBS’);

4. SELECT

Syntax:
SELECT column1,column2 FROM table_name
column1 , column2: names of the fields of the table
table_name: from where we want to fetch
Example:
SELECT ROLL_NO FROM Students;
Or
SELECT * FROM Students; (to select all)
5. ORDER BY
Syntax:
SELECT column1,column2 FROM table_name ORDER BY column1;
column1 , column2: names of the fields of the table
table_name: from where we want to fetch
Example:
SELECT * FROM Students ORDER BY ROLL_NO;
Displays the sorted table in increasing order according to roll number
SELECT * FROM Students ORDER BY ROLL_NO desc;
Displays the sorted table in decreasing order according to roll number

6. ALTER (ADD, DROP, MODIFY)

a. ADD
Syntax:
ALTER TABLE table_name ADD (Columnname_1 datatype, Columnname_2 datatype,

Columnname_n datatype);
Example:
ALTER TABLE Students ADD (AGE int(3),COUNTRY varchar(40));
b. DROP
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example:
ALTER TABLE Students DROP COLUMN AGE;
c. MODIFY
Syntax:
*Syntax may vary slightly in different databases.

Syntax(Oracle,MySQL,MariaDB):
ALTER TABLE table_name
MODIFY column_name column_type;

Syntax(SQL Server):
ALTER TABLE table_name
ALTER COLUMN column_name column_type;
Example:
ALTER TABLE Students MODIFY COUNTRY varchar(20);

7. DELETE
Syntax:
DELETE FROM table_name WHERE some_condition;

Example:
Method 1: Delete single record
DELETE FROM Students WHERE NAME = 'Ram';

Method 2: Delete multiple records


DELETE FROM Students WHERE Age = 20;

8. DROP
Syntax:
DROP TABLE tablename;
Example:
DROP TABLE Students

You might also like