0% found this document useful (0 votes)
6 views12 pages

Test 4

Uploaded by

govindanm223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Test 4

Uploaded by

govindanm223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

2.

8 OVERVIEW OF THE SQL QUERY LANGUAGE


SQL is the most widely used commercial relational database
language. it was originally developed at IBM’s san jose research
laboratory. This language originally called sequel and its name has
changed to SQL
IBM Sequel language developed as part of System R project at the
IBM San Jose Research Laboratory Renamed Structured Query Language
(SQL)
ANSI and ISO standard SQL:-
 SQL-86
 SQL-89
 SQL-92
 SQL:1999 (language name became Y2K compliant!)
 SQL:2003
Commercial systems offer most, if not all, SQL-92 features, plus
varying feature sets from later standards and special proprietary features.
Not all examples here may work on your particular system.
2.9 SQL DATA DEFINITION
SQL uses the following set of commands to define database schema

CREATE
 Creates new databases, tables and views from RDBMS.
For example:-
 Create database tutorials point;
 Create table article;
 Create view for_students;
DROP

 Drops commands, views, tables, and databases from RDBMS.


For example:-
 Drop object_type object_name;
 Drop database tutorials point;
 Drop table article;
 Drop view for_students;
ALTER

 Modifies database schema.

 Alter object_type object_name parameters;


For example−

 Alter table article add subject varchar;


 This command adds an attribute in the relation article with the
name subject of string type.

2.10 BASIC STRUCTURE OF SQL QUERIES

A relational database consists of a collection of relations, each of


which is assigned a unique name. SQL allows the use of null values to
indicate that the value either is unknown or does not exist. The basic
structure of an SQL expression consists of three clauses: select, from, and
where
 The select clause corresponds to the projection operation of the
relational algebra. It is used to list the attributes desired in the
result of a query.
 The from clause corresponds to the Cartesian-product operation
of the relational algebra.
 The where clause corresponds to the selection predicate of the
relational algebra.
SQL query form:-
select A1, A2,……,Ai

from r1,r2,….,rm

where P

each Ai represents an attribute, each ri a relation. P is a predicate.


The query is equivalent to the relational-algebra expression ∏

A1, A2, ….., An( P(r1 * r2 * ….* rm))


Select clause:-
 Find the names of all branches in the loan relation
select branch-name from loan
 Suppose we want to force the elimination of duplicates, we insert
the keyword distinct after select. We can rewrite the preceding
query as
select distinct branch-name from loan
Where clause:
 Find all loan numbers for loans made at the Perryridge branch with
loan amounts greater that $1200
select loan-number from loan where branch-name=
’Perryridge’ and amount >
1200
SQL uses the logical connectives and, or, and not and also includes
between comparison operator
 Find the loan number of those loans with loan amounts between
$90,000 and $100,000
select loan-number from loan where amount between 90000
and 100000
From clause:-
The from clause by itself defines a Cartesian product of the relations
in the clause.
Example: For all customers who have a loan from the bank, find their
names and loan a amount

select customer-name, borrower.loan-number, amount


from borrower, loan
where borrower.loan-no = loan.loan-number.
 A select clause corresponds to the projection operation of the relational
algebra.
 The from clause corresponds to the Cartesian product operation of the
relational algebra.
 The where clause corresponds to the selection predicate of the
relational algebra.
 The qualification in the where clause is a Boolean combination of
conditions.
 The distinct keyword is optional. It indicates that the table does not
contain duplicates.
 The default is that duplicates are not eliminated.
 The selection list is the list of column names of table.
 The from list in a from clause is the list of table names.
Example:
 Find the sname,sregno from student relation
select sname,sregno from student;
 Find the sname,sregno from student relation where smark1 greater
than 60
select sname,sregno from student where smark1>60
Syntax:

SELECT (DISTINCT) Select – list


FROM from – list
WHERE qualification
SELECT  Which specifies columns to be retained in the result
FROM  which specifies a gross – product of tables
WHERE which specifies selection condition on the tables.
 It is used to select the list from the condition
Example:
SELECT DISTINCT S.Sname. S.age FROM Sailors s
 It products each set of rows <sname, age> pair.
 It we omit DISTINCT then it returns a multiset of rows (ie) with
Duplicates.

Range Variable:-
SELECT S.sid, S.Sname, S.rating, S.age
FROM Sailors As S
WHERE S.rating > 7
Here As is called as range variable. This is convenient shorthand for
SQL
Example:

 Find the sid’s of sailors who have reserved a red boat.


SELECT R.Sid FROM Boats B, Reserves R
 Find the names of sailors who have reserved a red boat?
SELECT S.Sname FROM sailors S.Reserver R.
Boats B WHERE S.Sid = R.Sid AND R.Bid =
b.Bid AND B.Color= ‘red’
 Find the colors of boats reserved by lubber .
SELECT B.Color
FROM Sailors S.Reserves R.Boats B
WHERE S.Sid = R.Sid AND R.Bid AND S.Sname = ‘Libbs’

2.11 SET OPERATIONS


The following set operations are used in SQL. They are
 UNION
 INTERSECT
 EXCEPT
It is also called as a set – manipulation constructs. They return a
multiset of rows.
Union:-
The Union operation automatically eliminates duplicate values. If we
want to retain all duplicates we must write union all in place of union.
Example:
(select sname from student) union(select ename form
employee)
Intersect:-
The intersect operation automatically eliminates duplicate values. if
we want to retain all duplicates we must write intersect all in place of
intersect.

