0% found this document useful (0 votes)
122 views103 pages

Chapter 3: Introduction To SQL: Database System Concepts, 7 Ed

Chapter 3 introduces SQL, covering its history, components, and basic operations including data definition and manipulation. It outlines SQL's structure, integrity constraints, and provides examples of creating tables with primary and foreign keys. The chapter also explains the basic query structure and the select clause for retrieving data from databases.
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)
122 views103 pages

Chapter 3: Introduction To SQL: Database System Concepts, 7 Ed

Chapter 3 introduces SQL, covering its history, components, and basic operations including data definition and manipulation. It outlines SQL's structure, integrity constraints, and provides examples of creating tables with primary and foreign keys. The chapter also explains the basic query structure and the select clause for retrieving data from databases.
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

Chapter 3: Introduction to SQL

Database System Concepts, 7th Ed.


©Silberschatz, Korth and Sudarshan
See [Link] for conditions on re-use
Outline

 Overview of The SQL Query Language


 SQL Data Definition
 Basic Query Structure of SQL Queries
 Additional Basic Operations
 Set Operations
 Null Values
 Aggregate Functions
 Nested Subqueries
 Modification of the Database

Database System Concepts - 7th Edition 3.2 ©Silberschatz, Korth and Sudarshan
History

 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.

Database System Concepts - 7th Edition 3.3 ©Silberschatz, Korth and Sudarshan
SQL Parts

 DML -- provides the ability to query information from the


database and to insert tuples into, delete tuples from, and
modify tuples in the database.
 integrity – the DDL includes commands for specifying integrity
constraints.
 View definition -- The DDL includes commands for defining
views.
 Transaction control –includes commands for specifying the
beginning and ending of transactions.
 Embedded SQL and dynamic SQL -- define how SQL
statements can be embedded within general-purpose
programming languages.
 Authorization – includes commands for specifying access
rights to relations and views.

Database System Concepts - 7th Edition 3.4 ©Silberschatz, Korth and Sudarshan
Data Definition Language

The SQL data-definition language (DDL) allows the


specification of information about relations, including:

 The schema for each relation.


 The type of values associated with each attribute.
 The Integrity constraints
 The set of indices to be maintained for each relation.
 Security and authorization information for each relation.
 The physical storage structure of each relation on disk.

Database System Concepts - 7th Edition 3.5 ©Silberschatz, Korth and Sudarshan
Domain Types in SQL
 char(n). Fixed length character string, with user-specified length
n.
 varchar(n). Variable length character strings, with user-specified
maximum length n.
 int. Integer (a finite subset of the integers that is machine-
dependent). -231 through 231 - 1
 smallint. Small integer (a machine-dependent subset of the
integer domain type). -215 through 215 - 1
 numeric(p,d). Fixed point number, with user-specified precision of
p digits, with d digits to the right of decimal point. (ex.,
numeric(3,1), allows 44.5 to be stores exactly, but not 444.5 or
0.32)
 real, double precision. Floating point and double-precision
floating point numbers, with machine-dependent precision.
 float(n). Floating point number, with user-specified precision of at
least n digits. -1.79E + 308 through 1.79E + 308.
 More are covered in Chapter 4.

Database System Concepts - 7th Edition 3.6 ©Silberschatz, Korth and Sudarshan
Create Table Construct
 An SQL relation is defined using the create table command:
create table r
(A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))

r is the name of the relation


 each Ai is an attribute name in the schema of relation r
Di is the data type of values in the domain of attribute Ai
 Example:

Database System Concepts - 7th Edition 3.7 ©Silberschatz, Korth and Sudarshan
Create Table Construct
 An SQL relation is defined using the create table command:
create table r
(A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
 Example:
create table instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))

Database System Concepts - 7th Edition 3.8 ©Silberschatz, Korth and Sudarshan
Given the following schema of Emp table. Write
the SQL statement to create the Emp table.
Emp ( eid, ename ,age ,salary )

Database System Concepts - 7th Edition 3.9 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Emp (
eid INTEGER,
ename CHAR(10),
age INTEGER,
salary REAL
)

