SELECT
Use the SELECT
command to retrieve rows from a table or materialized view. It returns the rows that satisfy the criteria that you specify with the clauses and conditions in your query.
Syntax
Where from_item
can be:
Parameters
Parameter or clause | Description |
---|---|
WITH clause | Provides a way to write supplemental statements for a larger query. For more information, see WITH clause. |
DISTINCT clause | This clause eliminates duplicate rows from the result. SELECT DISTINCT eliminates duplicate rows based on all selected columns. SELECT DISTINCT ON allows you to specify expressions or columns and returns only the first row for each unique combination. It requires the use of the ORDER BY clause to determine the first row, and the DISTINCT ON expression must match the leftmost ORDER BY expression. The ORDER BY clause will normally contain additional expressions that determine the desired precedence of rows within each DISTINCT ON group. In this case, this expression can be an alternative with group topN when “N=1”. See examples of this clause below to know more about it. |
EXCEPT clause | Exclude one or more columns from the result set. By specifying except_column, the query will return all columns in the result set except those specified. |
expression | A column or an expression. |
VALUES clause | This clause generates one or more rows of data as a table expression. For details, see VALUES clause. |
alias | A temporary alternative name for a table or materialized view in a query. |
table_name | A table or materialized view. |
grouping_expression | Values can be:Input column namesInput column expressions without subqueries or correlated columns |
ORDER BY clause | By default, sorting is in ascending (ASC) order, with NULL values treated as the largest. For more information, see ORDER BY clause. |
sort_expression | Values can be:Output column namesOutput column ordinal numbersHidden select expressions |
count_number | The number of results you want to get. |
OFFSET clause | The OFFSET clause can only be used with the LIMIT and ORDER BY clauses. |
(SELECT) | A SELECT command. You must enclose the subquery in parentheses, and specify an alias. When you include a subquery in the FROM clause, the output of the subquery is used as a temporary view that is only valid in the query. |
join_type | Supported join types: [INNER] JOINLEFT [OUTER] JOINRIGHT [OUTER] JOINFULL [OUTER] JOINCurrently, only the ON clause is supported for joins. |
join_condition | Conditions for the ON clause that must be met before the two from_items can be joined. |
window_type | The type of the time window function. Possible values are HOP and TUMBLE. |
interval_expression | The interval expression, in the format of INTERVAL ‘<interval>‘. For example: INTERVAL ‘2 MINUTES’. The standard SQL format, which places time units outside of quotation marks (for example, INTERVAL ‘2’ MINUTE), is also supported. |
FROM clause | Specifies the source of the data on which the query should operate. For more information, see FROM clause. |
GROUP BY clause | Groups rows in a table with identical data, thus eliminating redundancy in the output and aggregates that apply to these groups. For more information, see GROUP BY clause. |
HAVING clause | Eliminates group rows that do not satisfy a given condition. For more information, see HAVING clause. |
LIMIT clause | When the ORDER BY clause is not present, the LIMIT clause cannot be used as part of a materialized view. For more information, see LIMIT clause. |
WHERE clause | Specifies any conditions or filters to apply to your data. For more information, see WHERE clause. |
Examples
DISTINCT
clause
Here is an example of SELECT DISTINCT
. This query will return only the unique combinations of first_name
and last_name
, eliminating any duplicate rows.
Here is an example of SELECT DISTINCT ON
. The query returns the latest order for each unique customer_id
. ORDER BY
is used to ensure that the desired row, that is, the row with the latest order date, appears first; otherwise, the returned row will be unpredictable.
Example of using several clauses
Below are the tables within the same schema that we will be writing queries from.
The table taxi_trips
includes the columns id
, distance
, duration
, and fare
, where id
identifies each unique trip.
The table taxi
includes the columns taxi_id
and trip_id
, where trip_id
and id
in taxi_trips
are matching fields.
The table company
includes the columns company_id
and taxi_id
, where taxi_id
and taxi_id
in taxi
are matching fields.
The following query returns the total distance and duration of trips that are beyond the initial charge ($2.50) of each taxi from the company “Yellow Taxi” and “FabCab”.
Was this page helpful?