0% found this document useful (0 votes)
17 views

SQL Syntax and Best Practices (Slides)

Uploaded by

iaseene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

SQL Syntax and Best Practices (Slides)

Uploaded by

iaseene
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL basics

SQL syntax and best practices


Please do not copy without permission. © ExploreAI 2023.
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.

-- This comment explains the purpose of the query


SELECT
last_name,
salary Comments provide
FROM explanatory notes
employees within the code which
WHERE
serve only as
department_id = 5
AND salary > 50000
documentation.
ORDER BY
last_name ASC;

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.

-- This comment explains the purpose of the query


SELECT
last_name,
salary
FROM
employees
WHERE
department_id = 5
AND salary > 50000
ORDER BY
last_name ASC;

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.

-- This comment explains the purpose of the query


SELECT
last_name,
salary
FROM
employees
WHERE
department_id = 5
AND salary > 50000
ORDER BY
last_name ASC;

Identifiers are used to name


tables, columns, and other
database objects.
4
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.

-- This comment explains the purpose of the query


SELECT
last_name,
salary
FROM
employees
WHERE
department_id = 5
AND salary > 50000
ORDER BY
last_name ASC;

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, to make them easier to read and SELECT


distinguishable from the identifiers, it is last_name,
salary
recommended that keywords and clauses be written in FROM
uppercase letters and begin on separate lines. employees
WHERE
department_id = 5
Following this convention helps to maintain AND salary > 50000
consistency in SQL code and makes it easier for other ORDER BY
developers to understand and work with the code. last_name ASC;

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:

camelCase PascalCase Underscore


SELECT SELECT SELECT
firstName, FirstName, first_name,
lastName LastName last_name
FROM FROM FROM
employees Employees employees

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.

INSERT INTO employees (first_name, last_name, salary)


VALUES ('John', 'Doe', 50000);

UPDATE employees
SET salary = 55000
WHERE employee_id = 1;

SELECT *
FROM employees;

By placing a semicolon at the end of each statement, we are able


to execute multiple queries in a single run.

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.

Single-line commenting Multi-line commenting


/*
-- This is a single-line comment
This is a multi-line comment.
SELECT *
It can span multiple lines.
FROM employees; -- Retrieve all records from the
*/
employees table
SELECT *
FROM employees;
Single-line comments begin with a double-dash (--).
They are used to provide explanatory notes or Multi-line comments begin with /* and end with */.
documentation for a specific line or statement. They are used to provide comments that span
Anything after the double-dash until the end of the multiple lines or to temporarily disable a block of
line is considered a comment and is ignored by the code. Anything between /* and */ is considered a
SQL engine. comment and is ignored by the SQL engine.
9
SQL basics
Line breaks Functionality

| 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.

● By using line breaks, SQL code becomes more


visually appealing and easier to read and
SELECT
comprehend, especially long and complex last_name,
queries. salary
FROM employees
● SQL ignores line breaks, so they do not affect the WHERE
functionality or performance of the queries. salary > 50000;

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:

Compact system Aligned system


SELECT first_name, last_name SELECT
FROM employees first_name,
last_name
WHERE (department_id = 5
FROM
AND salary > 50000);
employees
WHERE (
department_id = 5
Line breaks are used sparingly, only at the end of each AND salary > 50000
statement. This is useful for shorter queries or in situations );
where minimising vertical space is preferred.
● Keywords start on a new line and related column names are
● Keywords and the related column names start on a new given in new indented lines below.
line. ● Booleans start on a new line indented to the right.
● Parentheses are typically placed on the same line as the ● Opening parentheses terminate the line and closing
corresponding expression or condition. parentheses align with the last keyword on a new line.

11

You might also like