A subquery, an inner or nested query, is embedded within a PostgreSQL query and can be placed in a WHERE clause. It is utilized to return data that serves as a condition for the main query, helping to refine the data retrieval process. Subqueries can be employed with various statements, including SELECT, INSERT, UPDATE, and DELETE, as well as with operators such as =, <, >, <=, IN, and others.
Subqueries must adhere to these rules:
They need to be contained within parentheses.
A subquery should consist of only one column in the SELECT clause, unless the main query includes multiple columns for comparison with the subquery's selected column.
While ORDER BY cannot be utilized in a subquery, it can be applied in the main query, functioning similarly to GROUP BY in a subquery.
Subqueries that return multiple rows can only interact with various value operators, including IN, EXISTS, NOT IN, ANY / SOME, and ALL.
The BETWEEN operator is not allowable within a subquery, although it can be employed inside a subquery.
Subqueries are primarily utilized in SELECT statements, following this basic syntax:
Note: While subqueries are primarily utilized in SELECT, they also apply in INSERT, UPDATE, and DELETE.
Example:
Utilize the test table and data provided for SubQuery testing.
Retrieve the names of all IDs from table t1 that have a score of 90 or higher in table t2.
The WITH query is an advanced SQL feature in PostgreSQL, commonly known as CTE (Common Table Expressions). This feature allows you to define an auxiliary statement within a complex query, similar to a temporary table created in the query. When dealing with intricate query statements, using WITH can significantly simplify the SQL, enhancing readability.
With Query syntax:
Here’s a detailed example illustrating how to use the query. Below are the definitions and data for the test table:
The DELETE command removes data from a table. You can target particular rows for deletion using the defined WHERE condition. If no WHERE condition is provided, all the data in the table will be deleted.
Refer to the DELETE documentation for additional usage syntax.
Utilize DELETE USING to reference other tables in a delete statement, streamlining complex delete operations that involve multiple tables in one SQL query.
To enable multi-version concurrency control and TimeTravel features, MVCC attaches a timestamp to each record as a version marker. The deletion process does not instantly remove the old data from the disk. Instead, the old data is purged through asynchronous tasks only after the expired MVCC version. Consequently, storage space increases after a delete operation is performed but decreases once the asynchronous compaction is finished.
To implement multi-version concurrency control and TimeTravel features, MVCC attaches timestamp information to each record as a version indicator. The update operation does not instantly remove the old data from the disk. Instead, the old data is purged through asynchronous tasks once the MVCC version expires. As a result, storage space increases following the execution of an update operation, and it only decreases after the asynchronous compaction is finalized.
The MERGE INTO statement allows you to combine records by integrating INSERT, UPDATE, and DELETE operations into a single command. It executes the appropriate actions depending on the matching conditions between the source and target tables. With MERGE INTO, you can efficiently maintain data consistency while synchronizing information across two tables.
Usage Scenarios
Data Warehouse ETL: Combine incremental data into data warehouse tables.
Data Synchronization: Align data across various systems or databases.
Data Deduplication: Remove duplicates and refresh data in a table according to defined criteria.
Syntax:
Parameters:
target_table_name: The table that will be updated or have data inserted into
source_table_name: The table that supplies the data
condition: Defines the matching criteria between the target and source tables
WHEN MATCHED THEN: Perform an UPDATE operation when the matching criteria is satisfied
WHEN NOT MATCHED THEN: Perform an INSERT operation when the matching criteria is not satisfied
A view is fundamentally a stored query statement within a database. It functions as a virtual table without holding actual data. When we create a view, we assign it a name, allowing us to query it similarly to a table. A view does not exist physically, meaning a query executes each time the view is accessed. This approach is advantageous because it preserves only the query statement, alleviating the issues of constant data writing during frequent SQL calls.