0% found this document useful (0 votes)
15 views9 pages

SQL Syntax

Uploaded by

anubhadrinathh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views9 pages

SQL Syntax

Uploaded by

anubhadrinathh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

SQL SYNTAX

SQL Syntax
When you want to do some operations on the data in the database, then you must have to
write the query in the predefined syntax of SQL.

The syntax of the structured query language is a unique set of rules and guidelines, which
is not case-sensitive. Its Syntax is defined and maintained by the ISO and ANSI standards.

Following are some most important points about the SQL syntax which are to remember:

o You can write the keywords of SQL in both uppercase and lowercase, but writing
the SQL keywords in uppercase improves the readability of the SQL query.
o SQL statements or syntax are dependent on text lines. We can place a single
SQL statement on one or multiple text lines.
o You can perform most of the action in a database with SQL statements.
o SQL syntax depends on relational algebra and tuple relational calculus.

SQL Statement
SQL statements tell the database what operation you want to perform on the structured
data and what information you would like to access from the database.

Simple Example of SQL statement:

1. SELECT "column_name" FROM "table_name";


Each SQL statement begins with any of the SQL keywords and ends with the semicolon
(;). The semicolon is used in the SQL for separating the multiple Sql statements which are
going to execute in the same call. In this SQL tutorial, we will use the semicolon (;) at the
end of each SQL query or statement.

Most Important SQL Commands and Statements


1. Select Statement
2. Update Statement
3. Delete Statement
4. Create Table Statement
5. Alter Table Statement
6. Drop Table Statement
7. Create Database Statement
8. Drop Database Statement
9. Insert Into Statement
10. Truncate Table Statement
11. Describe Statement
12. Distinct Clause
13. Commit Statement
14. Rollback Statement
15. Create Index Statement
16. Drop Index Statement
17. Use Statement

Let's discuss each statement in short one by one with syntax and one example:

pg. 1
SQL SYNTAX

1. SELECT Statement
This SQL statement reads the data from the SQL database and shows it as the output to
the database user.

Syntax of SELECT Statement:

1. SELECT column_name1, column_name2, .…, column_nameN


2. [ FROM table_name ]
3. [ WHERE condition ]
4. [ ORDER BY order_column_name1 [ ASC | DESC ], .... ];
Example of SELECT Statement:

1. SELECT Emp_ID, First_Name, Last_Name, Salary, City


2. FROM Employee_details
3. WHERE Salary = 100000
4. ORDER BY Last_Name
This example shows the Emp_ID, First_Name, Last_Name, Salary, and City of
those employees from the Employee_details table whose Salary is 100000. The
output shows all the specified details according to the ascending alphabetical order
of Last_Name.

2. UPDATE Statement
This SQL statement changes or modifies the stored data in the SQL database.

Syntax of UPDATE Statement:

1. UPDATE table_name
2. SET column_name1 = new_value_1, column_name2 = new_value_2, ...., column_n
ameN = new_value_N
3. [ WHERE CONDITION ];
Example of UPDATE Statement:

1. UPDATE Employee_details
2. SET Salary = 100000
3. WHERE Emp_ID = 10;
This example changes the Salary of those employees of the Employee_details table
whose Emp_ID is 10 in the table.

pg. 2
SQL SYNTAX

3. DELETE Statement
This SQL statement deletes the stored data from the SQL database.

Syntax of DELETE Statement:

1. DELETE FROM table_name


2. [ WHERE CONDITION ];

Example of DELETE Statement:

1. DELETE FROM Employee_details


2. WHERE First_Name = 'Sumit';
This example deletes the record of those employees from the Employee_details table
whose First_Name is Sumit in the table.

4. CREATE TABLE Statement


This SQL statement creates the new table in the SQL database.

Syntax of CREATE TABLE Statement:

1. CREATE TABLE table_name


2. (
3. column_name1 data_type [column1 constraint(s)],
4. column_name2 data_type [column2 constraint(s)],
5. .....
6. .....,
7. column_nameN data_type [columnN constraint(s)],
8. PRIMARY KEY(one or more col)
9. );
Example of CREATE TABLE Statement:

1. CREATE TABLE Employee_details(


2. Emp_Id NUMBER(4) NOT NULL,
3. First_name VARCHAR(30),
4. Last_name VARCHAR(30),
5. Salary Money,
6. City VARCHAR(30),
7. PRIMARY KEY (Emp_Id)
8. );

pg. 3
SQL SYNTAX

This example creates the table Employee_details with five columns or fields in the SQL
database. The fields in the table are Emp_Id, First_Name, Last_Name,
Salary, and City. The Emp_Id column in the table acts as a primary key, which means
that the Emp_Id column cannot contain duplicate values and null values.

5. ALTER TABLE Statement


This SQL statement adds, deletes, and modifies the columns of the table in the SQL
database.

Syntax of ALTER TABLE Statement:

1. ALTER TABLE table_name ADD column_name datatype[(size)];


The above SQL alter statement adds the column with its datatype in the existing database
table.

2. ALTER TABLE table_name MODIFY column_name column_datatype[(si


ze)];
The above 'SQL alter statement' renames the old column name to the new column name
of the existing database table.

3. ALTER TABLE table_name DROP COLUMN column_name;


The above SQL alter statement deletes the column of the existing database table.

Example of ALTER TABLE Statement:

1. ALTER TABLE Employee_details


2. ADD Designation VARCHAR(18);
This example adds the new field whose name is Designation with size 18 in
the Employee_details table of the SQL database.

