SQL Syntax

In this lesson, you will learn the basic rules that you should follow whenever you write SQL statements.

SQL Statements

An SQL statement can be composed of a sequence of keywords, identifiers and operators to perform an action on a database.

Its recommended to add a semicolon ; at the end of the statement. This is allows you to write several statements and execute them together as one query.

In this tutorial, we added semicolon at the end of all statements.

Case Senstivity in SQL

SQL keywords are not case senstive, that means it doesn't matter wheter you write select or SELECT. However, it's prefered to write SQL keywords in capital letters because this make code readability easier.

Always deal with identifiers as you define them to be sure that your statements will work whether the database engine is case sensitive for identifiers or not.

In the following example, we write SQL keywords in capital letters. Notice how we can easily distinguish keywords and identifiers used in the statement.

Sample

SELECT email FROM users;
    

SQL Keywords

The following words are SQL Keywords which mean that they used to write SQL queries.

ADD - ALTER - ALL - AND - ANY - ASC - AUTO_INCREMENT - BACKUP - BETWEEN - CASE - CHECK - COLUMN - CONSTRAINT - CREATE - DATABASE - DEFAULT - DELETE - DISTINCT - DROP - EXEC - EXISTS - FOREIGN - FROM - FULL - HAVING - INDEX - INNER - INSERT - INTO - IS - JOIN - KEY - LEFT - LIKE - LIMIT - NOT - NULL - OR - ORDER BY - OUTER - UPDATE - REPLACE - RIGHT - ROWNUM - SELECT - SET - TABLE - TRUNCATE - TOP - UNIQUE - UNION - VALUES - VIEW - PRIMARY - PROCEDURE - WHERE

These are SQL standard keywords. However, some database systems may have extra keywords.

Single-line vs multi-line Statements

In general, you can write the statement on a single line or on multiple lines. For better readability, use the one line style if the statement is short and the multiline style if it's long.

In the following example, we write the statement on a single line because it's short.

Inline Statement

SELECT * FROM users;
    

In the following example, we write the statement on a multiple line because it's long.

Multi-line Statement

SELECT id, username, email
FROM users
Where id = 1;
    

SQL Comments

A comment is a text added to provide a hint about the SQL statement. Comments used for developers only. Database engine ignore comments.

A comment can be written on a single-line using -- or on multi-line using /* */.

For a single-line comment add -- at the beginning.

Single-line comment

-- Select all the products
SELECT id, name, price
FROM products;
    

For a multi-line comment add /* at the beginning and */ at the end.

Multi-line comment

/* Select all the products where 
price is greater than 100 */
SELECT id, name, price
FROM products
WHERE price > 100;
    

If you use a multi-line comment and forget to add the */ at the end, then the statements written after the comment will be ignored by the database engine.

Tutorials

Online Tools

Sections

Tutorials
Tools
Posts