72
Database SQL

Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Embed Size (px)

Citation preview

Page 1: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Database

SQL

Page 2: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Relational language SQL

SQL or Structured Query Language

SQL is an interactive Query language and

database programming Language

Page 3: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

SQL History

Originally (mid-’70s) defined as Sequel in System R project at IBM San Jose Research Center

Name change to SQL, multiple iterations of standards (’86, ’87, ’89, ’92,…)

Has usual good and bad aspects of much-used, much-extended languages

Think of C++ or Fortran-90…

Page 4: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

SQL

From American National Institue ANSI, it was

SQL-86 (ANSI 1986) and SQL-2 (SQL-92)

SQL Language is divided into DDL : Data Definition Language

DML : Data Manipulation Language

DCL : Data Control Language

Page 5: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

DDL

It is commands used for: Creates databases, tables, indices Create views Specify integrity constraints

DDL commands are

CREATE

ALTER

DROP

Page 6: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

DML

For values in the database Perform queries Perform updates

DML commands:

SELECT

UPDATE

DELETE

INSERT

Page 7: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

DCL

For controlling the access of data

DCL commands are

GRANT

REVOKE

Page 8: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

SQL - DDL

It used to define and manage the structure of

tables in the database

Its statements as CREATE TABLE CREATE INDEX

ALTER TABLE

DROP TABLE DROP INDEX

Page 9: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Creating tables

create table name (attr1 domain1, attr2 domain2, …)

Can also have integrity constraints not null unique primary key (attr1, attr2, …) check (predicate)

To be a primary key, must be non-null and unique Different SQLs differ about whether not null is implied by

primary key

Page 10: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

CREATE Commands

Create the table

