ASA Getting Started
Databases and Applications
SQL and database computing
The "Q" in "SQL" stands for query. You query (or retrieve) data from a database with the SELECT statement. The basic query operations in a relational system are projection, restriction, and join. The SELECT statement implements all of them.
A projection is a subset of the columns in a table. A restriction (also called selection) is a subset of the rows in a table, based on some conditions.
For example, the following SELECT statement retrieves the names and prices of all products that cost more than $15:
SELECT name, unit_price FROM product WHERE unit_price > 15
This query uses both a projection (SELECT name, unit_price
) and a restriction (WHERE unit_price > 15
).
For more information, see Selecting rows from a table.
A join links the rows in two or more tables by comparing the values in columns of each table. For example, you might want to select the order item identification numbers and product names for all order items that shipped more than a dozen pieces of merchandise:
SELECT sales_order_items.id, product.name FROM product JOIN sales_order_items WHERE sales_order_items.quantity > 12
The product table and the sales_order_items table are joined together based on the foreign key relationship between them.
For more information, see the chapter Selecting Data from Multiple Tables.