SQL Syntax

SQL Statements

SQL statements are designed to be simple and straightforward, resembling plain English but with specific syntax.

An SQL statement consists of a sequence of keywords, identifiers, etc., terminated by a semicolon (;). Here's an example of a valid SQL statement:

SELECT emp_name, hire_date, salary FROM employees WHERE salary > 5000;

For improved readability, you can also write the same statement as follows:

SELECT emp_name, hire_date, salary 
FROM employees 
WHERE salary > 5000;

It's best practice to use a semicolon at the end of an SQL statement—it either terminates the statement or submits it to the database server. While some database management systems do not enforce this, it's widely recommended.

In the following chapters, we'll delve into each part of these statements in detail.

Note: You can include any number of line breaks within a SQL statement, as long as they do not disrupt keywords, values, expressions, etc.


Case Sensitivity in SQL

Let's look at another SQL statement that retrieves records from the employees table:

SELECT emp_name, hire_date, salary FROM employees;

The same statement can also be written as follows:

select emp_name, hire_date, salary from employees;

SQL keywords are case-insensitive, which means SELECT is the same as select. However, database and table names may be case-sensitive depending on the operating system. Typically, Unix or Linux platforms are case-sensitive, while Windows platforms are not.

Tip: It's recommended to write SQL keywords in uppercase to distinguish them from other text within a SQL statement, improving readability and understanding.


SQL Comments

A comment is text that the database engine ignores. Comments can provide quick hints about SQL statements.

SQL supports both single-line and multi-line comments. To write a single-line comment, start the line with two consecutive hyphens (--). For example:

-- Select all the employees
SELECT * FROM employees;

To write multi-line comments, begin the comment with a slash followed by an asterisk (/*) and end it with an asterisk followed by a slash (*/), as shown below:

/* Select all the employees whose 
salary is greater than 5000 */
SELECT * FROM employees
WHERE salary > 5000;