pg. 4
SQL SYNTAX

6. DROP TABLE Statement


This SQL statement deletes or removes the table and the structure, views, permissions,
and triggers associated with that table.

Syntax of DROP TABLE Statement:

1. DROP TABLE [ IF EXISTS ]


2. table_name1, table_name2, ……, table_nameN;
The above syntax of the drop statement deletes specified tables completely if they exist in
the database.

Example of DROP TABLE Statement:

1. DROP TABLE Employee_details;


This example drops the Employee_details table if it exists in the SQL database. This
removes the complete information if available in the table.

7. CREATE DATABASE Statement


This SQL statement creates the new database in the database management system.

Syntax of CREATE DATABASE Statement:

1. CREATE DATABASE database_name;


Example of CREATE DATABASE Statement:

1. CREATE DATABASE Company;


The above example creates the company database in the system

8. DROP DATABASE Statement


This SQL statement deletes the existing database with all the data tables and views from
the database management system.

Syntax of DROP DATABASE Statement:

1. DROP DATABASE database_name;


Example of DROP DATABASE Statement:

1. DROP DATABASE Company;


The above example deletes the company database from the system.

pg. 5
SQL SYNTAX

9. INSERT INTO Statement


This SQL statement inserts the data or records in the existing table of the SQL database.
This statement can easily insert single and multiple records in a single query statement.

Syntax of insert a single record:

1. INSERT INTO table_name VALUES (value_1, value_2, ..…, value_N );


Example of insert a single record:

1. INSERT INTO Employee_details VALUES (101, Akhil, Sharma, 40000, Bangalo


re);

This example inserts 101 in the first column, Akhil in the second column, Sharma in the
third column, 40000 in the fourth column, and Bangalore in the last column of the
table Employee_details.

Syntax of inserting a multiple records in a single query:

1. INSERT INTO table_name


2. ( column_name1, column_name2, .…, column_nameN)
3. VALUES (value_1, value_2, ..…, value_N), (value_1, value_2, ..…, value_N),….;
Example of inserting multiple records in a single query:

1. INSERT INTO Employee_details


2. ( Emp_ID, First_name, Last_name, Salary, City )
3. VALUES (101, Amit, Gupta, 50000, Mumbai), (101, John, Aggarwal, 45000, Calcut
ta), (101, Sidhu, Arora, 55000, Mumbai);
This example inserts the records of three employees in the Employee_details table in
the single query statement.

10. TRUNCATE TABLE Statement


This SQL statement deletes all the stored records from the table of the SQL database.

Syntax of TRUNCATE TABLE Statement:

1. TRUNCATE TABLE table_name;

Example of TRUNCATE TABLE Statement:

TRUNCATE TABLE Employee_details;


This example deletes the record of all employees from the Employee_details table of the
database.

pg. 6
SQL SYNTAX

11. DESCRIBE Statement


This SQL statement tells something about the specified table or view in the query.

Syntax of DESCRIBE Statement:

1. DESCRIBE table_name | view_name;


Example of DESCRIBE Statement:

1. DESCRIBE Employee_details;
This example explains the structure and other details about
the Employee_details table.

12. DISTINCT Clause


This SQL statement shows the distinct values from the specified columns of the database
table. This statement is used with the SELECT keyword.

Syntax of DISTINCT Clause:

1. SELECT DISTINCT column_name1, column_name2, ...


2. FROM table_name;
Example of DISTINCT Clause:

1. SELECT DISTINCT City, Salary


2. FROM Employee_details;
This example shows the distinct values of the City and Salary column from
the Employee_details table.

13. COMMIT Statement


This SQL statement saves the changes permanently, which are done in the transaction of
the SQL database.

Syntax of COMMIT Statement:

1. COMMIT
Example of COMMIT Statement:

1. DELETE FROM Employee_details


2. WHERE salary = 30000;
3. COMMIT;
This example deletes the records of those employees whose Salary is 30000 and then
saves the changes permanently in the database.

pg. 7
SQL SYNTAX

14. ROLLBACK Statement


This SQL statement undo the transactions and operations which are not yet saved to the
SQL database.

Syntax of ROLLBACK Statement:

1. ROLLBACK
Example of ROLLBACK Statement:

1. DELETE FROM Employee_details


2. WHERE City = Mumbai;
3. ROLLBACK;
This example deletes the records of those employees whose City is Mumbai and then
undo the changes in the database.

15. CREATE INDEX Statement


This SQL statement creates the new index in the SQL database table.

Syntax of CREATE INDEX Statement:

1. CREATE INDEX index_name


2. ON table_name ( column_name1, column_name2, …, column_nameN );
Example of CREATE INDEX Statement:

1. CREATE INDEX idx_First_Name


2. ON employee_details (First_Name);
This example creates an index idx_First_Name on the First_Name column of
the Employee_details table.

16. DROP INDEX Statement


This SQL statement deletes the existing index of the SQL database table.

Syntax of DROP INDEX Statement:

1. DROP INDEX index_name;


Example of DROP INDEX Statement:

1. DROP INDEX idx_First_Name;


This example deletes the index idx_First_Name from the SQL database.

pg. 8
SQL SYNTAX

17. USE Statement


This SQL statement selects the existing SQL database. Before performing the operations
on the database table, you have to select the database from the multiple existing databases.

Syntax of USE Statement:

1. USE database_name;
Example of USE DATABASE Statement:

1. USE Company;
This example uses the company database.

pg. 9

You might also like