AI Hints
Home›PostgreSQL Tutorial›What Is PostgreSQL
PostgreSQL · Lesson 3 of 120

psql: Your First Query

Hello, Postgres.

⏱ 2 min read▶ Video at 1:43🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

You've got a prompt. Let's ask Postgres something. A query starts with a keyword like SELECT, and ends with a semicolon.

  • A prompt
  • SELECT
  • End with ;

Your first SELECT

SELECT can just calculate things. Two plus three, and a piece of text. As gives each column a name. Run it. Postgres answers with a small table, and tells you how many rows.

query.sqlsql
SELECT 2 + 3 AS answer, 'Hello, Postgres' AS greeting;
Output
 answer |    greeting
--------+-----------------
      5 | Hello, Postgres
(1 row)
Also uses _empty
1

Rows from a table

Now real data. This selects the title and price of three books from the books table. Run it. Three books from our shop, Riverside Books.

query.sqlsql
SELECT title, price
FROM books
LIMIT 3;
Output
       title        | price
--------------------+-------
 The Silent Orchard | 14.99
 Code of the River  | 12.99
 Stars Over Lagos   |  9.99
(3 rows)

Summary

SELECT asks Postgres for data, and psql shows the answer as a table. Next, how Postgres organises databases, schemas and tables.

Key takeaways
  • SELECT
  • FROM
  • Semicolon
Next up: Databases, Schemas and Tables — how data is organised