0% found this document useful (0 votes)
52 views

Lecture 8

SQL is a programming language used to manage data in relational database management systems. The basic structure of an SQL query consists of SELECT, FROM, and WHERE clauses. The SELECT clause specifies the attributes to return, FROM lists the relations, and WHERE filters rows. Natural joins combine relations based on common attribute values.

Uploaded by

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

Lecture 8

SQL is a programming language used to manage data in relational database management systems. The basic structure of an SQL query consists of SELECT, FROM, and WHERE clauses. The SELECT clause specifies the attributes to return, FROM lists the relations, and WHERE filters rows. Natural joins combine relations based on common attribute values.

Uploaded by

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

SQL

• SQL ( Structured Query Language) is a


special-purpose programming
language designed for managing data
held in a relational database
management system (RDBMS), or for
stream processing in a relational data
stream management system (RDSMS).
Basic Structure
• The basic structure of an SQL
expression consists of three
clauses:
• select ,
• from,
• where.
Basic Structure
• 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.
• It lists the relations to be scanned in the
evaluation of the expression.
Basic Structure
• The where clause corresponds to the
selection predicate of the relational algebra.
• It consists of a predicate involving attributes of
the relations that appear in the from clause.
• A typical SQL query has the form
The select Clause
• Find the names of all branches in the
loan relation?
The select Clause
• select branch_name
from loan
• If we want to force the elimination of duplicates,
we insert the keyword distinct after select .
• select distinct branch_name
from loan
The select Clause
• SQL allows us to use the keyword all to specify
explicitly that duplicates are not removed

• select all branch_name


• from loan

• The asterisk symbol “ * ” can be used to


denote all attributes.
The select Clause
• The select clause may also contain
arithmetic expressions involving the
operators +, −, ∗, and / operating on
constants or attributes of tuples.

• select loan_number, branch_name,


amount* 100
• from loan
The where Clause
• Find all loan numbers for loans made at
the Perryridge branch with loan amounts
greater that $1200.
The 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


• The operands of the logical connectives can be
expressions involving the comparison operators <, <=,
>, >=, =,and <>.
The where Clause
• Find the loan number of those loans with
loan amounts between $90,000 and
$100,000.
The where Clause
• select loan_number
• from loan
• where amount between 90000 and 100000

• SQL includes a between comparison operator


to simplify where clauses that specify that a
value be less than or equal to some value and
greater than or equal to some other value.
• Similarly, we can use the not between
comparison operator.
The from Clause
• For all customers who have a loan from
the bank, find their names, loan numbers
and loan amount.
The from Clause
• For all customers who have a loan from the
bank, find their names, loan numbers and
loan amount.

• select customer_name,
borrower.loan_number, amount
• from borrower, loan
• where borrower.loan_number =
loan.loan_number
The from Clause
• Find the customer names, loan numbers,
and loan amounts for all loans at the
Perryridge branch.
The from Clause
• Find the customer names, loan numbers, and loan
amounts for all loans at the Perryridge branch.

• select customer_name, borrower.loan_number,


amount
• from borrower, loan
• where borrower.loan_number =
loan.loan_number and branch_name =
’Perryridge’
Basic Schema Definition
• The general form of the create table command
is:
Basic Schema Definition
• The following command creates a
relation department in the database.

• create table department


(dept_name varchar (20),
building varchar (15),
budget numeric (12,2),
primary key (dept name));
Basic Schema Definition
• The following command creates a relation
Course in the database.

• CREATE TABLE course (


course_id VARCHAR(15) NOT NULL PRIMARY
KEY,
course_name TEXT(30) NOT NULL,
dept_name TEXT(10) NOT NULL,
batch VARCHAR(10) NOT NULL );
Basic Schema Definition
SQL
• To remove a relation from an SQL database,
we use the drop table command.
• The drop table command deletes all
information about the dropped relation from
the database.

• drop table table name ;


• Example
• drop table department ;
SQL
• We use the alter table command to add attributes
to an existing relation.
• All tuples in the relation are assigned null as the
value for the new attribute.

• The form of the alter table command is:


• alter table table_name add A D;

• Where A is the name of the attribute to be added,


and D is the type of the added attribute.
SQL
• We can drop attributes from a
relation by the command:

• alter table relation_name drop


Attribute_name;
SQL
• Find instructor names and course
identifiers for instructors in the Computer
Science department.
• Relation schema is given below:

• Instructor( ID, name, dept_name, Salary)


• Teaches(ID, course_id , sec_id ,
semester ,year)
SQL
• Find instructor names and course
identifiers for instructors in the Computer
Science department.

• select name, course_id


• from instructor, teaches
• where instructor. ID= teaches . ID and
instructor. dept name= ’Computer
Science’;
SQL
• For all instructors in the university who have
taught some course, find their names and the
course ID of all courses they taught.
SQL
• For all instructors in the university who
have taught some course, find their
names and the course ID of all courses
they taught.

• select name, course_id


• from instructor, teaches
• where instructor. ID= teaches . ID;
The Natural Join
• The natural join operation operates on two
relations and produces a relation as the result.
• Unlike the Cartesian product of two relations,
which concatenates each tuple of the first
relation with every tuple of the second,
natural join considers only those pairs of
tuples with the same value on those
attributes that appear in the schemas of both
relations.
The Natural Join
• Going back to the example of the relations instructor
and teaches ,computing instructor natural join teaches
considers only those pairs of tuples where both the
tuple from instructor and the tuple from teaches have
the same value on the common attribute, ID.
• This query can be written more concisely using the
natural-join operation in SQL as:

• select name, course_id


• from instructor natural join teaches ;
The Natural Join
• A from clause in an SQL query can have
multiple relations combined using
natural join, as shown here:
The Natural Join
• List the names of instructors along with
the titles of courses that they teach.
The Natural Join
• List the names of instructors along with
the titles of courses that they teach.

• select name, title


• from instructor natural join teaches ,
course
• where teaches . Course_id = course .
Course_id ;
The Natural Join
• select name, title
• from instructor natural join teaches , course
• where teaches . course id = course . course id ;

• The natural join of instructor and teaches is first computed,


and a Cartesian product of this result with course is
computed, from which the where clause extracts only those
tuples where the course identifier from the join result
matches the course identifier from the course relation.
• Note that teaches . Course_id in the where clause refers to
the course id field of the natural join result, since this field
in turn came from the teaches relation.
The Natural Join
• List the names of instructors along with the titles of
courses that they teach.

• select name, title


• from instructor natural join teaches natural join course ;

• **This query would then omit all (instructor name,


course title) pairs where the instructor teaches a course
in a department other than the instructor’s own
department.
The Natural Join
• select name, title
• from instructor natural join teaches natural join course ;
• Note that the natural join of instructor and teaches
contains the attributes ( ID, name, dept_ name, salary ,
course_id , sec_id )
• While the course relation contains the attributes
( course_id , title , dept_name, credits ).
• As a result, the natural join of these two would require
that the dept_name attribute values from the two
inputs be the same, in addition to requiring that the
course_id values be the same.
The Natural Join
• select name, title
• from (instructor natural join teaches ) join
course using (course id );
• The operation join ...using requires a list of
attribute names to be specified.
• Thus, in the preceding SQL query, the join
construct permits teaches . Dept_name and
course. Dept_name to differ, and the SQL
query gives the correct answer.

You might also like