Database System Concepts - 7th Edition 3.10 ©Silberschatz, Korth and Sudarshan
Given the following schema of Dept
table. Write the SQL statement to
create the Dept table.
Dept ( did, budget, managerid )

Database System Concepts - 7th Edition 3.11 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Dept (
did INTEGER,
budget REAL,
managerid INTEGER
)

Database System Concepts - 7th Edition 3.12 ©Silberschatz, Korth and Sudarshan
Given the following schema of EMPLOYEE
table. Write the SQL statement to create the
Emp table.
EMPLOYEE ( FNAME , MINIT , LNAME ,
SSN , ADDRESS, Gander , SALARY, DNO)

Database System Concepts - 7th Edition 3.13 ©Silberschatz, Korth and Sudarshan
CREATE TABLE EMPLOYEE (
FNAME VARCHAR(15),
MINIT CHAR(1),
LNAME VARCHAR(15),
SSN CHAR(9),
ADDRESS VARCHAR(30),
Gander CHAR,
SALARY DECIMAL(10,2),
DNO INT
)

Database System Concepts - 7th Edition 3.14 ©Silberschatz, Korth and Sudarshan
Integrity Constraints in Create Table

 Types of integrity constraints


 primary key (A1, ..., An )
 foreign key (Am, ..., An ) references r
 not null
 SQL prevents any update to the database that violates an
integrity constraint.
 Example:
create table instructor (
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references department);

Database System Concepts - 7th Edition 3.15 ©Silberschatz, Korth and Sudarshan
And a Few More Relation Definitions
 create table student (
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department);

 create table takes (


ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
grade varchar(2),
primary key (ID, course_id, sec_id, semester, year) ,
foreign key (ID) references student,
foreign key (course_id, sec_id, semester, year) references section);

Database System Concepts - 7th Edition 3.16 ©Silberschatz, Korth and Sudarshan
And more still

 create table course (


course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0),
primary key (course_id),
foreign key (dept_name) references department);

Database System Concepts - 7th Edition 3.17 ©Silberschatz, Korth and Sudarshan
Given the following schema of Emp table. Write
the SQL statement to create the Emp table.
Make sure that your statement specifies the
PRIMARY KEY and enforce the constraint that
Employee name should not be null.
Emp ( eid, ename ,age ,salary )
CREATE TABLE Emp (
eid INTEGER,
ename CHAR(10),
age INTEGER,
salary REAL
)
Database System Concepts - 7th Edition 3.18 ©Silberschatz, Korth and Sudarshan
Emp ( eid, ename ,age ,salary )
CREATE TABLE Emp (
eid INTEGER,
ename CHAR(10) not null,
age INTEGER,
salary REAL,
PRIMARY KEY (eid)
)

Database System Concepts - 7th Edition 3.19 ©Silberschatz, Korth and Sudarshan
Given the following schema of Dept table. Write the
SQL statement to create the Dept table. Make sure
that your statement specifies the PRIMARY.

Dept ( did, budget, managerid )


CREATE TABLE Dept (
did INTEGER,
budget REAL,
managerid INTEGER
)

Database System Concepts - 7th Edition 3.20 ©Silberschatz, Korth and Sudarshan
Dept ( did, budget, managerid )
CREATE TABLE Dept (
did INTEGER,
budget REAL,
managerid INTEGER,
PRIMARY KEY (did)
)

Database System Concepts - 7th Edition 3.21 ©Silberschatz, Korth and Sudarshan
Given the following schema of Emp table and
the schema of Dept table. Write the SQL
statement to create the Emp and Dept tables.
Make sure that your statement specifies the
PRIMARY KEY and any FOREIGN KEYS.

Emp ( eid, ename ,age ,salary )


Dept ( did, budget, managerid )

Database System Concepts - 7th Edition 3.22 ©Silberschatz, Korth and Sudarshan
Emp ( eid, ename ,age ,salary )
Dept ( did, budget, managerid )

CREATE TABLE Dept (


did INTEGER,
budget REAL,
managerid INTEGER,
PRIMARY KEY (did),
FOREIGN KEY (managerid) REFERENCES Emp
)

Database System Concepts - 7th Edition 3.23 ©Silberschatz, Korth and Sudarshan
Write the SQL statements that define the
relational schema (tables) for this database.
Assume that actor_id, play_id, year_born,
and year_written are all integers, and that
name, title, author, and character_name are
strings. Be sure to define primary keys and
foreign keys constraints.

