Retrieving data from a tables
The
SELECT statement can be used to
retrieve data from any or all columns from a table. Say that you have a table called product, which consists
product name, description, price, and etc. This is the statement to retrieve all the columns from
the table product.
SELECT - specifies
retrieval command plus what to select.
* - stands for
everything. In this case, it stands for every column in the table.
FROM - specifies the table required to retrieve information from.
Now, say that we want to retrieve only the product name, description and
price not everything. Here is how you would accomplish it.
SELECT name, description, price FROM product; |
How about if we want search product by specific chrematistics?. That is
when WHERE clause becomes handy. Where clause is used when only a subset
of all the rows in a table is required. Here is how you would list the
name and the price of the products that has the price value less then $100.
SELECT name, price FROM product WHERE price < 100; |
Here are operators that may be used in the WHERE clause;
= equal to
>, < greater then, less than
<>, !=, ^= not equal to
>=, <= greater then or equal to, less than or equal to.
IS NULL means no value stored in this field.
WHERE price <= 50; will display products selling $50 or less
WHERE price = 50; will display products selling at $50
WHERE description IS NULL; will display products that has no description
WHERE description IS NOT NULL; will display products that has descriptions
There are times you want list items by conditioning two or more
characteristics. For example, if you want list products that are more than
$100 or less then $50. Here is how you would accomplish it.
SELECT *
FROM product WHERE price < 50 OR price > 1000; |
The above statements will display products that are less than $50 or greater
than $1000. Logical operators AND and
OR is used in SQL as we use them in
English language. AND will result true when both conditions are met while
OR will result true when either condition is true.
SELECT name, description, price
FROM product WHERE (name = 'Printer' OR name = 'Scanner') AND price <= 100;
|
The above query will find all the printers and scanners that are less than or
equal $100. Note how single quotations and parentheses are used.
IN operator can be used instead of
OR. Here is equivalent query that uses
IN operator.
SELECT name, description, price
FROM product WHERE name IN ('Printer', 'Scanner')
AND price <= 100; |
When you finding an string value, you cannot sure the correct spelling.
For example Printer can also be printer, PRINTER, or printers. There are
more ways to solve this problem include, changing the column to upper case
characters, or using LIKE statement. The
LIKE operator provides pattern matching for
character data and uses % which means match zero or
more characters and _ which means mach one
character that is position-dependent. Here is an example that uses like
operator.
SELECT * FROM product WHERE name LIKE 'scan%';
|
This will display all the products that start scan. It could be scan, scanner,
scanners, scan camera, etc. To list all the items that has the word scan
in it, we rephrase the statement like this; WHERE name
LIKE '%scan%';.
BETWEEN operator can be used in numeric values to compare column against a
range of inclusive values. This is one example all the products between
$50 and $100 inclusive:
SELECT name, description, price
FROM product WHERE price BETWEEN 50 AND 100;
|
The retrieved data may not be in any specific order unless other ways
specified. Data can be sorted
using ORDER BY clause in the select statement.
SELECT name "Product Name", description "Product
Description", price FROM product ORDER BY price DESC, name ASC; |
This example simply lists product name, description, and the price sorted in
descending order and name in
ascending order. The column aliases display more understandable column
headers instead of actual column names.
Retrieving Calculated fields
You can perform mathematical calculations while retrieving data from tables or views. The math operators for sql are same as those of other languages, like +, -, *, /, etc. Say that you want calculate and display the profit of each item in the product table. The profit is price minus cost. Here is how you would do that:
SELECT product_id, name, price, cost, price-cost
FROM product;
| This example displays list of each product's information plus calculated field that dislays item profit [price-cost]. |
|