Basic Structure - SQL is based on set and relational operations with certain modifications and enhancements
- A typical SQL query has the form:
select A1, A2, ..., An from r1, r2, ..., rm where PAis represent attributes
ris represent relations
P is a predicate.
-This query is equivalent to the relational algebra (to be discussed later) expression ∏A1, A2, ..., An(σP (r1 x r2 x ... x rm))
- The result of an SQL query is a relation.
Schema Used in Examples - A Part of a Bank

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 loanThe select clause can contain arithmetic expression involving the operation +,-,*, and/,and
operating on constants or
attributes of tuples
- The query:
select loan_number,branch-number,amount*100 from loanwould return a relation which is the same as the loan relations,
except that the attribute amount is multiplied by 100
The where Clause - The where clause specifies conditions that the result must satisfy (corresponds to the selection predicate of the relational algebra)
- e.g Get the customer names for customers in kandy
select customer_name from customer where customer_city=‘kandy’-To find all loan number for loans made at the ‘colombo street-kandy’
with loan amounts greater than Rs.100000
select loan_number from loan where branch_name = ‘colombo street-kandy’ and amount >100000
- Comparison results can be combined using the logical connectives and, or, and not.
- Comparisons can be applied to results of arithmetic expressions.
The from Clause - The from clause lists the relations involved in the query (corresponds to the Cartesian product operation of the relational algebra)
- Find the Cartesian product borrower x loan
select * from borrower, loan- Find the name, loan number and loan amount of all customers having a loan at the ‘colombo street-kandy’
select borrower.customer_name, borrower.loan_number, loan.amount from borrower, loan where borrower.loan_number = loan.loan_number and loan.branch_name = ‘colombo street-kandy’The Rename Operation - The SQL allows renaming relations and attributes using the as
clause: old_name as new_name
- e.g Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id.
select borrower.customer_name, borrower.loan_number as loan_id, amount from borrower, loan where borrower.loan_number = loan.loan_numberTuple Variables - Tuple variables are defined in the from clause via the use of the as clause.
- e.g. Find the customer names and their loan numbers for all customers having a loan at some branch
select distinct T.branch_namefrom branch as T, branch as Swhere T.assets > S.assets and S.branch_city = ‘kandy’- e.g. Find the names of all branches that have greater assets than some branch located in kandy
select customer_name, T.loan_number, S.amountfrom borrower as T, loan as Swhere T.loan_number = S.loan_numberString Operations - SQL includes a string-matching operator for comparisons on character strings. Patterns are described using two special characters:
asterisk ( * ). The * character matches any substring.
question ( ? ). The ? character matches any character.
- Find the names of all customers whose street includes the sub string “Main”.
select customer_namefrom customerwhere customer_street like ‘*Main*’Ordering the Display of Tuples - List in alphabetic order the names of all customers having a loan in branch ‘colombo street-kandy’
select distinct customer_namefrom borrower, loanwhere borrower loan_number = loan.loan-number andbranch_name = ‘colombo street-kandy’order by customer-name- We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.
E.g. order by customer_name desc