Actor(actor_id, name, year_born)


Play(play_id, title, author, year_written)
Role(actor_id, character_name, play_id)

Database System Concepts - 7th Edition 3.24 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Actor (
actor_id INTEGER PRIMARY KEY,
Name VARCHAR(100),
year_born INTEGER );

CREATE TABLE Play(


play_id INTEGER PRIMARY KEY,
title VARCHAR(100),
author VARCHAR(100),
year_written INTEGER );

CREATE TABLE Role(


actor_id‘ INTEGER REFERENCES Actor(actor_id),
character_name VARCHAR(100),
play_id INTEGER REFERENCES Play(play_id),
PRIMARY KEY(actor_id, character_name, play_id)
);

Database System Concepts - 7th Edition 3.25 ©Silberschatz, Korth and Sudarshan
ASS

Consider the following relational schema


Student (SID, DID, Enroll_Year, Nationality)
Department (DID, Name, Building_Num, Telephone, FID)
Finance (FID, Budget, Expenses)
- List any PRIMARY KEYs
- List any FOREIGN KEYs
- Put one constraint within each table
- Write SQL statements to create the tables with
the specified constraint

Database System Concepts - 7th Edition 3.26 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 3.27 ©Silberschatz, Korth and Sudarshan
Updates to tables
 Insert
 insert into instructor values ('10211', 'Smith', 'Biology', 66000);
 Delete
 Remove all tuples from the student relation
 delete from student
 Drop Table
 drop table r
 Alter
 alter table r add A D
 where A is the name of the attribute to be added to relation
r and D is the domain of A.
 All exiting tuples in the relation are assigned null as the
value for the new attribute.
 alter table r drop A
 where A is the name of an attribute of relation r
 Dropping of attributes not supported by many databases.

Database System Concepts - 7th Edition 3.28 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Actor (
actor_id INTEGER PRIMARY KEY,
h t able
i n eac
Name VARCHAR(100),
ecor d
r ta r
year_born INTEGER ); I n se

CREATE TABLE Play(


play_id INTEGER PRIMARY KEY,
title VARCHAR(100),
author VARCHAR(100),
year_written INTEGER );

CREATE TABLE Role(


actor_id‘ INTEGER REFERENCES Actor(actor_id),
character_name VARCHAR(100),
play_id INTEGER REFERENCES Play(play_id),
PRIMARY KEY(actor_id, character_name, play_id)
);

Database System Concepts - 7th Edition 3.29 ©Silberschatz, Korth and Sudarshan
insert into Actor values (101, ‘abood’, 1999)

insert into Play values (10, ‘boos’,‘adool’, 2015)

insert into Role values (101, ‘se-sead’, 10)

Database System Concepts - 7th Edition 3.30 ©Silberschatz, Korth and Sudarshan
Basic Query Structure

 A typical SQL query has the form:

select A1, A2, ..., An


from r1, r2, ..., rm
where P

 Ai represents an attribute
 Ri represents a relation
 P is a predicate.
 The result of an SQL query is a relation.

Database System Concepts - 7th Edition 3.31 ©Silberschatz, Korth and Sudarshan
The select Clause

 The select clause lists the attributes desired in the result of


a query
 corresponds to the projection operation of the relational
algebra
 Example: find the names of all instructors:
select name
from instructor

Database System Concepts - 7th Edition 3.32 ©Silberschatz, Korth and Sudarshan
select name
from instructor

instructor

Database System Concepts - 7th Edition 3.33 ©Silberschatz, Korth and Sudarshan
The select Clause

 NOTE: SQL names are case insensitive (i.e., you may use
upper- or lower-case letters.)
 E.g., Name ≡ NAME ≡ name
 Some people use upper case wherever we use bold font.

select name
from instructor

Database System Concepts - 7th Edition 3.34 ©Silberschatz, Korth and Sudarshan
List the account numbers of
existing accounts

account

Database System Concepts - 7th Edition 3.35 ©Silberschatz, Korth and Sudarshan
List the account numbers of
existing accounts
SELECT account_number
FROM account

