Sie sind auf Seite 1von 34

SQL Basics

SQL language elements

Statements

Statement types

Clauses

Expressions

Agenda Predicates

Operators

Functions

Logical order of operations in SELECT statement

Data Flow in VEEAM

Clean code
SQL Elements
• Clauses
• Expressions
• Predicates
• Queries
• Statements

SQL statements also include the semicolon (";") statement


terminator.

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
SQL Elements
Clauses, which are constituent components of statements and queries- in some
cases, these are optional.

Expressions, which can produce either scalar values, or tables consisting


of columns and rows of data.

Predicates, which specify conditions that can be evaluated to SQL three-valued


logic (3VL)(true/false/unknown) or Boolean truth values and are used to limit
the effects of statements and queries, or to change program flow.

Queries, which retrieve the data based on specific criteria. This is an important
element of SQL.

Statements, which may have a persistent effect on schema and data, or may
control transactions, program flow, connections, sessions, or diagnostics.
© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
STATEMENTS
SQL Statement Types

Data Manipulation Data Definition Data Control Language


Language (DML) Language (DDL) (DCL)
Statements for Statements for defining Statements for assigning
querying and modifying database objects: security permissions:
data: • CREATE • GRANT
• SELECT • ALTER • REVOKE
• INSERT • DROP • DENY
• UPDATE
• DELETE

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
SELECT statement
Returns a result set from one or more data sources.

INSERT statement
Inserts values into all projections of the specified table
SQL Elements (insert data into temporary tables).

UPDATE statement
Replaces the values of the specified columns in all rows
for which a specific condition is true (update data from
temporary tables).
Statements
CREATE statement
Let you create new database objects, such as tables and
users (we are only allowed to create temporary tables).

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Example

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
CLAUSES
.

FROM clause
A comma-separated list of data sources (columns) to
query.

WHERE clause
Eliminates rows from the result table that do not satisfy
SQL Elements one or more predicates (acts like a filter).

GROUP BY clause
Used with aggregate functions in a SELECT statement to
collect data across multiple records.

Clauses HAVING clause


Filters the results of a GROUP BY clause (occurs after the
GROUP BY operation). Added because WHERE clause
cannot specify aggregate functions

ORDER BY clause
Sorts a query result set on one or more columns.
© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Examples

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
EXPRESSIONS
.
SQL expressions are the components of a query that
compare a value or values against other values.
Basically, anything that returns or changes a value.

They can also perform calculations.

SQL Elements When an expression includes more than one operator, it


is recommended that you specify the order of operation
using parentheses, rather than relying on operator
precedence.

Aggregate Expression CASE Expression

Expressions

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
PREDICATES
Understanding predicate logic
A predicate may be true or false depending on the values of its
variables.

It can be thought of as an operator or function that returns a value


that is either true or false.

A predicate is an expression that evaluates to TRUE, FALSE, or


UNKNOWN.

Predicates are used in the search condition of WHERE clauses and


HAVING clauses, the join conditions of FROM clauses, and other
constructs where a Boolean value is required.
© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
IN-predicate
IN and NOT IN deal with whether specified values are
contained within a particular set of values

SQL Elements LIKE-predicate


Used to search for a specified pattern in a column.

% wildcard is used in conjunction with LIKE and


represents zero, one or multiple characters.

Predicates

BETWEEN-predicate
Specifies a range to test.

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Examples

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
OPERATORS
Date/Time Operators
+ (Addition),- (Subtraction),* (Multiplication), / (Division)

String Concatenation Operator


Operators ||

Bitwise Operators
& (AND), ~ (NOT),| (OR),…

Comparison Operators
= (Equals), > (Greater Than), < (Less Than), >=
(Greater Than or Equal To), <= (Less Than or Equal To),
<>, != (Not Equal To)

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
@

Boolean Operators
AND, NOT, OR

Set Operators
EXCEPT, INTERSECT, UNION
Operators
Mathematical Operators
+ (Add)
- (Subtract)
* (Multiply)
/ (Divide)
% (Modulo)
@ (Absolute value)
! (Factorial)

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Examples

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
FUNCTIONS
String Functions
• SUBSTRING
• LEFT, RIGHT
• LENGTH
• REPLACE
• REPEAT
Functions •

UPPER, LOWER
LTRIM, RTRIM
• CONCAT

Date and Time


• GETDATE
• SYSDATE
• DATEDIFF
• YEAR, QUARTER, MONTH, DAY, HOUR, MINUTE
• DAYOFMONTH, DAYOFWEEK
• EXTRACT
• TRUNC

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Aggregate Functions
• SUM
• MIN
• MAX
• AVG
• COUNT
Functions •

STDEV
VAR_POP

Mathematical Functions
• ABS
• DEGREES
• RANDOM
• ACOS
• ROUND
• FLOOR
• SIN

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Aggregate

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Date and time

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Understanding the Logical Order of
Operations in SELECT Statements

5
1
2
3
4
6

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
DATA FLOW
Data flow
The Postgres DWH replicates raw data from SFDC. Normally you should find here most of columns (at least more
than in Vertica).

Vertica DWH loads data from the Postgres one. The purpose of Vertica DWH is to allow you to do all calculations and
analytics, because it is a lot more efficient for these operations.

Data in Vertica is updated with data by some integration processes developed by the CIS Team. These processes
constantly run at a rate of 15 mins, depending on the table.

Because of its analytical nature, columns are implemented here only if necessary. There is a development cost in
implementing new columns.

Veeam IT CIS – DWH Team

Relational Junction data


data jobs
jobs (ETL/ELT)
(ETL/ELT)

Veeam Operational Analytical


Production DWH DWH
Salesforce (Postgres) (Vertica)

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
CLEAN CODE
Beautify your code
Alignment, indentation, comments

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Naming objects
Avoid the name of a table/column in the plural (employee-> not employees).

Check that the name is not already used as a keyword in SQL (enclose the name within
quotation marks if it’s a keyword).

Use “AS” keyword for creating aliases, because this makes the code more readable.

Avoid giving the same name to both a table and a column.

Avoid abbreviations, but, if you do use them, be sure that they will be understood.

Use the same naming rules for aliases for columns or tables.

Use semi-colon separator “;”.

© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Q&A
Thank you

Das könnte Ihnen auch gefallen