Your First Query
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 thestudentstable.- 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 BYto 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:
SELECT * FROM students; 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:
| Part | Meaning |
|---|---|
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:
| Part | What it tells you |
|---|---|
| Column names | The first line: one name per column, in the order you asked for them |
| Line of dashes | Separates the names from the data; the + signs mark the column borders |
| Rows | One line per row, with values lined up under their column names |
| Alignment | Numbers are lined up on the right (101, 12) and text on the left (Srihitha) |
NULL | No 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:
SELECT first_name, city, state
FROM students
ORDER BY student_id; first_name | city | state
------------+---------------+-------
Srihitha | San Francisco | CA
Advitha | San Francisco | CA
Navika | Dallas | TX
(3 rows)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.
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.)
A Misspelled Table Name
The table is called students, with an s:
SELECT * FROM student;ERROR: relation "student" does not existIn 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
SELECT frist_name FROM students;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:
SELECT first_name
FROM students
WHERE state = CA;ERROR: column "ca" does not existThere 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':
SELECT first_name
FROM students
WHERE state = 'CA'
ORDER BY first_name; 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
SELECT *
FROM courses
ORDER BY course_id; 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.
Key Takeaways
| You want to | Write |
|---|---|
| See every column of a table | SELECT * FROM students; |
| See only some columns | SELECT first_name, city FROM students; |
| Choose the row order | ... ORDER BY student_id; |
| Compare with a text value | WHERE state = 'CA' |
Was this tutorial helpful?