Lecture 5 - SQL Part III
Lecture 5 - SQL Part III
Roi Yehoshua
Agenda
Accessing SQL from Python
Data warehouses and lakes
OLAP
Window functions
If you’ve installed Python with the Anaconda distribution, you should already have it
You should always close the connection in the end by calling conn.close()
You should never hard-code your login credentials directly in a Python script
The where clause is now always true and the entire users table is returned
14 Roi Yehoshua, 2024
SQL Injection Attack
Most databases allow execution of multiple SQL statements separated by semicolon
This allows the hacker to inject whole SQL statements into the query
For example, the hacker could enter the following string:
Solution: use parameterized queries whenever user input is involved in the query
You must call conn.commit() at the end, otherwise your changes will be lost!
Unless you turn on automatic commits by setting conn.autocommit = True
Source: https://www.grazitti.com/blog/data-lake-vs-data-warehouse-which-one-should-you-go-for
The OVER clause defines the rows on which the window function operates
An optional PARTITION BY clause divides the rows into groups
The ORDER BY clause determines the order of the rows within each partition
Frame specification defines which rows are included in the frame (subset of the current row)
53 Roi Yehoshua, 2024
Example: Moving Average
The AVG() function can be used as a window function to get a moving average
For example, assume that we have a table with daily sales figures
We can calculate a 3-day moving average for sales as follows: