Data Query, Update and Delete
SELECT
Overview
The SELECT
statement is used to retrieve data from a table or view. The syntax is as follows:
For more syntax options regarding SELECT
, refer to SELECT
.
Examples
Refer to the table below for the SELECT
test.
- Retrieve the data from the table that satisfies the given criteria.
- Select specific fields to query
SubQuery
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 toGROUP BY
in a subquery. - Subqueries that return multiple rows can only interact with various value operators, including
IN
,EXISTS
,NOT IN
,ANY
/SOME
, andALL
. - 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.
With Query (CTE)
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:
USING With Query in SELECT
.
Benefits of using CTE:
- CTE simplifies SQL code and enhances readability.
- It is lighter than the views.
DELETE
Overview
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.
Examples
Conduct a 'DELETE' test for the table below.
Remove a record from the table.
Remove all data from the entire table.
Utilize DELETE USING
to reference other tables in a delete statement, streamlining complex delete operations that involve multiple tables in one SQL query.
Notices
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.
UPDATE
Overview
To change data in a table, utilize the UPDATE
statement. Here’s the syntax for UPDATE
:
To learn more about the UPDATE
usage syntax, refer to UPDATE
.
Examples
Perform an 'UPDATE' test using the table provided below.
- Revise a row of data in the table.
- Update multiple rows that fulfill the specified conditions.
Notices
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.
MERGE
Overview
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 intosource_table_name
: The table that supplies the datacondition
: Defines the matching criteria between the target and source tablesWHEN MATCHED THEN
: Perform anUPDATE
operation when the matching criteria is satisfiedWHEN NOT MATCHED THEN
: Perform an INSERT operation when the matching criteria is not satisfied
Examples
For additional syntax regarding MERGE
, refer to MERGE
.
TRUNCATE
Overview
The ’TRUNCATE' command removes all entries from a table. Its syntax is as follows:
For additional syntax on TRUNCATE
, refer to TRUNCATE
.
Examples
Refer to the table below to conduct the TRUNCATE
test.
Truncate the table.
Notices
TRUNCATE
functions like DELETE
, yet it performs more quickly since it skips scanning the table.
VIEW
Overview
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.
Syntax:
To explore additional syntax for VIEW
, please check out CREATE VIEW
, ALTER VIEW
, and DROP VIEW
.
Examples
Refer to the table below for conducting tests related to views.
- Creating a View
You can add several tables in a SELECT
statement, similar to how you would in a SQL SELECT
query.
- Modify a view
- Deleting a View
Remove the view by dropping it. Here’s an example:
- Query all views
You can access all user-defined view information via information_schema.tables or by using \dv
.