0% found this document useful (0 votes)
225 views

SQL Commands - Part I: Nijin.V, Ssnce

The document describes 34 SQL commands for performing common operations on database tables. These include commands for creating, altering, dropping, truncating, renaming, inserting, deleting, updating, and selecting data from tables. Aggregate functions are also covered, as are logical operators, pattern matching, sorting, grouping, and commenting. The commands provide a basic overview of fundamental SQL functionality for managing relational database tables and their structure and contents.

Uploaded by

Nijin Vinod
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
225 views

SQL Commands - Part I: Nijin.V, Ssnce

The document describes 34 SQL commands for performing common operations on database tables. These include commands for creating, altering, dropping, truncating, renaming, inserting, deleting, updating, and selecting data from tables. Aggregate functions are also covered, as are logical operators, pattern matching, sorting, grouping, and commenting. The commands provide a basic overview of fundamental SQL functionality for managing relational database tables and their structure and contents.

Uploaded by

Nijin Vinod
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL COMMANDS –Part I 2011

1.To create a table


SQL > CREATE TABLE <Table_Name>(field1 datatype(width), field2
datatype(width),…… fieldN datatype(width));

2.To view all created tables


SQL > SELECT * FROM TAB;

3.To Desrcibe A table->To view Table Structure


SQL > DESC <Table_Name>;

4.To alter a table(add)->To add an additional column


SQL > ALTER TABLE <Table_Name> ADD <Field_Name> <Data_Type>(width);

5.To alter a table(add)->To add multiple columns


SQL > ALTER TABLE <Table_Name> ADD(<Field_Name_1> <Data_Type>(width),
(<Field_Name_2> <Data_Type>(width),…… (<Field_Name_N> <Data_Type>(width));

6.To alter a table(modify)->Modifies the properties of a column


SQL > ALTER TABLE <Table_Name> MODIFY <Field_Name> <Data_Type>(width);

7.To alter a table(modify)->To modify multiple columns


SQL > ALTER TABLE <Table_Name> MODIFY(<Field_Name_1> <Data_Type>(width),
(<Field_Name_2> <Data_Type>(width),…… (<Field_Name_N> <Data_Type>(width));

8.To drop a table


SQL > DROP TABLE <Table_Name>;

9.To Drop a Specified Column


SQL > ALTER TABLE <Table_Name> DROP COLUMN <Column_name>;

10.To Truncate a Table


SQL > TRUNCATE TABLE <Table_Name>;

11.To rename a Table


SQL > ALTER TABLE <Table_Name> RENAME TO <New_Table_Name>;

12.To rename a Column


SQL > ALTER TABLE <Table_Name> RENAME COLUMN <Old_Column_Name> TO
<New_Column_Name>;
(or)
SQL > SELECT <Old_Column_Name> AS <New_Column_Name> FROM <Table_Name>;

NIjin.V,SSNCE
SQL COMMANDS –Part I 2011

13.To insert values into a table


SQL > INSERT INTO <Table_Name>VALUES(&field1,&field2…..,&fieldN);
(or)
SQL > INSERT INTO <Table_Name> VALUES(data,’data’);

14.To delete a table


SQL > DELETE FROM <Table_Name> WHERE <field1> = <condition>;

15.To replace in a value in a table


SQL > SELECT REPLACE(<Column_name>,’<word_to_be_replaced>’,
’<new_word>’)FROM <Table_Name>;

16. To Update A Table


SQL > UPDATE <Table_Name> SET <field> = <some-data> WHERE <field2> =
<condition>;

17.To Display the contents of a Table


SQL > SELECT * FROM <Table_Name>;

18.To Display the certain fields in a Table


a)SQL > SELECT <field_name> FROM <Table_Name>;
b)SQL > SELECT <field_name> FROM <Table_Name> WHERE <field> = <condition>;
c) SQL > SELECT <field_name_1>,<field_name_2> FROM <Table_Name> WHERE
<field> = <condition>; ---Selecting Multiple columns
d)SQL > SELECT * FROM <Table_Name> WHERE <PrimaryKey_Field_name> =
<some_data>; ---Selecting A Particular Row

19.To Display the certain Unique fields in a Table->Removing Duplicates