account

Database System Concepts - 7th Edition 3.36 ©Silberschatz, Korth and Sudarshan
 Fined the firms and the number
of its employees

firm
Database System Concepts - 7th Edition 3.37 ©Silberschatz, Korth and Sudarshan
 Fined the firms and the number
of its employees
SELECT firmName, employees
FROM firm

firm
Database System Concepts - 7th Edition 3.38 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)

 SQL allows duplicates in relations as well as in query


results.
 To force the elimination of duplicates, insert the keyword
distinct after select.
 Find the department names of all instructors, and remove
duplicates
select distinct dept_name
from instructor

Database System Concepts - 7th Edition 3.39 ©Silberschatz, Korth and Sudarshan
select distinct dept_name
from instructor
dept_name
Physics
Finance
History
.Comp. Sci
.Elec. Eng
Biology
Music

instructor
Database System Concepts - 7 Edition
th
3.40 ©Silberschatz, Korth and Sudarshan
 List all the cities that have branches
without duplication

branch
Database System Concepts - 7 Edition
th
3.41 ©Silberschatz, Korth and Sudarshan
 List all the cities that have branches
without duplication

select distinct branch_city


from branch

branch
Database System Concepts - 7 Edition
th
3.42 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)

 The keyword all specifies that duplicates should not be


removed.

select all dept_name


from instructor

instructor
Database System Concepts - 7th Edition 3.43 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)

 An asterisk in the select clause denotes “all


attributes”
select *
from instructor

instructor
Database System Concepts - 7th Edition 3.44 ©Silberschatz, Korth and Sudarshan
The select Clause (Cont.)

 The select clause can contain arithmetic expressions


involving the operation, +, –, , and /, and operating on
constants or attributes of tuples.
 The query:
select ID, name, salary/12
from instructor
would return a relation that is the same as the instructor
relation, except that the value of the attribute salary is
divided by 12.
 Can rename “salary/12” using the as clause:
select ID, name, salary/12 as monthly_salary

Database System Concepts - 7th Edition 3.45 ©Silberschatz, Korth and Sudarshan
 List the account numbers
each with quarter of its
balance with head “quarter”

account
Database System Concepts - 7th Edition 3.46 ©Silberschatz, Korth and Sudarshan
 List the account numbers
each with quarter of its
balance with head “quarter”
SELECT account_number, balance/4 as quarter
FROM account

account
Database System Concepts - 7th Edition 3.47 ©Silberschatz, Korth and Sudarshan
 List the account numbers
each with its balance added
to the balance 10% with head
“tbalace”

account
Database System Concepts - 7th Edition 3.48 ©Silberschatz, Korth and Sudarshan
 List the account numbers
each with its balance added
to the balance 10% with head
“tbalace”
SELECT account_number, balance*1.1 as tbalance
FROM account

account
Database System Concepts - 7th Edition 3.49 ©Silberschatz, Korth and Sudarshan
 List the branches each with the half of
its assets amount with header “hASS”

branch
Database System Concepts - 7 Edition
th
3.50 ©Silberschatz, Korth and Sudarshan
 List the branches each with the half of
its assets amount with header “hASS”

SELECT branch_name, asset/2 as hASS


FROM branch

branch
Database System Concepts - 7 Edition
th
3.51 ©Silberschatz, Korth and Sudarshan
The where Clause
 The where clause specifies conditions that the result must satisfy
 Corresponds to the selection predicate of the relational algebra.
 To find all instructors in Comp. Sci. dept
select name
from instructor
where dept_name = 'Comp. Sci.'

instructor
Database System Concepts - 7th Edition 3.52 ©Silberschatz, Korth and Sudarshan
The where Clause
 SQL allows the use of the logical connectives and, or, and not
 The operands of the logical connectives can be expressions involving
the comparison operators <, <=, >, >=, =, and <>.
 Comparisons can be applied to results of arithmetic expressions
 To find all instructors in Comp. Sci. dept with salary > 80000

select name
from instructor
where dept_name = 'Comp. Sci.' and salary < 80000

Database System Concepts - 7th Edition 3.53 ©Silberschatz, Korth and Sudarshan
 List the branches in “Brooklyn” City

branch

