Updated on 2024-06-03 GMT+08:00

Simple Queries

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

-- Create a table and insert data into the table.
gaussdb=# CREATE TABLE student(
    sid INT PRIMARY KEY, 
    class INT, 
    name  VARCHAR(50),
    sex INT CHECK(sex = 0 OR sex = 1) -- Gender. 1: male; 0: female
);
gaussdb=# INSERT INTO student (sid, class, name, sex) VALUES (1, 1, 'Michael', 0);
gaussdb=# INSERT INTO student (sid, class, name, sex) VALUES (2, 2, 'Bob', 1);
gaussdb=# INSERT INTO student (sid, class, name, sex) 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   | sex 
-----+-------+---------+-----
   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)

-- Delete.
gaussdb=# DROP TABLE student;