Example:
(select sname from student) intersect(select ename form
employee)
Except(-):-
The Except operation automatically eliminates duplicate values. if
we want to retain all duplicates we must write except all in place of
except.
Example:
(select sname from student) except(select ename form
employee)
2.12 NULL VALUES
SQL allows the use of null values to indicate the absence of
information about the value of an attribute. The keyword null is used to
test for null values. SQL also provides a special comparison operator is
null to test whether a column value is null, not null constraints are used
disallow the null values. The primary key constraints are not allowed to
take on null values.
Disadvantage:-
The issue in the presence of null values in the definition of when two
rows in a relation instance or regarded as duplicates. Count (*) handles
null values. All other aggregate operations simply discard null values.
Example:
Select loan-no from loan where amount is null
Disallowing Null values:-
 It can be done by Specifying NOT NULL as a part of the field
definition.
 EX: CHAR (20) NOTNULL
 The fields on a primary key are not allowed to take on null values.
2.13 AGGREGATE FUNCTIONS
To retrieve the data, we need to perform some Computation or
Summarization.
SQL Supports Five Aggregation Operators:-
 Count
 Sum
 Avg
 Max
 Min
Count:-
It is used to count the number of occurrence
Example:
Count the number of sailors
SELECT COUNT (*) FROM Sailors S
It computes the number of distinct sailor names.
Sum:-
It is used to sum to number of values.
Avg:-
It is used to calculate the average value
Example:
Find the average of all Sailors?
Find the average age of sailors with a rating of 10?
SELECT AVG(S.age) FROM sailors S where S.rating =10;
Max:-
It is used to return the maximum value
Example:
find the name and age of Oldest Sailor
SELECT S.Sname, Max (s.age) FROM Sailors
SELECT S.Sname,S.age FROM Sailors S WHERE S.age =
(SELECT MAX(S2.age) FROM Sailors S2)
Min:-
It is used to count the number of occurrence

Example:
Find the name and age of Oldest Sailor
SELECT S.Sname, Max(s.age)FROM Sialors

2.14 NESTED SUBQUERIES


 It is a Query that has another query embedded with it
 The embedded query is called a sub – query
 The inner sub – query could Independent of the Outer – query.
 Find the names of sailors who have reserved the boat 103
SELECT S.Sname
FROM Sailors S
WHERE S.Sid IN (SELECT R.Sid FROM Reserver R WHERE
R.Bid = 103)
Multiple - Nested subqueries”-
It contains multiple queries embedded within it
Example:
 Find the names of sailors who have reserved a red boat?
SELECT S.Sname FROM Sailor S
WHERE S.Sid IN (SELECT R.Sid FROM Reserver R)
WHERE R.Bid IN SELECT B.Sid FROM Boats B
WHERE B.Color = ‘red’)
 Find the names of sailors who have not reserved a red boat?
SELECT S.Sname FROM Sailors S
WHERE S.Sid NOT IN (SELECT R.Sid FROM Reservers P)
WHERE R.Bid IN (SELECT B.Bid FROM Boats B)
WHERE B.Color = ‘red’
Correlated Nested Queries:-
Here the inner sub – query could depend on the low that is
currently being examined in the outer query.

Example:
 Find the names of sailors who have reserved a beat number 103.
SELECT S.Sname FROM Sailor S
WHERE EXISTS (SELECT * FROM Reserves R
WHERE R.Bid = 103 AND R. Bid = S.Sid)
 This EXISTS is another set – comparison operator such as IN NOTIN.
 It allows us to test whether a set is non – empty set of reserves lows
R such that R.Bid = 103 AND S.Sid = R.Sid is non - empty
 If so, sailor S has reserved boat 103, we retrieve the name.
 The sub query clearly depends on the current rows and must be
reevaluated for each row in sailors.

2.15 MODIFICATION OF DATABASE


The modification operations that can be performed on a database
are
 Insertion
 Deletion
 Updation
Insertion:-
While inserting a single row of data into the table, the insert
operation:
Creates a new row in the database table.
Loads the values passed into all the columns specified.
Syntax:
INSERT INTO tablename(attributename1,attributename2,…
attributenamen)
VALUES(expression);
Where
Table name->Name of the table .
Attribute Name->Name of the column.
Expression->Values of the column.
The character expression must be enclosed in a single quotes.
Example:
 To insert row values directly
INSERT INTO student VALUES(1001,’vikky’, 80, 60, 90, 240,
80.00,’pass’
 To insert row values through keyboard
INSERT INTO student VALUES (&regno,’&name’,&m1,&m2,
&m2,&total,&percentage,’&result’)
 To insert row values
INSERT INTO student
(&regno,’&name’,&m1,&m2,&m2,&total,
&percentage,’ result’) VALUES(1001,’vikky’, 80, 60, 90, 240,
80.00,’pass’);
 To insert row values into a table from another table
INSERT INTO student SELECT rno, name, tot, res FROM
academic;
Deletion:-
 Delete command is used to remove rows from the table.
 Using this command either of the below mentioned are possible.
 All rows can be deleted from a table.
 Remove selected rows from a table.
 Delete command operated in only one operation.
Syntax:
DELETE FROM <table name> WHERE predicate;
Example:
 To delete those rows from the table student, the query
DELETE FROM student;
 To delete those rows whose result is fail, the query is
DELETE FROM student WHERE result=’fail’;
Update:-
To change a value in a tuple without changing all values in the
tuple.
Syntax:
UPDATE <TABLENAME> SET PREDICATE;
Example:
 The annual interest payments are made, and all branches are to be
increased by 5 percent.
UPDATE account
SET balance=balance*1.05
 If interest is to paid only to accounts with a balance of $1000 or more
UPDATE account
SET balance=balance*1.05
WHERE balance>=1000
 SQL first tests all tuples in the relation to see whether they should be
updated, and carries out the updates afterward

12

You might also like