Database System Concepts - 7th Edition 3.54 ©Silberschatz, Korth and Sudarshan
 List the branches in “Brooklyn” City

branch
SELECT branch_name
FROM branch
WHERE branch_city = 'Brooklyn'

Database System Concepts - 7th Edition 3.55 ©Silberschatz, Korth and Sudarshan
 List the branches in “Brooklyn” City
and the branches whose assets are
exceeded 7000000 $

branch
Database System Concepts - 7 Edition
th
3.56 ©Silberschatz, Korth and Sudarshan
 List the branches in “Brooklyn” City
and the branches whose assets are
exceeded 7000000 $
SELECT branch_name
FROM branch
WHERE branch_city = 'Brooklyn' or assets > 7000000

branch
Database System Concepts - 7 Edition
th
3.57 ©Silberschatz, Korth and Sudarshan
 Translate the following operation to SQL
statement
course_id, year ( semester=“Spring” Λ year=2018 (section))

Database System Concepts - 7th Edition


section3.58 ©Silberschatz, Korth and Sudarshan
 Translate the following operation to SQL
statement
course_id, year ( semester=“Spring” Λ year=2018 (section))

SELECT course_id, year


FROM section
WHERE semester = 'spring' and year=2018

Database System Concepts - 7th Edition


section3.59 ©Silberschatz, Korth and Sudarshan
 Translate the following operation to SQL
statement
course_id ( semester=“Fall” Λ year=2017 (section))

Database System Concepts - 7th Edition


section3.60 ©Silberschatz, Korth and Sudarshan
QUIZ
CREATE TABLE Actor (
actor_id INTEGER PRIMARY KEY,
Name VARCHAR(100),
year_born INTEGER );

Write SQL statement to insert a record into the Actor table

Find the records of all sections have been taken in Painter building at any of time slots (A or D) only

 salary >85000 (instructor)

Database System Concepts - 7th Edition 3.61 ©Silberschatz, Korth and Sudarshan
CREATE TABLE Actor (
actor_id INTEGER PRIMARY KEY,
Name VARCHAR(100),
year_born INTEGER );

Write SQL statement to insert a record into


the Actor table

Database System Concepts - 7th Edition 3.62 ©Silberschatz, Korth and Sudarshan
INSERT INTO Actor VALUES (10, ‘xxxxx’, 2000)

Database System Concepts - 7th Edition 3.63 ©Silberschatz, Korth and Sudarshan
 Find the records of all sections have
been taken in Painter building

section
Database System Concepts - 7th Edition 3.64 ©Silberschatz, Korth and Sudarshan
 SELECT * FROM section WHERE building = ‘Painter ’

Database System Concepts - 7th Edition 3.65 ©Silberschatz, Korth and Sudarshan
 Find the records of all sections have
been taken in Painter building at any of
time slots (A or D) only

section
Database System Concepts - 7th Edition 3.66 ©Silberschatz, Korth and Sudarshan
 SELECT * FROM section WHERE building = ‘Painter ’ AND ( time-
slot_id = ‘A’ OR time-slot_id = ‘D’ )

Database System Concepts - 7th Edition 3.67 ©Silberschatz, Korth and Sudarshan
 Find the records of all sections have
been taken at Fall semester and take
place in Packard building

section
Database System Concepts - 7th Edition 3.68 ©Silberschatz, Korth and Sudarshan
 SELECT * FROM section WHERE semester = ‘Fall’ AND building =
‘Packard ’

Database System Concepts - 7th Edition 3.69 ©Silberschatz, Korth and Sudarshan
 List the account numbers
each with its balance added
to the balance 20% with head
“twbalance ”

account
Database System Concepts - 7th Edition 3.70 ©Silberschatz, Korth and Sudarshan
 SELECT account_number, balance*1.2 as twbalance
FROM account

Database System Concepts - 7th Edition 3.71 ©Silberschatz, Korth and Sudarshan
 salary >85000 (instructor)

instructor

Database System Concepts - 7th Edition 3.72 ©Silberschatz, Korth and Sudarshan
SELECT *
FROM instructor
WHERE salary >85000

Database System Concepts - 7th Edition 3.73 ©Silberschatz, Korth and Sudarshan
 dept_name=“Physics”  salary > 90,000 (instructor)

