Your first SELECT
Back to the library. The simplest request goes like this: "Bring the names and prices of all menu items." It has two parts: what to bring and where from. In SQL the request looks like this:
SELECT name, price
FROM menu_items; name | price
-------------------+--------
Espresso | 140.00
Americano | 170.00
Cappuccino | 220.00
Latte | 220.00
Flat white | 250.00
Raf | 280.00
Pumpkin latte | 320.00
Green tea | 150.00
Sea buckthorn tea | 230.00
Croissant | 160.00
Cheesecake | 290.00
Syrniki | 310.00
Hot chocolate | 240.00
(13 rows)The DBMS returned 13 rows: one for each menu item. The result has two columns, as we asked. The table's other five columns stayed in the database.
The rows came in the order the DBMS chose: we didn't ask for any other.
The two parts of a query
SELECT and FROM are keywords: words that have a special meaning in SQL. The names of tables and columns are made up by the database's author, while keywords are the same in every query.
SELECT: which columns
After SELECT you list the columns you need in the result, separated by commas. The columns of the result come in the same order as in the list. The query SELECT price, name returns the same 13 rows, but with the price in the first column and the name in the second.
FROM: which table
After FROM you write the table's name. Every column in the SELECT list must be in this table.
The semicolon
The semicolon at the end finishes the query. If the editor holds one query, PostgreSQL runs it without one too, but the habit pays off once there are several queries.
All columns at once
Instead of a list of columns you can put an asterisk *. It means "all columns of the table" in the order they are described:
SELECT * FROM menu_items;This query returns all seven columns of the menu. The asterisk is handy for a quick look at an unfamiliar table. In working queries columns are listed explicitly: that shows which data is needed, and the result doesn't change when someone adds a new column to the table.
Case and line breaks
SQL does not tell uppercase from lowercase letters in keywords and names. This query returns exactly the same table as the first one:
select name, price from menu_items;The convention is to write keywords in uppercase and names in lowercase: that makes a query easier to read. Line breaks and extra spaces change nothing either. A short query goes on one line, and a long one is split so that each part starts on a new line.
Common mistake
A typo in a column name, such as prise instead of price:
SELECT name, prise FROM menu_items; -- ✗ERROR: column "prise" does not existThe DBMS doesn't guess which column you meant and doesn't run the query at all: the answer doesn't even have the name column. The message names the unknown name in quotes, so you spot the typo at once. Take column names from the table's description.
Key points
SELECTlists the columns you need,FROMnames the table.- The columns of the result come in the same order as in the
SELECTlist. SELECT *returns all columns; working queries list them explicitly.- Keywords don't depend on case, but the convention is to write them in uppercase.