Your First Query

Updated · 10 min read
easyPostgreSQL 18sqlselectbasicserrors

Time to ask the database a question. In this lesson you'll run your first query, learn to read every part of the result, pick the columns you want, and understand the error messages that everyone meets in their first hour of SQL. Every example runs on the Fee Tracker database; press Try it under any of them.

In One Minute

  • SELECT * FROM students; returns every column of every row in the students table.
  • The result is a table: column names, a line of dashes, the rows, and a row count such as (3 rows).
  • List column names instead of * to get only the columns you need: SELECT first_name, city FROM students;
  • Add ORDER BY to choose the order of the rows; without it, the order isn't guaranteed.
  • Error messages are helpful. They name the table or column the database couldn't find, and often suggest a fix.

Run Your First Query

This query asks for everything in the students table. Press Try it and run it:

SQL▶ Try it
SELECT * FROM students;
Output
 student_id | first_name | last_name |             email              |     phone      |     city      | state | grade | mentor_id | joined_on
------------+------------+-----------+--------------------------------+----------------+---------------+-------+-------+-----------+------------
        101 | Srihitha   | Anamaneni | srihitha.anamaneni@example.com | (415) 555-0142 | San Francisco | CA    |    12 |      NULL | 2024-08-19
        102 | Advitha    | Anamaneni | NULL                           | (415) 555-0178 | San Francisco | CA    |    10 |       101 | 2025-01-13
        103 | Navika     | Anamaneni | NULL                           | NULL           | Dallas        | TX    |     7 |       102 | 2025-09-02
(3 rows)

Read it word by word:

PartMeaning
SELECT"Give me..." Almost every query that reads data starts with it
*"...every column..." The star is short for "all columns"
FROM students"...from the students table."
;The end of the statement

That's a complete query. SQL keywords such as SELECT and FROM aren't case-sensitive, so select * from students; works too. Names without quotes are treated as lowercase, so FROM STUDENTS also finds the students table. This course writes keywords in capitals and names in lowercase, which makes queries easier to read.

This first query has no ORDER BY, so the database may return its three rows in any order. You'll add ORDER BY in the next section.

Reading the Result Table

Every result has the same parts. Here they are, from the top:

The query word by word, and every part of its real result
PartWhat it tells you
Column namesThe first line: one name per column, in the order you asked for them
Line of dashesSeparates the names from the data; the + signs mark the column borders
RowsOne line per row, with values lined up under their column names
AlignmentNumbers are lined up on the right (101, 12) and text on the left (Srihitha)
NULLNo value. Advitha and Navika have no email address on file, and Navika has no phone
Row count(3 rows) at the bottom: how many rows the query returned

Choose the Columns You Need

Most of the time, you don't want every column. List the columns you want after SELECT, separated by commas, in the order you want to see them. This query also adds ORDER BY student_id, which sorts the rows by their id:

SQL▶ Try it
SELECT first_name, city, state
FROM students
ORDER BY student_id;
Output
 first_name |     city      | state
------------+---------------+-------
 Srihitha   | San Francisco | CA
 Advitha    | San Francisco | CA
 Navika     | Dallas        | TX
(3 rows)
Pick columns by name; they come back in the order you list them

Notice that the columns come back in the order you listed them, not the order they have in the table. Two habits make queries easier to read, and you'll see them in every example from now on:

  • Put each clause on its own line once a query has more than two parts.
  • End every query that returns several rows with ORDER BY, so the rows always come back in the same order. You'll learn it fully in the lesson on ORDER BY.

Running It in psql

The same query works in psql just as it does in Try it. Start psql with psql -d feetracker (see Setting Up SQL), type the query, end it with a semicolon and press Enter. psql prints the same table, with the same row count; the one visible difference is that it leaves NULL cells blank unless you use \pset null NULL. If the result is wider than your terminal, psql may show it in a pager (press q to get back to the prompt) or let long lines wrap. Widening the window helps, and \x switches to expanded display, which prints each row as a list of column names and values; type \x again to switch back.

Try it shows NULL; psql shows a blank cell until you use \pset null NULL

Reading an Error Message

Everyone makes typing mistakes in SQL. The database never guesses; it stops and tells you what went wrong. Learning to read these three messages will save you hours. (psql often adds a LINE 1: line with a ^ under the spot where it got confused; Try it shows the message, plus any DETAIL and HINT lines.)

The three first errors, with the real messages and the fix

A Misspelled Table Name

The table is called students, with an s:

SQL▶ Try it
SELECT * FROM student;
Output
ERROR:  relation "student" does not exist

In PostgreSQL, a relation means a table (or something that looks like one, such as a view). The message says the database couldn't find a table called student. Check the spelling: \dt in psql lists the real table names.

A Misspelled Column Name

SQL▶ Try it
SELECT frist_name FROM students;
Output
ERROR:  column "frist_name" does not exist
HINT:  Perhaps you meant to reference the column "students.first_name".

The HINT line is PostgreSQL suggesting the column you probably meant. When you see a hint, read it first: it's usually the fix.

Text Without Quotes

Text values in SQL go inside single quotes. Without them, the database thinks you typed a column name:

SQL▶ Try it
SELECT first_name
FROM students
WHERE state = CA;
Output
ERROR:  column "ca" does not exist

There is no column called ca, so the query fails. (The database turned CA into lowercase ca before looking for it, because names without quotes are treated as lowercase.) The fix is to write the value as text, 'CA':

SQL▶ Try it
SELECT first_name
FROM students
WHERE state = 'CA'
ORDER BY first_name;
Output
 first_name
------------
 Advitha
 Srihitha
(2 rows)

Common Mistakes

Forgetting FROM. SELECT first_name; fails with column "first_name" does not exist, because without FROM the database has no table to look in. Every query that reads a table needs FROM table_name.

Putting a comma after the last column. SELECT first_name, city, FROM students; fails with syntax error at or near "FROM". Commas go between columns, not after the last one.

Using double quotes around text. WHERE state = "CA" fails with column "CA" does not exist: double quotes mark a name, and they keep its capitals, so the database looks for a column called CA. Text values use single quotes: 'CA'.

Expecting the same row order every time. Without ORDER BY, the database may return rows in a different order tomorrow. Add ORDER BY whenever the order matters.

Practice

Challenge: List every course in the Fee Tracker database, with all of its columns, sorted by course_id. How many courses are there?

Show solution
SQL▶ Try it
SELECT *
FROM courses
ORDER BY course_id;
Output
 course_id |       title       | technology |    level     | fee_usd | prerequisite_id
-----------+-------------------+------------+--------------+---------+-----------------
         1 | Java Basics       | Java       | Beginner     |  450.00 |            NULL
         2 | Java OOP          | Java       | Intermediate |  500.00 |               1
         3 | Java Collections  | Java       | Intermediate |  550.00 |               2
         4 | SQL Basics        | SQL        | Beginner     |  400.00 |            NULL
         5 | JavaScript Basics | JavaScript | Beginner     |  450.00 |            NULL
         6 | Git Basics        | Git        | Beginner     |  250.00 |            NULL
(6 rows)

There are 6 courses: the row count at the bottom tells you. prerequisite_id is NULL for courses you can take without finishing another one first.

Quick check: Your first query

3 questions
1What does the * in SELECT * FROM courses mean?
2A query fails with: column "ca" does not exist. What is the most likely cause?
3Why add ORDER BY to a query that returns several rows?

Key Takeaways

You want toWrite
See every column of a tableSELECT * FROM students;
See only some columnsSELECT first_name, city FROM students;
Choose the row order... ORDER BY student_id;
Compare with a text valueWHERE state = 'CA'

Was this tutorial helpful?

Lesson 5 of 115