instructor

Database System Concepts - 7th Edition 3.74 ©Silberschatz, Korth and Sudarshan
SELECT *
FROM instructor
WHERE dept_name=“Physics” AND salary > 90,000

Database System Concepts - 7th Edition 3.75 ©Silberschatz, Korth and Sudarshan
 Translate the following operation to SQL
statement
course_id, semester, year ( semester=‘Spring’ Λ year=2018 V building = ‘

Packard’ (section))

Database System Concepts - 7th Edition


section3.76 ©Silberschatz, Korth and Sudarshan
SELECT course_id, semester, year
FROM instructor
WHERE semester=‘Spring’ AND year=2018 OR building = ‘ Packard’

Database System Concepts - 7th Edition 3.77 ©Silberschatz, Korth and Sudarshan
The from Clause

 The from clause lists the relations involved in the query


 Corresponds to the Cartesian product operation of the relational
algebra.
 Find the Cartesian product instructor X teaches
select 
from instructor, teaches
 generates every possible instructor – teaches pair, with all
attributes from both relations.
 For common attributes (e.g., ID), the attributes in the resulting
table are renamed using the relation name (e.g., [Link])

Database System Concepts - 7th Edition 3.78 ©Silberschatz, Korth and Sudarshan
instructor X teaches
select 
from instructor, teaches

instructor

teaches
Database System Concepts - 7th Edition 3.79 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 3.80 ©Silberschatz, Korth and Sudarshan
The from Clause

 Cartesian product not very useful


directly, but useful combined with
where-clause condition (selection
operation in relational algebra).
Join Operation

Database System Concepts - 7th Edition 3.81 ©Silberschatz, Korth and Sudarshan
Examples

 Find the names of all instructors who


have taught some course and the
course_id
select name, course_id
from instructor , teaches
where [Link] = [Link]

Database System Concepts - 7th Edition 3.82 ©Silberschatz, Korth and Sudarshan
Examples

 Find the names of all instructors in the Art


department who have taught some course
and the course_id
select name, course_id
from instructor , teaches
where [Link] = [Link] and
instructor. dept_name = 'Art'

Database System Concepts - 7th Edition 3.83 ©Silberschatz, Korth and Sudarshan
 Find the names of all employees who
work at marketing department

Dtable
Etable

Database System Concepts - 7th Edition 3.84 ©Silberschatz, Korth and Sudarshan
 Find the names of all employees who
work at marketing department
SELECT ename
FROM etable, dtable
WHERE [Link] = [Link] and dname = ‘marketing’

Dtable
Etable
Database System Concepts - 7th Edition 3.85 ©Silberschatz, Korth and Sudarshan
 Find the teacher name who teach the course with
course id ‘CS-319’
SELECT name
FROM instructor , teaches
WHERE [Link] = [Link] and course_id = ‘CS-319’

instructor

teaches
Database System Concepts - 7th Edition 3.86 ©Silberschatz, Korth and Sudarshan
The Rename Operation

The SQL allows renaming


relations and attributes using
the as clause:
old-name as new-name

Database System Concepts - 7th Edition 3.87 ©Silberschatz, Korth and Sudarshan
The Rename Operation

 Find the names of all instructors who


have a higher salary than some
instructor in 'Comp. Sci'.
select distinct [Link]
from instructor as T, instructor as S
where [Link]>[Link] and
S.dept_name='Comp. Sci.’

Database System Concepts - 7th Edition 3.88 ©Silberschatz, Korth and Sudarshan
The Rename Operation

 Keyword as is optional and may be omitted


al
instructor as T ≡ instructor T t ion
op
is
as
select distinct [Link]
from instructor T, instructor S
where [Link]>[Link] and S.dept_name='Comp. Sci.’

s me
l ia na
e a n
b l tio
t a ela
r r
co
Database System Concepts - 7th Edition 3.89 ©Silberschatz, Korth and Sudarshan
 Find the teacher name who teach the course with
course id ‘CS-319’ [rename the tables names]

instructor

teaches
Database System Concepts - 7th Edition 3.90 ©Silberschatz, Korth and Sudarshan
 Find the teacher name who teach the course with
