Thursday, July 16, 2009

learn simple sql command using example data base-select clause

A Part of a Bank Database


The select Clause
- The select clause list the attributes desired in the result of a query (corresponds to the projection operation of the relational algebra)
- E.g. find the names of all branches in the loan relation
select branch_name
from loan
- In the “pure” relational algebra syntax, the query would be:
∏branch-name(loan)
- SQL does not permit the ‘-’ character in names,
• Use, e.g., branch_name instead of branch-name in a real implementation
- SQL names are case insensitive, i.e. you can use capital or small letters.

- The general form of the SELECT clause
Select [distinct] attribute (s)
from table (s)
[where condition]
[group by attributes (s)]
[having condition]
[order by attributes (s)]
- SQL allows duplicates in relations as well as in query results
- To force the elimination of duplicates, insert the keyword distinct after select

- e.g Find the names of all branches in the loan relations, and remove duplicates
select distinct branch_name
from loan
- The keyword all specifies that duplicates not be removed.
select all branch_name
from loan

- An asterisk in the select clause denotes “all attributes”
select *
from loan
- The select clause can contain arithmetic expressions involving
the operation, +, –, *, and /, and operating on constants or
attributes of tuples
- The query:
select loan_number, branch_name, amount * 100
from loan
would return a relation which is the same as the loan relations,
except that the attribute amount is multiplied by 100

No comments:

Post a Comment