← coderrocketfuel.com

Only Return Certain Table Columns in a SELECT PostgreSQL Query

When you make a SELECT query to a PostgreSQL database, how can you limit which columns from each row are returned in the query data?

As an example, let's say you have a query where you're returning all table columns like this:

SELECT * FROM table WHERE id = '1';

In that query, * is a shorthand for "all columns".

If you want to only return specific columns, replace the * symbol with the names of the columns you want to be returned.

SELECT id, username, created FROM table WHERE id = '1';

Using that query, only data for the id, username, and created columns would be returned.

Thanks for reading and happy coding!