SQL Syntax


SQL is not case sensitive. Generally SQL keywords are written in uppercase.

SQL statements are dependent on text lines. We can place a single SQL statement on one or multiple text lines.

You can perform most of the action in a database with SQL statements.

SQL depends on relational algebra and tuple relational calculus.


SQL statement

SQL statements are started with any of the SQL commands/keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP etc. and the statement ends with a semicolon (;).

SELECT column_name FROM table_name;

Semicolon is used to separate SQL statements. It is a standard way to separate SQL statements in a database system in which more than one SQL statements are used in the same call.


SQL Commands

These are the some important SQL command:

  • SELECT: It extracts data from a database.
  • UPDATE: It updates data in database.
  • DELETE: It deletes data from database.
  • CREATE TABLE: It creates a new table.
  • ALTER TABLE: It is used to modify the table.
  • DROP TABLE: It deletes a table.
  • CREATE DATABASE: It creates a new database.
  • ALTER DATABASE: It is used to modify a database.
  • INSERT INTO: It inserts new data into a database.
  • CREATE INDEX: It is used to create an index (search key).
  • DROP INDEX: It deletes an index.

SQL SELECT Statement

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

SQL DISTINCT Clause

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

SQL WHERE Clause

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

SQL AND/OR Clause

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

SQL IN Clause

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

SQL BETWEEN Clause

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

SQL LIKE Clause

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

SQL ORDER BY Clause

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

SQL GROUP BY Clause

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

SQL COUNT Clause

SELECT COUNT(column_name)
FROM   table_name
WHERE  CONDITION;

SQL HAVING Clause

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

SQL CREATE TABLE Statement

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

SQL DROP TABLE Statement

DROP TABLE table_name;

SQL CREATE INDEX Statement

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

SQL DROP INDEX Statement

ALTER TABLE table_name
DROP INDEX index_name;

SQL DESC Statement

DESC table_name;

SQL ALTER TABLE Statement

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

SQL ALTER TABLE Statement (Rename)

ALTER TABLE table_name RENAME TO new_table_name;

SQL INSERT INTO Statement

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

SQL UPDATE Statement

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

SQL DELETE Statement

DELETE FROM table_name
WHERE  {CONDITION};

SQL CREATE DATABASE Statement

CREATE DATABASE database_name;

SQL DROP DATABASE Statement

DROP DATABASE database_name;

SQL USE Statement

USE database_name;

Share this article on