Skip to main content

Syntax

Where from_item can be:

Parameters

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.

Qualified wildcard on a schema-qualified relation

If the FROM clause uses a schema-qualified relation without an alias, you can still reference all of its columns with table_name.*.

ORDER BY with a repeated scalar subquery expression

You can use a scalar subquery inside an ORDER BY expression, including when the same expression already appears in the SELECT list.

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”.