SQL > SELECT DISTINCT <field_name> FROM <Table_Name> WHERE <field> =
<condition>;

20.Sorting(ascending)
SQL > SELECT <field_name> FROM <Table_Name> WHERE <field> = <condition>
ORDER BY <field> ASC;
(or)
SQL > SELECT <field_name> FROM <Table_Name> WHERE <field> = <condition>
ORDER BY <field>;

21. Sorting(Descending)
SQL > SELECT <field_name> FROM <Table_Name> WHERE <field> = <condition>
ORDER BY <field> DESC;
NIjin.V,SSNCE
SQL COMMANDS –Part I 2011
22.Use of Logical Operators->AND
SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> = <condition> AND
<field2> = <condition>;

23.Use of Logical Operators->OR


SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> = <condition> OR
<field2> = <condition>;

24.Use of Logical Operators->NOT


SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> <> <condition>;
(or)
SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> != <condition>;
(or)
SQL >SELECT <field_name> FROM <Table_Name> WHERE NOT(<field>=<condition>);

25.Use Of Comparison Operators


a)SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> > <condition>;
---Greater Than
b) SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> < <condition>;
---Lesser Than
c) SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> <=
<condition>; ---Lesser Than and Equal to
d) SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> >=
<condition>; ---Greater Than and Equal to

26.Use Of Between Operator


a)SQL > SELECT <field_name> FROM <Table_Name> WHERE <field> BETWEEN
<condition> AND <condition>;
b)SQL > SELECT <field_name> FROM <Table_Name> WHERE <field> NOT BETWEEN
<condition> AND <condition>; ---Not between operator

27.Use Of Group By
SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> = <condition>
GROUP BY <field2>;

28.Use of Having
SQL > SELECT <field_name> FROM <Table_Name> WHERE <field1> = <condition>
GROUP BY <field2> HAVING <field1> <some_operator> <condition>;

29.Pattern Search ->Like


a)SQL > SELECT <field> FROM <Table_Name> WHERE <field> LIKE ‘%s’;
---Selects specified field ending with ‘s’
b) SQL > SELECT <field> FROM <Table_Name> WHERE <field> LIKE ‘s%’;
---Selects specified field beginning with ‘s’
NIjin.V,SSNCE
SQL COMMANDS –Part I 2011
c)SQL > SELECT <field> FROM <Table_Name> WHERE <field> LIKE ‘%sun%’;
---Selects specified field having ‘sun’ as intermediate word
d)SQL > SELECT <field> FROM <Table_Name> WHERE <field> LIKE ‘_s%’;
---Selects specified field having ‘s’ as second character
e)SQL > SELECT <field> FROM <Table_Name> WHERE <field> LIKE ‘_ _ _ _ _’;
---Selects specified field with 5 characters
*Do not give space between underscores_

29.Use of IN
SQL > SELECT <field> FROM <Table_Name> WHERE
<field>IN(value1,value2…valueN);

30.Use of AS
a)SQL > SELECT <field1>,(2 * <field2> AS <field3>) FROM <Table_Name>;
---Here the contents of field2 are multiplied by 2 and then stored in field3.The contents of field2 are
not altered.Applicable for WHERE clause also.

b)SQL > SELECT <Old_Column_Name> AS <New_Column_Name> FROM <Table_Name>;


-----Rename a Column

31.To count the total Number of rows in a table


SQL > SELECT COUNT(*) FROM <Table_Name>;

32.Aggregate Functions
a) SQL > SELECT SUM(field) FROM <Table_Name>; --To calculate Sum
b) SQL > SELECT AVG(field) FROM <Table_Name>; --To calculate Avg
c) SQL > SELECT MAX(field) FROM <Table_Name>; --To calculate Max
d) SQL > SELECT MIN(field) FROM <Table_Name>; --To calculate Min
e) SQL > SELECT COUNT(field) FROM <Table_Name>; --To count

33.To Duplicate a Table


SQL > CREATE TABLE <Duplicate_Table_Name> AS SELECT * FROM <Table_Name>;

34.To Comment in SQL


SQL > SELECT * FROM <Table_Name> /*some comments*/;

---Continued in Part II

NIjin.V,SSNCE

You might also like