Esta página ainda não está disponível no idioma selecionado. Estamos trabalhando para adicionar mais opções de idiomas. Agradecemos sua compreensão.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Usage of GaussDB(DWS) UNION

Updated on 2024-12-18 GMT+08:00

UNION is a powerful SQL operator that combines the result sets of two or more SELECT statements into one. During combination, the number of columns and data types in the two tables must be the same and correspond to each other. Use the UNION or UNION ALL keyword between SELECT statements.

UNION removes duplicate rows, while UNION ALL keeps them. Deduplication is time-consuming, so UNION ALL can be faster than UNION if the data sets are already distinct by logic.

Syntax

1
SELECT column,... FROM table1 UNION [ALL]SELECT column,... FROM table2

Example

  1. Create the student information table student (ID, name, gender, and school).

    1
    2
    3
    4
    5
    6
    7
    SET current_schema=public;
    DROP TABLE IF EXISTS student;
    CREATE table student( 
    sId VARCHAR(10) NOT NULL,
    sname VARCHAR(10) NOT NULL,
    sgender VARCHAR(10) NOT NULL,
    sschool VARCHAR(10) NOT NULL);
    

  2. Insert data into the student table.

    1
    2
    3
    4
    5
    6
    7
    8
    INSERT INTO student VALUES('s01' , 'ZhaoLei' , 'male', 'NENU'); 
    INSERT INTO student VALUES('s02' , 'QianDian' , 'male', 'SJTU'); 
    INSERT INTO student VALUES('s03' , 'SunFenng' , 'male', 'Tongji'); 
    INSERT INTO student VALUES('s04' , 'LIYun' , 'male', 'CCOM'); 
    INSERT INTO student VALUES('s05' , 'ZhouMei' , 'female', 'FuDan'); 
    INSERT INTO student VALUES('s06' , 'WuLan' , 'female', 'WHU'); 
    INSERT INTO student VALUES('s07' , 'ZhengZhu' , 'female', 'NWAFU'); 
    INSERT INTO student VALUES('s08' , 'ZhangShan' , 'female', 'Tongji');
    

  3. View the student table.

    1
    SELECT * FROM student;
    

    Information similar to the following is displayed.

  4. Create the teacher information table teacher (ID, name, gender, and school).

    1
    2
    3
    4
    5
    6
    DROP TABLE IF EXISTS teacher;
    CREATE table teacher( 
    tid VARCHAR(10) NOT NULL,
    tname VARCHAR(10) NOT NULL,
    tgender VARCHAR(10) NOT NULL,
    tschool VARCHAR(10) NOT NULL);
    

  5. Insert data to the teacher table.

    1
    2
    3
    INSERT INTO teacher VALUES('t01' , 'ZhangLei', 'male', 'FuDan'); 
    INSERT INTO teacher VALUES('t02' , 'LiLiang', 'male', 'WHU'); 
    INSERT INTO teacher VALUES('t03' , 'WangGang', 'male', 'Tongji');
    

  6. Query the teacher table.

    1
    SELECT * FROM teacher;
    

  7. Use UNION (combine and deduplicate) to obtain the schools of students and teachers and sort the schools in ascending order by initial letter of the school name.

    1
    2
    3
    4
    5
    6
    7
    8
    SELECT t.school  FROM (
         SELECT sschool AS school
          FROM student
          UNION
          SELECT tschool AS school
          FROM teacher
      ) t
      ORDER BY t.school ASC;
    

    Information similar to the following is displayed.

  8. Use UNION ALL (combine without deduplication) to obtain the schools of all students and teachers and sort the schools by initial letter of the school name in ascending order.

    1
    2
    3
    4
    5
    6
    7
    8
    SELECT t.school  FROM (
         SELECT sschool AS school
          FROM student
          UNION ALL
          SELECT tschool AS school
          FROM teacher
      ) t
      ORDER BY t.school ASC;
    

  9. Use UNION ALL (combine the result sets of SQL statements with WHERE clause) to get all information about students and teachers from "Tongji' and sort by student and teacher number in ascending order.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    SELECT t.*  FROM  (
      SELECT Sid AS id,Sname AS name,Sgender AS gender,Sschool AS school
      FROM student
      WHERE Sschool='Tongji'
      UNION ALL
      SELECT Tid AS id,Tname AS name,Tgender AS gender,Tschool AS school
      FROM teacher
      WHERE Tschool='Tongji'
    ) t
      ORDER BY t.id ASC;
    

Summary

In actual service scenarios, pay attention to the following points when using UNION and UNION ALL:

  • The number of SQL fields and field types on the left and right sides must be the same.
  • Check whether data deduplication (deduplication before combination or during combination) is needed based on service requirements.
  • Based on the data volume, valuate the SQL execution efficiency and determine whether to use temporary tables.
  • Select UNION or UNION ALL wisely and consider the complexity when writing SQL statements.

Usamos cookies para aprimorar nosso site e sua experiência. Ao continuar a navegar em nosso site, você aceita nossa política de cookies. Saiba mais

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback