SQL Syntax

SQL syntax has been written in such a way that you can easily understand and remember it. SQL syntax follows the rules defined by American National Standards Institute (ANSI) and the International Organization for Standardization (ISO).


SQL Statements

SQL statements are used to query the database. SQL statements are basically a sequence of keywords, identifiers etc.

Following is an example of SQL statement, which queries the database to get the list of all employees whose salary is greater than 30 thousand.

Example

Try It
SELECT * FROM [Employees] WHERE Salary > 30000;

We can write a SQL statement on multiple lines as well. To make the statements more clear the above statement can be written as below as well.

Example

Try It
SELECT * FROM [Employees] 
         WHERE Salary > 30000;

Does SQL case sensitive?

SQL keywords are not a case-sensitive meaning SELECT can be written as select or INSERT can be written as insert and so on.

The above SQL statement can be written as below, this will give you the same result as above.

Example

Try It
select * from [employees] where salary > 30000; 

Semicolon after SQL statements..

The semicolon is a standard way to terminate the SQL statements.

In some databases semicolon after SQL statements is not required, but in some database semicolon is required in particular cases like in MSSQL, to use Common Table Expressions (CTE), the preceding statement must be terminated with a semi-colon

SQL Comments

When we comment a line or statement in SQL, the database engine simply ignores that line or statement.

SQL comments are useful in many cases like if you want to put the author name of a particular part in your SQL you can make use of SQL comments.

In SQL we can comment the statements in two different ways, the first is by using a single line commenting and second is multiline commenting. SQL support both kinds of comments. A single line comment starts with two hyphens(--) one after another. Following is an example of single line comment.

Example

Try It
--Get the list of all Employees whose salary is greater than 30000
SELECT * FROM [Employees] WHERE Salary > 30000;

SQL Multiline comments start with a slash (/) followed by an asterisk (*) character and end with an asterisk (*) followed by a slash (/) . Following is an example of multiline comments in SQL.

Example

Try It
/*Get the list of all Employees 
Selects those Employees whose salary is greater than 30000*/
SELECT * FROM [Employees] WHERE Salary > 30000;