Basics of SQL
Basics of SQL
These systems allow users to create update, and extract information from their
databases.
Compared to a manual filing system, the biggest advantages to a computerized
database system are speed, accuracy, and' accessibility.
One of the reasons behind the success of relational model is its simplicity. It is easy to
understand the data and easy to manipulate.
Another important advantage with relational model, compared with remaining two
models is, it doesnt bind data with relationship between data item. Instead it allows
you to have dynamic relationship between entities using the values of the columns.
Almost all Database systems that are sold in the market, now- a-days, have either
complete or partial implementation of relational model.
Figure 1 shows how data is represented in relational model and what are the terms
used to refer to various components of a table. The following are the terms used in
relational model.
Table name
STUDENTS
Rollno
Name
Column name
Phone
s1
Louis Figo
454333
s2
Raul
656675
s3
Roberto Carlos
546782
s4
Guti
567345
Tuple / Row
Table / Relation
Attribute / Column
Tuple / Row: A single row in the table is called as tuple. Each row represents the data
of a single entity.
Attribute / Column: A column stores an attribute of the entity. For example, if details
of students are stored then student name is an attribute; course is another attribute and
so on.
Column Name: Each column in the table is given a name. This name is used to refer
to value in the column.
Table Name: Each table is given a name. This is used to refer to the table. The name
depicts the content of the table.
you need to concentrate on what you want, not on how you get it. Put it in other way,
you need not be concerned with procedural details.
SQL Commands are divided into four categories, depending upon what they do.
DDL (Data Definition Language)
DML (Data Manipulation Language)
DCL (Data Control Language)
Query (Retrieving data)
DDL commands are used to define the data. For example, CREATE TABLE.
DML commands such as, INSERT and DELETE are used to manipulate data.
DCL commands are used to control access to data. For example, GRANT.
Query is used to retrieve data using SELECT.
DML and Query are also collectively called as DML. And DDL and DCL are
called as DDL.
ORACLE
Password
system
manager
sys
change_on_install
Scott
tiger
Demo
demo
Datatypes
Each column of the table contains the datatype and maximum length, if it is length is
applicable. Datatype of the column specifies what type of data can be stored in the
column.
The datatype VARCHAR2 is to store strings that may have different number of
characters, NUMBER is used to store numbers. The maximum length, which is given
in parentheses after the datatype, specifies how many characters (or digits) the column
can store at the most. For example, column VARCHAR2 (20) would mean it can store
up to 20 characters.
Table-1 lists out datatypes available in Oracle8i along with what type of data can be
stored and maximum length allowed.
DATATYPE
DESCRIPTION
VARCHAR2( len)
VARCHAR(len)
characters.
Same as VARCHAR2. But use VARCHAR2 as Oracle
CHAR(len)
NUMBER
NUMBER (p ,s)
sign.
P represents the maximum significant digits allowed. S is
DATE
Null? Type
NAME
VARCHAR2(30)
DURATION
FEE
PREREQUISITE
NUMBER(3)
NUMBER(5)
VARCHAR2(100)
While inserting rows, you may enter value for each column of the table or selected columns.
Note: After inserting the required row, issues COMMIT command to make sure the changes are made
permanent. We will discuss more about COMMIT command later in this book but for the time being it is
sufficient to know that COMMIT command will make changes permanent. Without COMMIT, rows that are
inserted might be lost if there is any power failure.
During insertion, character values are enclosed in single quotes. Unless otherwise specified we have to
supply a value for each column of the table. If the value of any column is not known or available then you
can give NULL as the value of the column.
For example, the following insert will insert a new row with null value for PREREQUISITE column.
insert into courses
values('c','C Programming',25,3000,null);
Note: INSERT command can insert only one row at a time. For multiple row, INSERT command must be
issued for multiple times.
DATE type values must be in the format DD-MON-YY or DD-MON-YYYY, where MON is the first three letters
of the month (Jan, Feb). If only two digits are given for year then current century is used. For example, if
you give 99 for year, Oracle will take it as 2099 as the current century is 2000.
remember this and give four digits if required.
subquery }
We will see how to insert row into a table using a subquery later in this book.
So it is important to
The following INSERT command will insert a new row only two values.
The above command will create a new row in COURSES table with values for only two columns CCODE
and NAME. The remaining columns will take NULL value or the default value, if the column is associated
with default value. We will discuss more about default value in the next chapter.
NULL value
Null value means a value that is not available or not known. When a columns value is not known then we
store NULL value into the column. NULL value is neither 0 nor blank nor any other known value. We have
already seen how to store null value into a column and when Oracle automatically stores null value into a
column. We will discuss more about how to process null value later in this chapter.
The following is the syntax of SELECT command. The syntax given here is incomplete. For complete
syntax, please refer to online documentation.
[alias ]
[alias ]
] ...
[schema.]object
[, [schema.]object ] ...
[WHERE condition]
[ORDER BY {expr|position} [ASC | DESC]
[, {expr|position} [ASC | DESC]] ...]
schema is the name of the user whose table is being accessed. Schema prefix is not required if the table is
in the current account. Schema prefix is required while we are accessing a table of some other account
and not ours.
The following is an example of a basic SELECT command.
select
* from courses;
CCODE NAME
DURATION
FEE PREREQUISITE
Oracle database
25
4500 Windows
vbnet VB.NET
30
C programming
20
asp
ASP.NET
25
java
Java Language
25
4500 C language
xml
XML Programming
15
Projection
Projection is the operation where we select only a few columns out of the available columns. The following
is an example of projection.
select name,fee from courses;
NAME
FEE
4500
VB.NET
5500
C programming
3500
ASP.NET
5000
Java Language
4500
XML Programming
4000
FEE
FEE*0.15
4500
675
VB.NET
5500
825
C programming
3500
525
ASP.NET
5000
750
Java Language
4500
675
XML Programming
4000
600
Column Alias
The column heading of an expression will be the expression itself. However, as it may not
be meaningful to have expression as the result of column heading, we can give an alias to
the column so that alias is displayed as the column heading.
The following example will use alias DISCOUNT for the expression FEE * 0.15.
select name, fee, fee * 0.15 DISCOUNT from courses
NAME
FEE
DISCOUNT
4500
675
VB.NET
5500
825
C programming
3500
525
ASP.NET
5000
750
Java Language
4500
675
XML Programming
4000
600
The following are the arithmetic operators that can be used in expressions.
Operator
+
*
/
Description
Add
Subtract
Multiply
Divide
ORDER BY clause
It is possible to display the rows of a table in the required order using ORDER BY clause. It is used to sort
rows on the given column(s) and in the given order at the time of retrieving rows. Remember, sorting
takes place on the row that are retrieved and in no way affects the rows in the table. That means the order
of the rows will remain unchanged.
Note: ORDER BY must always be the last of all clauses used in the SELECT command.
The following SELECT command displays the rows after sorting rows on course fee.
NAME
FEE
3500
XML Programming
4000
Oracle database
4500
Java Language
4500
ASP.NET
5000
VB.NET
5500
Note: Null values are placed at the end in ascending order and at the beginning in descending order.
The default order for sorting is ascending. Use option DESC to sort in the descending
order. It is also possible to sort on more than one column.
To sort rows of COURSES table in the ascending order of DURATION and descending order of FEE, enter:
select
order by
NAME
DURATION
FEE
15
4000
C programming
20
3500
ASP.NET
25
5000
Oracle database
25
4500
Java Language
25
4500
VB.NET
30
5500
First, all rows are sorted in the ascending order of DURATION column. Then the rows that have same value
in DURATION column will be further sorted in the descending order of FEE column.
For example, the following SELECT sorts rows based on discount to be given to each
course.
select
from courses
order by 3;
NAME
FEE
FEE*0.15
3500
525
XML Programming
4000
600
Oracle database
4500
675
Java Language
4500
675
ASP.NET
5000
750
VB.NET
5500
825
Note: Column position refers to position of the column in the selected columns and not the position of the
column in the table.
The above command uses column position in ORDER BY clause. Alternatively you can use column alias in
ORDER BY clause as follows:
select
from courses
order by discount;
NAME
FEE
DISCOUNT
3500
525
XML Programming
4000
600
Oracle database
4500
675
Java Language
4500
675
ASP.NET
5000
750
VB.NET
5500
825
Selection
It is possible to select only the required rows using WHERE clause of SELECT command. It implements
selection operator of relational algebra.
WHERE clause specifies the condition that rows must satisfy in order to be selected.
The following
FEE
-------------------- --------VB.NET
5500
ASP.NET
5000
The following relational and logical operators are used to form condition of WHERE clause. Logical
operators AND, OR are used to combine conditions.
If condition returns true, NOT will make the overall condition false.
Operator
=
!= or <>
>=
<=
>
<
AND
OR
NOT
Meaning
Equal to
Not equal to
Greater than or equal to
Less than or equal to
Greater than
Less than
Logical ANDing
Logical Oring
Negates result of condition.
The following SELECT command displays the courses where duration is more than 15 days and course fee
is less than 4000.
select
* from courses
DURATION
FEE PREREQUISITE
C programming
20
The following SELECT command retrieves the details of course with code ORA.
DURATION
FEE PREREQUISITE
Oracle database
25
4500 Windows