Basic SQL Manual
Basic SQL Manual
NIRMAUNIVERSITY
INSTITUTE OF TECHNOLOGY
SEMSTER V
2CS402 – DATABASE MANAGEMENT SYSTEM
Lab Manual
What is SQL?
SQL (Structured Query Language) is a powerful language that is used for retrieving or
modifying data from the database. It is designed such that this can be also accessed by non-
programmer.
IBM devloped the original version of SQL called as Sequel as a part of the system are developed
in the early 1970s. Since them, it has evolved & now called Structured Query language. SQL has
clearly established itself as the Standard relational database language.
SQL*PLUS is an ORACLe tool which accepts SQL, SQL commands and executes them.
DML group can be further broken down into the following statements:
SELECT, which retrieves data
INSERT, which adds data to the database
UPDATE, which modifies existing data from the database
DELETE, which removes data from the database
DDL statements can be used to create user and restructure database objects. The following list
are DDL commands:
CREATE TABLE
ALTER TABLE
DROP TABLE
CREATE INDEX
DROP INDEX
DCL commands are used to create objects related to user access and privileges. Here are some
common DCL commands:
ALTER PASSWORD
GRANT
REVOKE
CREATE SYNONYM
Monika Shah 1
SQL Lab Manual
DDL and DCL are in the realm of the DBA (DataBase Administration) or for the privilege user.
Monika Shah 2
SQL Lab Manual
OUTPUT :
3. FROM emp
If the command is broken into multiple lines, make the line which is to be edited, the
current line
Type N at the SQL> prompt to make line no N to make as current line
Example : To make line 1 as current line
SQL > 1
OUTPUT :
1* SELECT *
Monika Shah 3
SQL Lab Manual
Monika Shah 4
SQL Lab Manual
Monika Shah 5
SQL Lab Manual
Basic Datatypes
Datatype Description Length limitation
Char(n) Fixed length character string. Note that 255(2000 in oracle)
a string of type char is always padded
on right with blanks to full length of n.
Monika Shah 6
SQL Lab Manual
SQL COMMANDS
Where ,
The asterisk (*) in the SELECT statement is used to denote that all columns in the table.
WHERE clause used to filter out the records satisfying the specified criteria
Where clause - Comparators
• <, >, =, <>, >=, <=
– Compare two values as expected
– Operates on numbers as well as text values
• LIKE
– Compare a text value with a pattern
– ‘%’ compares with zero or more characters
– ‘_’ compares with exactly one character
For, E.g.
Select * from student Where Age>=20; //List the students atleast 20 yr. older
Select * from student Where name like 'A%' //Students having A as first character.
START WITH clause is used to start fetching the data from the record, which satisfies the
specified criteria.
E.g. SELECT ename FROM emp CONNECTED BY prior id = mgr_id START WITH
id=101
Monika Shah 7
SQL Lab Manual
The query writer can assign new, different names to the attributes or columns that appear in a
query answer with the AS <new attribute name> clause. Use the AS operator to name the
result attributes for formulas.
This is an only statement that enables you to add new record to an existing table.
The delete statement is used to remove the record from the table.
Monika Shah 8
SQL Lab Manual
Example:
RNO CHAR(8)
NAME VARCHAR2(30)
CITY VARCHAR2(20)
DOB DATE
PROGRAM CHAR(3)
HSC NUMBER(5,2)
JEE NUMBER(3)
Types of SubQuery
Correlated
The subquery mentions an attribute from a table in the outer query.
The subquery executes as many times as many records are in outer Query.
E.g.
SELECT S.Number, S.Name
FROM Salesperson S
WHERE S.Number IN
(SELECT C.SalespersonNum
FROM Customer C
WHERE C.Name = S.Name);
Non-correlated
The subquery only uses attributes from the the table mentioned in the subquery.
The subquery executes once only.
E.g.
SELECT C1.Number, C1.Name
FROM Customer C1
Monika Shah 9
SQL Lab Manual
WHERE C1.CRating IN
(SELECT MAX (C2.CRating)
FROM Customer C2);
Operator JOINS
JOINS
There are a number of join types that can be
expressed in the FROM clause:
These are syntactic sugar
– inner join (the regular join)
… these joins can be
– cross join
expressed in a basic
– natural join
SELECT..FROM..WHERE ..query
– left outer join
– right outer join
– full outer join There are new operators
– union join … these joins can only be
expressed in a complex
SQL query involving the
union operator.
Monika Shah 10
SQL Lab Manual
Monika Shah 11
SQL Lab Manual
CROSS JOIN
A “CROSS JOIN” is simply a cross product
E.g.
These two queries are equivalent:
(1)
SELECT A,T1.B, C,D
FROM Customer T1, Salesperson T2
Where T1.B=T2.B;
(2)
SELECT C.Name, S.Name
FROM Customer C, Salesperson S WHERE C.SalespersonNum = S.Number AND
C. CreditRating < 6;
E.g.
These queries are equivalent.
(1)
SELECT C.Name, S.Name
FROM Customer C JOIN
(SELECT Name, Number AS SalespersonNum FROM Salesperson ) S
ON SalespersonNum;
(2)
SELECT C.Name, S.Name
FROM Customer C INNER JOIN
(SELECT Name, Number AS SalespersonNum FROM Salesperson) S
Monika Shah 12
SQL Lab Manual
ON SalespersonNum;
Natural Join
NATURAL JOIN requires equal values for all corresponding columns from the two tables that
have the same name (or you can list the column name for the join) in the ON clause.
E.g.
These queries are equivalent.
(1)
SELECT C.Name, S.Name
FROM Customer C, Salesperson S
WHERE C.SalespersonNum = S.Number;
(2)
SELECT C.Name, S.Name
FROM Customer C NATURAL JOIN
(SELECT Name, Number AS SalespersonNum From SalesPerson) S
ON SalespersonNum;
OUTER JOIN
Monika Shah 13
SQL Lab Manual
OUTER JOIN
(2)
SELECT C.Name, S.Name
FROM Customer C, Salesperson S
Where C.SalespersonNum(+) = S.SalespersonNum;
(2)
SELECT C.Name, S.Name
FROM Customer C, Salesperson S
Where C.SalespersonNum = S.SalespersonNum(+);
(2)
SELECT C.Name, S.Name
FROM Customer C, Salesperson S
Where C.SalespersonNum(+) = S.SalespersonNum(+);
UNION JOIN
Not really a join ... tuples from the two tables are not matched.
UNION JOIN = FULL OUTER JOIN - INNER JOIN
E.g.
These two queries are equivalent.
(1)
Monika Shah 14
SQL Lab Manual
(2)
(SELECT C.Name, S.Name
FROM Customer FULL OUTER JOIN Salesperson ON
SalespersonNum;)
MINUS
(SELECT C.Name, S.Name
FROM Customer JOIN Salesperson ON
SalespersonNum;);
Note: Refer Oracle Documentation for detail syntax help for any SQL command
Monika Shah 15