Updated on 2025-05-29 GMT+08:00

Simple Queries

A simple query retrieves one or more columns of data from one or more tables or views.

The following example demonstrates a typical simple query operation that retrieves both partial and complete information from a specified table:

-- Create a table and insert data into the table.
gaussdb=# CREATE TABLE student(
    sid INT PRIMARY KEY, 
    class INT, 
    name  VARCHAR(50),
    gender INT CHECK(gender = 0 OR gender = 1) -- Gender. 1: male; 0: female
);
gaussdb=# INSERT INTO student (sid, class, name, gender) VALUES (1, 1, 'michael', 0);
gaussdb=# INSERT INTO student (sid, class, name, gender) VALUES (2, 2, 'bob', 1);
gaussdb=# INSERT INTO student (sid, class, name, gender) VALUES (3, 2, 'gary', 0);

-- Query some columns.
gaussdb=# SELECT sid, name FROM student;
 sid |  name   
-----+---------
   1 | michael
   2 | bob
   3 | gary
(3 rows)

-- Query all columns.
gaussdb=# SELECT * FROM student;
 sid | class |  name   | gender
-----+-------+---------+-----
   1 |     1 | michael |   0
   2 |     2 | bob     |   1
   3 |     2 | gary    |   0
(3 rows)

-- Specify the column alias.
gaussdb=# SELECT sid student_id, name FROM student;
 student_id |  name   
------------+---------
          1 | michael
          2 | bob
          3 | gary
(3 rows)

-- Drop.
gaussdb=# DROP TABLE student;