The SELECT command is used to search database tables for specific records.
There is always a SELECT and a FROM, but there may or not be a WHERE or ORDER BY
The order is of these lines must be:
SELECT columns FROM tables WHERE conditions ORDER BY sorting
SELECT & FROM lines
Specify which fields are to be displayed, and which tables are to be used
Display all columns
SELECT * FROM orders
Display some columns
SELECT product, customer, quantity FROM orders
WHERE conditions (optional)
This is used to select records from the tables. Conditions are described on the Conditions reference page.
If there is no WHERE line, then all records are displayed
SELECT product, customer, quantity FROM orders WHERE quantity > 100
ORDER BY sorting (optional)
Specify the sorting order of columns
Single Column sort, ascending order
SELECT product, customer, quantity FROM orders ORDER BY quantity ASC
Single Column sort, descending order
SELECT product, customer, quantity FROM orders ORDER BY quantity DESC
Two Column sort
SELECT product, customer, quantity FROM orders ORDER BY quantity DESC, product ASC
Queries across two tables
When a column name appears in both tables, the name of the table must be included (eg customers.customer_id). (line 1)
The primary key and foreign keys of each pair of tables must be connected in the WHERE section (line 3)
SELECT forename, surname, customers.customer_id, product, quantity FROM orders, customers WHERE orders.customer_id = customers.customer_id