You are on page 1of 22

SQL

(Structured Query Language)


Tables Will Be
Used In
Examples
Table: Customer_Bank
Customer FirstName LastName DOB DOE Address
ID
1 Sunil Saha 1/1/1970 1/1/2001 Delhi
2 Hrishikesh Mukarjee 1/4/1972 3/4/2001 Calcutta
3 Sangeeta Rao 5/2/1974 1/1/2001 Delhi
4 Jai Kishan 2/8/1977 1/1/2002 Delhi
5 Aizaz Ahmed 1/1/1978 1/2/2002 Bombay
6 Rupali Saxsena 1/2/1977 1/2/2001 Madras
7 Anup Aggarwal 1/1/1968 1/2/2000 Delhi
8 Gaurav Sharma 1/4/1973 6/7/2001 Agra
9 Satish Agarwal 1/2/1974 12/8/2001 Jaipur
Table: AccountOpening
CustomerID AccountType DOO AccountID

1 Current 4/1/2001 7
1 Saving 8/1/2001 2
2 Current 8/1/2001 3

3 Saving 8/11/2001 4
4 Current 11/18/2001 5
6 Saving 1/1/2001 6
7 Current 5/1/2002 9
7 Saving 6/1/2002 8
Table: Transaction
Account ID Transaction DOT Amount
Type
2 D 1/1/2001 Rs. 2,000.00
2 D 1/5/2001 Rs. 5,000.00
2 W 1/5/2002 Rs. 1,100.00
3 D 1/5/2001 Rs. 2,000.00
3 W 4/5/2002 Rs. 1,300.00
3 D 5/5/2002 Rs. 5,000.00
4 W 6/6/2002 Rs. 1,600.00
4 D 1/1/2002 Rs. 3,000.00
4 W 5/1/2002 Rs. 1,000.00
SELECT Statement
Retrieves rows from the database and allows
the selection of one or many rows or
columns from one or many tables. The full
syntax of the SELECT statement is complex,
but the main clauses can be summarized as:

SELECT select_list
FROM table_source
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
SELECT Clause

Retrieves rows from the database and


allows the selection of one or many rows
or columns from one or many tables.

Syntax:
SELECT [ ALL | DISTINCT ] < select_list >
< select_list > ::=
{*| { table_name | view_name | table_alias }.*
| { column_name | expression | ROWNUM }
[ [ AS ] column_alias ] } [ ,...n ]
FROM Clause
Specifies the table(s) from which to
retrieve rows.

Syntax
FROM { < table_source > } [ ,...n ]

< table_source > ::=


table_name [ [ AS ] table_alias ]
| view_name [ [ AS ] table_alias ] [ ,...n ]
WHERE Clause
Specifies a search condition to
restrict the rows returned.
Syntax
[ WHERE < search_condition >
| < old_outer_join > ]
< old_outer_join > ::=
column_name { * = | = * }
column_name
Search Condition
Syntax
< search_condition > ::=
{ [ NOT] < predicate >
| ( < search_condition > ) }
[ { AND | OR } [NOT] { < predicate >
| ( < search_condition > ) } ]
} [ ,...n ]
Predicate
< predicate > ::=
{
expr { = | < > | ! = | > | > = | ! > | < | < = | ! < } expr
| string_expr [ NOT ] LIKE string_expr
[ ESCAPE 'escape character' ]
| expr [ NOT ] BETWEEN expr AND expr
| expr IS [ NOT ] NULL
| expr [ NOT ] IN ( subquery | expr [ ,...n ] )
| expr { = | < > | ! = | > | > = | ! > | < | < = | ! < }
{ ALL | SOME | ANY} ( subquery )
| EXISTS ( subquery )
}
LIKE Operator
Determines whether or not a given character
string matches a specified pattern

Syntax
match_expression [ NOT ] LIKE pattern
[ ESCAPE escape_character ]

match_expression: String Type Expression


Pattern: Is the pattern to search for in
match_expression, and can include these
valid SQL wildcard characters.

Wildcard Character Description


% Any string of zero or more
characters.
_ (underscore) Any single character.
ESCAPE Clause
Syntax

ESCAPE 'escape_character'
Allows a wildcard character to be searched
for in a character string instead of
functioning as a wildcard.
escape_character is the character that is
placed in front of the wildcard character to
denote this special use.
BETWEEN Operator

Specifies a range to test.

Syntax
test_expression [ NOT ]
BETWEEN begin_expression
AND end_expression
IN Operator
Determines if a given value matches
any value in a subquery or a list.

Syntax
test_expression [ NOT ] IN
(subquery | expression [ ,...n ])
ALL Operator
Compares a scalar value with a single
column set of values.

Syntax
scalar_expression
{ = | <> | != | > | >= | !> | < | <= |
!< } ALL ( subquery )
SOME | ANY Operator
Compares a scalar value with a single
column set of values.

Syntax
scalar_expression { = | < > | ! = |
>|>=|!>|<|<=|!<}
{ SOME | ANY } ( subquery )
EXISTS Operator

Specifies a subquery to test


for the existence of rows.

Syntax
EXISTS subquery
GROUP BY Clause

Specifies the groups into which


output rows are to be placed

[ GROUP BY [ ALL ]
group_by_expression [ ,...n ]
HAVING Clause
Specifies a search condition for a group or
an aggregate. HAVING is usually used with
the GROUP BY clause.

Syntax
[ HAVING < search_condition > ]
ORDER BY Clause

Specifies the sort for the result set.

[ ORDER BY { order_by_expression
[ ASC | DESC ]} [ ,...n] ]

You might also like