SoFunction
Updated on 2025-05-09

The best of SQL common operations: copying tables, cross-store query, and deleting duplicate data

Summary of the essence of SQL common operations

Table structure and data operations

Copy the table structure

SELECT * INTO b FROM a WHERE 1<>1 (SQL Serverdedicated)
SELECT TOP 0 * INTO b FROM a (More general)

Copy table data

INSERT INTO b(a,b,c) SELECT d,e,f FROM a

Cross-database copy

Specify the source database using absolute path

Advanced query skills

Subquery

Conditional filtering using IN clause

External connection query

LEFT OUTER JOIN connection method

View query

Secondary filtering from subquery results

Range query

BETWEEN contains boundary values, NOT BETWEEN does not contain

Multi-table joint search

Supports complex association queries of four tables and above

Data Management

Delete orphaned data from the main table

Use NOT EXISTS to determine the relationship

Random record acquisition

SELECT TOP 10 * FROM table ORDER BY NEWID()

Delete duplicate records

Three methods: NOT IN grouping retains the maximum ID, temporary table method, and adds self-increment column method

Practical Tips

Database pagination

Efficient paging using nested queries and TOP

Grouping maximum value

Can be used in rankings and other scenarios

Exclude query

Exclude data from multiple tables using EXCEPT

Metadata query

Get all table names and column names of the database

Conditional aggregation

Implement multiple condition statistics using CASE

Table initialization

TRUNCATE TABLE Quickly clear table

Range selection

Nested TOP implements the acquisition of specified range records

These SQL operations cover the most commonly used technical points in database development, including table operations, data query, data management, etc., and are the core skills necessary for database developers.

This is the article about the replication table, cross-store query, and deletion of duplicate data in SQL common operations. This is the end of this article. For more related content on deletion of duplicate data in SQL, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!