link: SQL Operations
Advanced Querying Techniques
Window Functions
Window functions perform calculations across sets of rows related to the current row, providing powerful analytics capabilities directly in SQL queries.
- Functions Include:
ROW_NUMBER()
,RANK()
,DENSE_RANK()
,SUM()
,AVG()
,MIN()
,MAX()
, over specified partitions of data.
Recursive Queries
Recursive queries are used for processing hierarchical or nested data structures, such as organizational charts or multi-level categories.
Common Table Expressions (CTEs)
CTEs allow for the temporary naming of a result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement.
- Benefits: Enhances readability and maintainability of complex SQL queries, especially useful for breaking down complex joins and subqueries.
Pivoting Data
Pivoting transforms data from rows into columns, turning unique values from one column into multiple columns in the output, facilitating easier comparative analysis.
Dynamic SQL
Dynamic SQL involves SQL commands that are dynamically constructed at runtime and executed as strings. This allows for more flexible and programmable interactions with the database.
- Use Cases: Building queries programmatically based on varying user inputs or application conditions.
Advanced Joins
Beyond basic inner and outer joins, advanced joins include self joins, cross joins, and natural joins which are used to handle specific types of data relationships and query requirements.
- Complexity Handling: Useful in scenarios requiring sophisticated data linkage such as combining data from a table with itself (self-join) or cartesian products (cross join).
Set Operations
Set operations extend the ability to perform mathematical set operations like unions, intersections, and differences directly with result sets.
- Operations:
UNION
,INTERSECT
, andEXCEPT
, each serving to combine or contrast data from multiple queries.
Windowing
Windowing pertains to the ability of window functions to operate on a set of rows with a flexible range related to the current row, which is great for running totals or moving averages.
- Capabilities: Defining windows or frames of data for calculations can be adjusted dynamically over a specified range.
Geospatial Functions
SQL databases equipped with geospatial capabilities allow for the manipulation and query of geographic data within SQL.
- Functionalities: Functions like
ST_Contains
,ST_Distance
, and others in PostGIS (PostgreSQL) or spatial functions in MySQL help in processing geospatial information.
Analytic Functions
Analytic functions process data over a group of rows and return a single result for each row. These are akin to aggregate functions but more powerful due to their ability to respect row order and partitioning without collapsing rows.
- Examples:
LEAD
,LAG
,FIRST_VALUE
,LAST_VALUE
.