course id ‘CS-319’ [rename the tables names]
SELECT [Link]
FROM instructor ins , teaches tch
WHERE [Link] = [Link] and tch.course_id = ‘CS-319’

instructor

teaches
Database System Concepts - 7th Edition 3.91 ©Silberschatz, Korth and Sudarshan
Self Join Example
c es
 Relation emp-super ren
fe
re
i ch
wh ute
ute trib
b t
tt ri er a
a th
an no
s a
ha wn
ble o
ta i ts
a
h en
w
 Find the supervisor of “Bob”
 Find the supervisor of the supervisor of “Bob”
 Can you find ALL the supervisors (direct and indirect) of
“Bob”?

Database System Concepts - 7th Edition 3.92 ©Silberschatz, Korth and Sudarshan
Find the supervisor of “Bob”
SLECT supervisor
FROM emp-super emp, emp-super sup
WHERE [Link] = ‘Bob’

SELECT [Link]
FROM emp-super emp, emp-super sup
WHERE [Link] = ‘Bob’ and [Link]= [Link]

Database System Concepts - 7th Edition 3.93 ©Silberschatz, Korth and Sudarshan
String Operations

 SQL includes a string-matching operator for


comparisons on character strings. The
operator like uses patterns that are
described using two special characters:
percent ( % ). The % character matches
any substring.
underscore ( _ ). The _ character
matches any character.

Database System Concepts - 7th Edition 3.94 ©Silberschatz, Korth and Sudarshan
String Operations

 percent ( % ).
 The % character matches any substring.

• '%dar%'
• 'Intro%' beginning with “Intro”.
• ‘%d’ Ending with “d”

Database System Concepts - 7th Edition 3.95 ©Silberschatz, Korth and Sudarshan
String Operations

 Find the names of all instructors whose name


includes the substring “dar”.
select name
from instructor
where name like '%dar%'

Database System Concepts - 7th Edition 3.96 ©Silberschatz, Korth and Sudarshan
 Find the names of all instructors whose name ending with the
substring “an”.
SELECT [Link]
FROM instructor I
WHERE [Link] like '%an'

instructor

Database System Concepts - 7th Edition 3.97 ©Silberschatz, Korth and Sudarshan
 Find the course id and the names of all instructors whose teach courses
that have id ending with the substring “101”.

instructor

teaches
Database System Concepts - 7th Edition 3.98 ©Silberschatz, Korth and Sudarshan
 Find the course id and the names of all instructors whose teach courses
that have id ending with the substring “101”.
SELECT [Link], T.course_id
FROM instructor I, teaches T
WHERE [Link] = [Link] and course_id like '%101'

instructor

teaches
Database System Concepts - 7th Edition 3.99 ©Silberschatz, Korth and Sudarshan
 Find the course id and the names of all instructors whose name beginning
with the char ‘E’.

instructor

teaches
Database System Concepts - 7th Edition 3.100 ©Silberschatz, Korth and Sudarshan
 Find the course id and the names of all instructors whose name beginning
with the char ‘E’.
SELECT [Link], T.course_id
FROM instructor I, teaches T
WHERE [Link] = [Link] and [Link] like ‘E%'

instructor

teaches
Database System Concepts - 7th Edition 3.101 ©Silberschatz, Korth and Sudarshan
String Operations

 Match the string “100%”


like '100\%' escape '\'
in that above we use backslash (\) as the
escape character.

Database System Concepts - 7th Edition 3.102 ©Silberschatz, Korth and Sudarshan
String Operations (Cont.)

 Patterns are case sensitive (Except MySQL).


 Pattern matching examples:
 'Intro%' matches any string beginning with “Intro”.
 '%Comp%' matches any string containing “Comp” as a substring.
 '_ _ _' matches any string of exactly three characters.
wi
ld
ca  '_ _ _ %' matches any string of at least three characters.
rd

 SQL supports a variety of string operations such as


 concatenation (using “||”)
 converting from upper to lower case (and vice versa)
 finding string length, extracting substrings, etc.

e r(s)
upp er(s)
low (s)
trim
Database System Concepts - 7th Edition 3.103 ©Silberschatz, Korth and Sudarshan

You might also like