S (s#, Sname, Status, City)

CREATE TABLE S (s# CHAR(5),

Sname CHAR(20),

Status INT,

City CHAR(15),

PRIMARY KEY (s#)).

Page 11: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Data Definition Language

Every attribute has a domain Some common ones:

char(n): character string of fixed length n varchar(n): character string of length up to n int: integer, exact size machine-dependent smallint: another (usually smaller than int)

integer

Page 12: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

More common domains

numeric(p,d): fixed point number with p total digits, d of them to the right of the decimal point

real, double precision: floating-point numbers

float(n): floating-point with precision at least n date: a calendar date (day, month, year) time: hours, minutes, seconds datetime: a time on a date

Page 13: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

13

Example: Default Values

CREATE TABLE Students(

name CHAR(30) PRIMARY KEY,

addr CHAR(50)

DEFAULT ‘123 Sesame St.’,

phone CHAR(16)

);

Page 14: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

14

Other Keys

CREATE TABLE Product ( productID CHAR(10),

name CHAR(30),category VARCHAR(20),price INT,

PRIMARY KEY (productID), UNIQUE (name, category))

CREATE TABLE Product ( productID CHAR(10),

name CHAR(30),category VARCHAR(20),price INT,

PRIMARY KEY (productID), UNIQUE (name, category))

There is at most one PRIMARY KEY;there can be many UNIQUE

Page 15: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

15

Foreign Key Constraints

(name, category) must be a PRIMARY KEY

CREATE TABLE Purchase (prodName CHAR(30),category VARCHAR(20),

date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

CREATE TABLE Purchase (prodName CHAR(30),category VARCHAR(20),

date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

Page 16: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Example

Create a database which can use in Seaport

The database consists of 3 tables:1. Sailors (sid integer, sname char, rating

integer, age real)2. Reverses (sid, bid, day)3. Boats (bid integer, bname char, color char)

Page 17: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

DROP command

DROP TABLE BOATS

DROP TABLE REVERSES

DROP TABLE SAILORS

Page 18: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

ALTER COMMAND

ALTER TABLE Sailors ADD address

CHAR(30)

ALTER TABLE Boats DROP colors

Page 19: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

INDEX

CREATE INDEX S ON Sailors (sid)

CREATE INDEX R ON REVERSES (sid, bid,

day)

CREATE INDEX B ON Boats (bid)

DROP INDEX R

Page 20: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

20

Example 1

WorksInProfessor Department

Since Status

CREATE TABLE WorksIn ( Since DATE, -- attribute Status CHAR (10), -- attribute ProfId INTEGER, -- role (key of Professor) DeptId CHAR (4), -- role (key of Department) PRIMARY KEY (ProfId), -- since a professor works in at most

one department FOREIGN KEY (ProfId) REFERENCES Professor (Id), FOREIGN KEY (DeptId) REFERENCES Department )

Page 21: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

21

Example 2

SoldProject Part

Date Price

CREATE TABLE Sold ( Price INTEGER, -- attribute Date DATE, -- attribute ProjId INTEGER, -- role SupplierId INTEGER, -- role PartNumber INTEGER, -- role PRIMARY KEY (ProjId, SupplierId, PartNumber, Date), //WHY DATE? FOREIGN KEY (ProjId) REFERENCES Project, FOREIGN KEY (SupplierId) REFERENCES Supplier (Id), FOREIGN KEY (PartNumber) REFERENCES Part (Number) )

Supplier

Page 22: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

DML SELECT

The basic format for the SELECT command is

SELECT <attribute list>

FROM <table list>

WHERE <condition>

Page 23: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Simple SQL Query

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

SELECT *FROM ProductWHERE category=‘Gadgets’

SELECT *FROM ProductWHERE category=‘Gadgets’

Product

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks“selection”

Page 24: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Examples

Retrieve the birthdate and address of employee whose name is “John B. Smith”

SELECT Bdate, Address

FROM Employee

WHERE Name = ‘John B. Smith’

Page 25: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Example

Retrieve all Sailors name whose reverses boat id 103

SELECT sname

FROM Sailors , Reverses

WHERE Sailors.sid = Reverses.sid AND Reverses.bid=103

Page 26: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

ORDER BY

The Select command has sentential ORDER BY

Select S.sname

FROM Sailors S, Reverses R

WHERE S.sid = R.sid AND R.bid = 103

ORDER BY age DESC

Page 27: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

27

The LIKE operator

s LIKE p: pattern matching on strings p may contain two special symbols:

% = any sequence of characters _ = any single character

Product(PName, Price, Category, Manufacturer)Find all products whose name mentions ‘gizmo’:

SELECT *FROM ProductsWHERE PName LIKE ‘%gizmo%’

SELECT *FROM ProductsWHERE PName LIKE ‘%gizmo%’

Page 28: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Eliminating Duplicates

SELECT DISTINCT categoryFROM Product

SELECT DISTINCT categoryFROM Product

Compare to:

SELECT categoryFROM Product

SELECT categoryFROM Product

Category

Gadgets

Gadgets

Photography

Household

Category

Gadgets

Photography

Household

Page 29: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Joins in SQL

Connect two or more tables:

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product

Company Cname StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

What isthe connection

betweenthem ?

Page 30: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Joins

Product (pname, price, category, manufacturer)Company (cname, stockPrice, country)

Find all countries that manufacture some product in the ‘Gadgets’ category.

SELECT countryFROM Product, CompanyWHERE manufacturer=cname AND category=‘Gadgets’

SELECT countryFROM Product, CompanyWHERE manufacturer=cname AND category=‘Gadgets’

Page 31: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Joins

Product (pname, price, category, manufacturer)Purchase (buyer, seller, store, product)Person(persname, phoneNumber, city)

Find names of people living in Seattle that bought some product in the ‘Gadgets’ category, and the names of the stores they bought such product from

SELECT DISTINCT persname, storeFROM Person, Purchase, ProductWHERE persname=buyer AND product = pname AND city=‘Seattle’ AND category=‘Gadgets’

SELECT DISTINCT persname, storeFROM Person, Purchase, ProductWHERE persname=buyer AND product = pname AND city=‘Seattle’ AND category=‘Gadgets’

Page 32: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Complex Correlated Query

Product ( pname, price, category, maker, year) Find products (and their manufacturers) that are more

expensive than all products made by the same manufacturer before 1972

Powerful, but much harder to optimize !

SELECT DISTINCT pname, makerFROM Product AS xWHERE price > ALL (SELECT price FROM Product AS y WHERE x.maker = y.maker AND y.year < 1972);

SELECT DISTINCT pname, makerFROM Product AS xWHERE price > ALL (SELECT price FROM Product AS y WHERE x.maker = y.maker AND y.year < 1972);

Page 33: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

33

Aggregate Operators

COUNT (*) COUNT ( [DISTINCT] A)

A is a column SUM ( [DISTINCT] A) AVG ( [DISTINCT] A) MAX (A) MIN (A) Count the number of sailors

SELECT COUNT (*)FROM Sailors S

Page 34: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

34

Find name and age of the oldest sailor(s)

SELECT S.sname, MAX (S.age)FROM Sailors S

This is illegal, but why? Cannot combine a column with a value

SELECT S.sname, S.age FROM Sailors S

WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2)

Page 35: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Examples

What is the average distance between plazas?select avg(distfromprev) from plazas

How many events are there?select count(*) from events 29

How many distinct times are there?select count(distinct occurredat) from

events 24 How do we find the duplicates?

Page 36: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Aggregation: Count

SELECT Count(*)FROM ProductWHERE year > 1995

SELECT Count(*)FROM ProductWHERE year > 1995

Except COUNT, all aggregations apply to a single attribute

Page 37: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Aggregation: Count

COUNT applies to duplicates, unless otherwise stated:

SELECT Count(category) same as Count(*)FROM ProductWHERE year > 1995

Better:

SELECT Count(DISTINCT category)FROM ProductWHERE year > 1995

Page 38: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Aggregate functions

GROUP BY & HAVING

GROUP BY for grouping the Query results

Having to provide a condition on the group

Page 39: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Example

SELECT Pnumber, Pname, COUNT(*)

FROM PROJECT, WORKS_ON

WHERE Pnumber = PNO

GROUP BY Pnumber, Pname

HAVING COUNT(*) >2

Page 40: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Grouping and Aggregation

Usually, we want aggregations on certain parts of the relation.

Purchase(product, date, price, quantity)

Example 2: find total sales after 10/1 per product.

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > “10/1”GROUPBY product

SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > “10/1”GROUPBY product

Let’s see what this means…

Page 41: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Grouping and Aggregation

1. Compute the FROM and WHERE clauses.2. Group by the attributes in the GROUPBY3. Select one tuple for every group (and apply aggregation)

SELECT can have (1) grouped attributes or (2) aggregates.

Page 42: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

HAVING Clause

SELECT product, Sum(price * quantity)FROM PurchaseWHERE date > “9/1”GROUP BY productHAVING Sum(quantity) > 30

SELECT product, Sum(price * quantity)FROM PurchaseWHERE date > “9/1”GROUP BY productHAVING Sum(quantity) > 30

Same query, except that we consider only products that hadat least 100 buyers.

HAVING clause contains conditions on aggregates.

Page 43: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Find all authors who wrote at least 10 documents:

Attempt 1: with nested queries

SELECT DISTINCT Author.nameFROM AuthorWHERE count(SELECT Wrote.url FROM Wrote WHERE Author.login=Wrote.login) > 10

SELECT DISTINCT Author.nameFROM AuthorWHERE count(SELECT Wrote.url FROM Wrote WHERE Author.login=Wrote.login) > 10

This isSQL bya novice

Page 44: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Find all authors who wrote at least 10 documents:

Attempt 2: SQL style (with GROUP BY)

SELECT Author.nameFROM Author, WroteWHERE Author.login=Wrote.loginGROUP BY Author.nameHAVING count(wrote.url) > 10

SELECT Author.nameFROM Author, WroteWHERE Author.login=Wrote.loginGROUP BY Author.nameHAVING count(wrote.url) > 10

This isSQL byan expert

No need for DISTINCT: automatically from GROUP BY

Page 45: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

SQL

Using the following tables Sid sname rating age

22 Dustin 7 45

29 Brutus 1 33

31 Lubber 8 55.5

32 Andy 8 25.5

58 Rusty 10 35

64 Horatio 7 35

71 Zorba 10 16

74 Horatio 9 40

85 Art 3 25.5

95 bob 3 63.5

Sailors

Page 46: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Example

Sid bid Day

22 101 10/10/98

22 102 10/10/98

22 103 10/8/98

22 104 10/7/98

31 102 11/10/98

31 103 10/6/98

31 104 10/12/98

64 101 9/5/98

64 102 9/8/98

74 103 9/8/98

Bid Banem Color

101 Interlake Blue

102 Interlake Red

103 Clipper Green

104 Marine Red

Reverses

Boats

Page 47: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Queries

Find the names and ages of all sailors

Find all sailors with rating above 7

Find the name of sailors who have reserved

boat number 104

Page 48: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

48

Page 49: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

49

Quiz

Write an SQL query for following, and show the result on the example tables: Find all course names that were not taught in

1997 List the professor names along with all the

departments ids where a professor has taught Find the names of students who took courses

in all the departments that offered a course in Fall of 1995 or 1997.

Page 50: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Exercises Product (pname, price, category, manufacturer)Purchase (buyer, seller, store, product)Company (cname, stock price, country)Person(per-name, phone number, city)

Ex #1: Find people who bought telephony products.Ex #2: Find names of people who bought American productsEx #3: Find names of people who bought American products and they live in Seattle.Ex #4: Find people who have both bought and sold something.Ex #5: Find people who bought stuff from Joe or bought products from a company whose stock prices is more than $50.

Page 51: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Mutating (non-read-only) queries

Deletion Deletes whole tuples

Insertion Inserts whole tuples

Update Changes values of attributes

Can generally do these to tables but not to views or other derived entities Note that renaming is not in itself derivation So a renamed relation can be mutated like the original

Page 52: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Update

update name set assignment where P

where clause is optional (selects tuples to be updated)

Give Jane Swift a $20 balanceupdate subscribers set balance = 20 where

firstname=“Jane” and lastname=“Swift” Give every subscriber a $3 rebate

update subscribers set balance = balance + 3

Page 53: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Update examples

UPDATE P

SET Color = ‘Yellow’

Weight = Weight + 5

City = NULL

WHERE P# = 2

Page 54: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Deletion

delete from name where P

Delete all events from transponder 72delete from events where tid=72

Delete all events relating to George Bushdelete from events where tid in

select from transponders as t, subscribers as s where t.tid = s.tid and s.firstname=“George” and s.lastname=“Bush”

Page 55: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Delete example

DELETE

FROM S

WHERE S# =1

Page 56: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

56

Insertions

General form:

Missing attribute NULL.May drop attribute names if give them in order.

INSERT INTO R(A1,…., An) VALUES (v1,…., vn) INSERT INTO R(A1,…., An) VALUES (v1,…., vn)

INSERT INTO Purchase(buyer, seller, product, store) VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’)

INSERT INTO Purchase(buyer, seller, product, store) VALUES (‘Joe’, ‘Fred’, ‘wakeup-clock-espresso-machine’, ‘The Sharper Image’)

Example: Insert a new purchase to the database:

Page 57: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Insert example

INSERT

INTO P(P#, City , Weight)

VALUES (7 , ‘athens’, 24 )

Page 58: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Grant & revoke operations

the format are

GRANT operation ON table TO user

REVOKE operation FROM user

Page 59: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Examples

GRANTINSERT, DELETE ON TABLE reserves TO ahmed

GRANT ALL ON TABLE saliros TO hassan

REVOKE SELECT ON TABLE boats FROM manal

Page 60: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Grant Example

GRANT SELECT ON TABLE S TO u3

WITH GRANT OPTION

REVOKE DELETE ON TABLE S FROM U2

Page 61: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

61

Starwars Exercises

char(name, race, homeworld, affiliation)

planets(name, type, affiliation)

timetable(cname, pname, movie, arrival, departure)

Which planet does Princess Leia go to in movie3?

SELECT distinct pname

FROM timetable

WHERE cname ='Princess Leia' and movie=3;

Page 62: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

62

Starwars Exercises

char(name, race, homeworld, affiliation)planets(name, type, affiliation)timetable(cname, pname, movie, arrival, departure)

How many humans stay on Dagobah in movie 3?

SELECT count(*) FROM timetable, characters WHERE movie=3 and pname =‘Dagobah’ and timetable.cname=characters.name and characters.race=‘Human’;

Page 63: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

63

Starwars Exercises

char(name, race, homeworld, affiliation)planets(name, type, affiliation)timetable(cname, pname, movie, arrival, departure)

Who has been to his/her homeworld in movie 2?

SELECT distinct c.name FROM characters c, timetable t WHERE c.name=t.cname and t.pname=c.homeworld

and movie=2;

Page 64: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

64

Starwars Exercises

char(name, race, homeworld, affiliation)planets(name, type, affiliation)timetable(cname, pname, movie, arrival, departure)

Find distinct names of the planets visited by those of race “droid”.

SELECT distinct t.pname FROM char c, timetable tWHERE c.name=t.cname and c.race=‘droid’;

Page 65: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

65

Starwars Exercises

char(name, race, homeworld, affiliation)planets(name, type, affiliation)timetable(cname, pname, movie, arrival, departure)

For each character and for each neutral planet, how much time total did the character spend on the planet?

SELECT c.name, p.name, SUM(t.departure-t.arrival) as amount

FROM characters c, timetable t, planets p WHERE t.cname=c.name and t.pname=p.name and p.affiliation='neutral' GROUP BY c.name, p.name;

Page 66: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

EXERCISE 1: Queries

1.First and last name of employees who have no supervisor.2.First and last name of employees supervised

by Franklin Wong.3.Last name of employees who have dependents.4.Last name of employees who have daughters.5.Last name of employees in department 5 who

work more than 10 hours/week on ProductX.6.Last name of supervisors of employees in department 5

who work more than 10 hours/week on ProductX.7.First and last names of all department managers.8.Salaries of all employees

who have worked on the Reorganization project.9.SSN of all employees who have worked on a project

that is controlled by a department different than the department that they are assigned to.

10.Last name of all employees who are not married.

Page 67: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

EXERCISE 1: Schema

Page 68: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

EXERCISE 1: Instance

Page 69: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

EXERCISE 2: Queries

1.List all airplane types that can land at any airport in San Francisco.

2.List the ids and number of seats for all airplanes that can land at any airport in Chicago.

3.List the name and phone number of all customers with a seat reserved on a flight that leaves Chicago O’Hara airport (ORD) on October 31, 2008.

4.List all airlines that have seats available for flights leaving Los Angeles (LAX) on September 25, 2008.

5.List all airlines that operate at San Jose International Airport (SJC).

Page 70: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

Exercise 2: Schema

Page 71: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

EXERCISE 3: Queries

1.Count the number of overdue books.

2.How many books by author Harry Crews are in the database?

3.Determine the number of library cards assigned to each borrower phone number.

4.Find names of all borrowers who do not have any book loans.

5.Do any library branches have every book?

Page 72: Database SQL. Relational language SQL SQL or Structured Query Language SQL is an interactive Query language and database programming Language

EXERCISE 3: Schema