How to perform basic queries with `SELECT` in PostgreSQL
SELECT command is the primary way to query and read information about records stored within database tables within PostgreSQL. Its usefulness, however, is not restricted to read-only operations. The
SELECT syntax is combined with many other commands to target specific records or fields within databases for updates, deletions, and more complex operations.
In this guide, we'll show how the basic syntax of
SELECT supports gathering data from tables. While we'll leave the vast number of optional clauses to the command for other articles, it will hopefully become evident how even the most basic components provide a strong foundation for querying data. These fundamentals only require you to learn a few clauses and constructions.
The basic format of a
SELECT command looks something like this:
SELECT <column_names> FROM <table_name> <additional_conditions_and_formatting>;
This statement is composed of a few different pieces:
SELECTcommand itself. This SQL statement indicates that we want to query tables or views for data they contains. The arguments and clauses surrounding it determine both the contents and the format of the output by defining criteria.
SELECTstatement can return entire rows (indicated by the
*wildcard character) or a subset of the available columns. If you want to output only specific columns, provide the column names you'd like to display, separated by commas.
FROM <table_name>: The
FROMkeyword is used to indicate the table or view that should be queried. In most simple queries, this consists of a single table that contains the data you're interested in.
<additional_conditions_and_formatting>: A large number of filters, output modifiers, and conditions can be specified as additions to the
SELECTcommand. You can use these to help pinpoint data with specific properties, modify the output formatting, or further process the results.
The column specification portion of the
SELECT command requires you to name the columns you want to display for the data you are querying.
For ad hoc querying and during data exploration, one of the most helpful options is to use an asterisk to indicate that you want to display values from every column available:
SELECT * FROM my_table;
This will display all of the records from
my_table since we do not provide any filtering to narrow the results. All of the columns for each record will be shown in the order that they are defined within the table.
You can also choose to view a subset of available column by specifying them by name. Column names are separated by commas and are displayed in the order in which they are given:
SELECT column2, column1 FROM my_table;
This will display all of the records from
my_table, but only show the columns named
column1, in that order.
You can optionally set column aliases to modify the name used for columns in the output.
SELECT column1 AS "first column" FROM my_table;
This will show the each of the values for
my_table. However, the column in the output will be labeled as
first column instead of
This is especially useful if the output combines column names from multiple tables that might share names or if it includes computed columns that don't already have a name.
ORDER BY clause can be used to sort the resulting rows according to the criteria given. The general syntax looks like this:
SELECT * FROM my_table ORDER BY <sort_expression>;
This will display the values for all columns in all records within
my_table. The results will be ordered according to the expression represented by the placeholder
For example, suppose we have a
customer table that contains columns for
phone_number. If we want to display the results in alphabetical order by
last_name, we could use the following command:
SELECT * FROM customer ORDER BY last_name;
The results are sorted in ascending alphabetical order by the
To reverse the ordering, we can add the
DESC modifier to the end of the
ORDER BY clause:
SELECT * FROM customer ORDER BY last_name DESC;
You can also sort by multiple columns. Here, we sort first by
last_name, and then by
first_name for any columns with the same
last_name value. Both sorts are in ascending order:
SELECT * FROM customer ORDER BY last_name, first_name;
One additional option that is often important is clarifying where
NULL values should be presented in the sort order. You can do this by adding
NULLS FIRST (the default) or
NULLS LAST for any sort column:
SELECT * FROM customer ORDER BY last_name NULLS LAST;
You can sort your results with Prisma Client in much the same way as you would in an SQL query.
If you want to find the range of values for a column in PostgreSQL, you can use the
SELECT DISTINCT variant. This will display a single row for each distinct value of a column.
The basic syntax looks like this:
SELECT DISTINCT column1 FROM my_table;
This will show one row per unique value in
For example, to display all of the different values for
color that your
shirt table contains, you can type:
SELECT DISTINCT color FROM shirt;
To show uniqueness across multiple columns, you can add additional columns separated by commas.
For instance, this will display all of the different combinations of
shirt_size for the
SELECT DISTINCT color,shirt_size FROM shirt;
color | shirt_size-------+-----------blue | Mblue | Sgreen | Mgreen | Lgreen | Sorange | Lorange | Mred | Myellow | S
This displays every unique combination of
shirt_size within the table.
An often more flexible variant is PostgreSQL's
SELECT DISTINCT ON command. This format allows you to specify a list of columns that should be unique in combination and separately list the columns you wish to display.
The general syntax looks like this, with the column or columns that should be unique listed in the parentheses after
SELECT DISTINCT ON, followed by the columns you wish to display:
SELECT DISTINCT ON (column1) column1, column2 FROM my_table ORDER BY column1;
For example, if you want to display a single color for each shirt size, you could type:
SELECT DISTINCT ON (shirt_size) color,shirt_size FROM shirt;
color | shirt_size------+-----------red | Mgreen | Lgreen | S
This will show a single row for each unique value in
shirt_size. For each row, it will display the
color column, followed by the
If using an
ORDER BY clause, the column selected for ordering must match the column selected within the
DISTINCT ON parentheses for the output to have predictable results:
SELECT DISTINCT ON (shirt_size) color,shirt_size FROM shirt ORDER BY shirt_size DESC;
color | shirt_size------+-----------green | Sred | Mgreen | L
You can filter duplicate rows from your query with Prisma Client by using the distinct functionality.
In this guide, we covered some of the basic ways you can use the
SELECT command to identify and display records from your tables and views. The
SELECT command is one of the most flexible and powerful operations within SQL-oriented databases, with many different ways to add clauses, conditions, and filtering.
While we only covered basic usage in this guide, the general format you learned here will serve as the foundation for all other read and many write queries. Learning ways to filter and target the results more accurately extend the capabilities that we covered today.