Understanding Tables and Data

Before writing queries, let's understand how data is organized in PostgreSQL.

Tables are Like Spreadsheets

Think of a database table like a well-organized spreadsheet:

employees table:
+----+----------------+-------------+-----------+------------+
| id | name           | department  | salary    | hire_date  |
+----+----------------+-------------+-----------+------------+
| 1  | Alice Johnson  | Engineering | 95000.00  | 2020-03-15 |
| 2  | Bob Smith      | Marketing   | 72000.00  | 2019-07-22 |
| 3  | Carol Williams | Engineering | 98000.00  | 2018-11-01 |
+----+----------------+-------------+-----------+------------+
  • Each column has a name and a data type
  • Each row is one record (one employee)
  • Each row has a unique id (primary key)

Common Data Types

Type Description Example
INTEGER Whole numbers 42, 1000
SERIAL Auto-incrementing integer 1, 2, 3...
VARCHAR(n) Variable-length text (max n chars) 'Alice Johnson'
TEXT Unlimited text 'A long description...'
NUMERIC(p,s) Exact decimal (p digits, s after decimal) 95000.00
BOOLEAN True or false true, false
DATE Calendar date '2020-03-15'
TIMESTAMP Date and time '2020-03-15 09:30:00'
UUID Universally unique identifier 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'

The Employees Table

Your sandbox has an employees table with 8 records. Try running this query in the editor:

SELECT * FROM employees;

This will show all columns (* means "everything") from the employees table.

Primary Keys

Every table should have a primary key - a column (or combination of columns) that uniquely identifies each row. In our employees table, the id column is the primary key.

Primary keys must be:

  • Unique - no two rows can have the same value
  • Not null - every row must have a value

Try It Yourself

Use the SQL editor on the right to explore the employees table. Try these queries:

-- See all employees
SELECT * FROM employees;

-- Count how many employees there are
SELECT count(*) FROM employees;
Take Quiz Next Lesson
SQL Editor Ctrl+Enter to run
Results
Run a query to see results here
Tables: