SQL Basics
SQL Basics
Statements
Statement types
Clauses
Expressions
Agenda Predicates
Operators
Functions
Clean code
SQL Elements
• Clauses
• Expressions
• Predicates
• Queries
• Statements
© 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.
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
© 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.
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.
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.
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)
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
© 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.
© 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 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.
© 2020 Veeam Software. Confidential information. All rights reserved. All trademarks are the property of their respective owners.
Q&A
Thank you