SQL Syntax and Best Practices (Slides)
SQL Syntax and Best Practices (Slides)
| The SQL syntax structure consists of several key components that include clauses, keywords,
operators, identifiers, and comments that make up the query.
2
SQL basics
SQL syntax structure
| The SQL syntax structure consists of several key components that include clauses, keywords,
operators, identifiers, and comments that make up the query.
SQL is built upon a set of reserved keywords that have specific meanings and functions.
Examples of keywords include SELECT, FROM, WHERE, ORDER BY, GROUP BY, and JOIN.
3
SQL basics
SQL syntax structure
| The SQL syntax structure consists of several key components that include clauses, keywords,
operators, identifiers, and comments that make up the query.
| The SQL syntax structure consists of several key components that include clauses, keywords,
operators, identifiers, and comments that make up the query.
Identifiers are used to name SQL supports various operators for performing operations on data,
tables, columns, and other including arithmetic (+, -, *, /), comparison (=, <, >, <=, >=, <>),
database objects. logical AND, OR, NOT, and string concatenation operators (+ or ||.
5
SQL basics
Naming conventions
| SQL is generally not case-sensitive. This means that whether you write keywords in uppercase
or lowercase, the SQL engine will interpret them the same way.
However, this does not affect the way the SQL engine
interprets the statement.
6
SQL basics
Naming conventions
|
We use relevant, descriptive, and consistent identifiers for tables, columns, and other database
objects. In most cases, spaces and other special characters can cause errors, so we avoid them. The
most common naming conventions include:
The first letter of each word is Each word starts with an Each word is separated by an
lowercase, and the first letter of uppercase letter, and there are underscore, and all letters are
each subsequent concatenated no separators between words. lowercase.
word is capitalised.
SQL basics
Semicolons
| In SQL, semicolons (;) are used as statement terminators. They indicate the end of a SQL
statement and separate multiple SQL statements within a script.
UPDATE employees
SET salary = 55000
WHERE employee_id = 1;
SELECT *
FROM employees;
8
SQL basics
Commenting
| Comments are used to explain the reasoning behind a statement, provide reminders or to-do
lists for future changes, temporarily disable some functions for debugging, making notes, etc.
| Line breaks, also known as line terminators, are characters used to systematically separate
lines of code in SQL.
Functionality Example
● It is recommended to use line breaks consistently SELECT last_name, salary FROM employees WHERE salary = 50000;
and strategically to improve the clarity and
readability of SQL code.
10
SQL basics
Line breaks
| In SQL, there are different systems for using line breaks depending on personal preference and
coding style. Here are the most commonly used:
11