Cloud

SELECT

The SELECT statement retrieves data from one or more tables. Use SELECT to:

  • Retrieve specific columns from a table.

  • Query data across multiple tables.

  • Filter results based on specific criteria.

Syntax

To retrieve data from a table, use this syntax:

SELECT * FROM default_redpanda_catalog=>table_name;

To filter by specific columns, use:

SELECT column1, column2, ...
FROM default_redpanda_catalog=>table_name;

Where:

  • SELECT: Specifies the data to retrieve.

  • *: Returns all columns.

  • FROM: Specifies the table to query.

  • table_name: The name of the table.

  • column1, column2, …​: The columns to retrieve.

The SELECT statement is case-insensitive. select and SELECT produce the same result.

Examples

The following examples query a table named student_data that contains student records with id, name, and domicile columns.

Query data from all columns

  1. To display all the data from the student_data table, use this syntax:

    SELECT * FROM default_redpanda_catalog=>table_name;
  2. Run the following query:

    SELECT * FROM default_redpanda_catalog=>student_data;
  3. The query returns:

    +--------+----------+----------------+
    | id     | name     | domicile       |
    +--------+----------+----------------+
    | 119291 | Jordan   | Los Angeles    |
    | 119292 | Mike     | Melbourne      |
    | 119293 | Will     | Sydney         |
    +--------+----------+----------------+

Query data from specific columns

  1. To get the list of students' names with their IDs, use this syntax:

    SELECT column_1, column_2 FROM default_redpanda_catalog=>table_name;
  2. Run:

    SELECT id, name FROM default_redpanda_catalog=>student_data;
  3. The query returns:

    +--------+----------+
    | id     | name     |
    +--------+----------+
    | 119291 | Jordan   |
    | 119292 | Mike     |
    | 119293 | Will     |
    +--------+----------+

Query data from a specific column with the condition

  1. With a large amount of data, skimming for the desired data can take a long time. Apply conditions to the SELECT statement to narrow the results:

    SELECT column_1 FROM default_redpanda_catalog=>table_name WHERE condition;
  2. To find the student who lives in Sydney, run:

    SELECT name FROM default_redpanda_catalog=>student_data WHERE domicile='Sydney';
  3. The query returns:

    +----------+
    | name     |
    +----------+
    | Will     |
    +----------+

SELECT DISTINCT

To return only unique rows from the result set, add the DISTINCT keyword after SELECT:

SELECT DISTINCT column1, column2, ...
FROM default_redpanda_catalog=>table_name;

DISTINCT deduplicates rows based on the combined values of all listed columns.

Redpanda SQL does not support SELECT DISTINCT ON (column_list) …​, the PostgreSQL-specific form that keeps the first row for each combination of ON columns.