Database I
SQL
SELECT Statement
1
SELECT Statement (1/3)
SELECT is the SQL command for querying
(retrieving) data from a database table, view,
or other object etc.
Basic syntax of the SELECT statement in a
textual form (Backus-Naur Form)
SELECT { [alias.]column | expression | [alias.]*
[ , … ] }
FROM [schema.]table [alias];
2
Creating Table (1/3)
Before we start executing the SQL
statements, we need some data and tables
and there by database
Click the Go to Database Home Page
Enter the password for sys user to log in
3
Creating Table (1/3)
Before we start executing the SQL
statements, we need some data and tables
and there by database
Click the Go to Database Home Page
Enter the password for sys user to log in
4
Creating Table (2/3)
Create Student table
by selecting create
menu from object
browser
Define the columns as shown in figure and
click next
5
Creating Table (1/3)
Before we start executing the SQL
statements, we need some data and tables
and there by database
Click the Go to Database Home Page
Enter the password for sys user to log in
6
Creating Table (2/3)
Create Student table
by selecting create
menu from object
browser
Define the columns as shown in figure and
click next
7
Creating Table (3/3)
Click next two times and then finish
Click create to confirm
To insert some sample data, select data from
top menu and then insert row
Insert data for individual columns. Click create
to insert data
8
Different Flavors of SQL Statement
(1/6)
Simple Queries
SELECT * FROM STD;
SELECT RNo, Name, Address FROM STD;
SELECT RNo * 2 "Double RNo", Name FROM STD;
SELECT RNo * 2 DoubleRNo, Name FROM STD;
SELECT [Link], [Link], [Link] FROM STD S;
9
Different Flavors of SQL Statement
(2/6)
SELECT ([Link] + 2) / 10 , [Link], [Link] FROM STD S;
WHERE Clause
Adds conditions that filter out rows
Use various comparison conditions and logical operators
between each filter
Comparison
Operators:
Equi (=),
anti (!=, <>),
range (<, >,
=<, >=)
10
Different Flavors of SQL Statement
(3/6)
SELECT Name, Address FROM STD WHERE RNo = 100;
SELECT * FROM STD WHERE RNo > 102;
SELECT * FROM STD WHERE Name = 'Jamal Khan';
LIKE pattern-matches between strings
% and _ characters are pattern-matching wild card
characters
% is used to representing zero or more characters in a
subset of a string
_ is used to represent one and only one character
11
Different Flavors of SQL Statement
(4/6)
SELECT * FROM STD WHERE Name LIKE '%G%';
SELECT Name, FName FROM STD WHERE
Name LIKE '_a%';
SELECT Name, FName FROM STD WHERE Address IN(
'Peshawar', 'Lahore');
SELECT RNo, Name, FName FROM STD WHERE RNo BETWEEN
100 AND 102;
12