SQL Syntax

SQL Statements

SQL statements end with a semicolon and follow a simple declarative structure:

SELECT * FROM customers;
DELETE FROM users WHERE id = 10;

Keywords and Identifiers

Keywords like SELECT, FROM, and WHERE are not case-sensitive. Table and column names may be case-sensitive depending on the database system.

  • SELECT: Specifies the columns or expressions you want to retrieve from the database.
    Example: SELECT name, age
  • FROM: Indicates the table from which to retrieve the data.
    Example: FROM users
  • WHERE: Filters the rows based on a specified condition.
    Example: WHERE age > 18

Formatting and Indentation

SQL queries are easier to read with proper indentation:

SELECT first_name, last_name
FROM employees
WHERE department = 'Sales';

Comments in SQL

Use comments to explain complex sections of a query:

-- This is a single-line comment
/* This is
   a multi-line comment */

String and Numeric Literals

Use single quotes for string values:

SELECT * FROM users WHERE name = 'Alice';
SELECT * FROM orders WHERE amount > 100;

Execution Order

Though we write SQL in a readable format, the database executes queries in the following logical order:

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY
  7. LIMIT

Best Practices

  • Use uppercase for SQL commands and lowercase for table/column names
  • Terminate all statements with semicolons
  • Comment non-trivial queries

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic