0% found this document useful (0 votes)
12 views28 pages

Lec 9

Uploaded by

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

Lec 9

Uploaded by

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

DATABASE SYSTEMS

Dr. Noha Nagy

Lecture 9
Inserting from Another Table
2

MyCustomers( id, name, city)


Insert all customers from the table “Customers” to table
“MyCustomers”

insert into myCustomers


Select customerNumber, customerName, city
from customers;
SQL SYNTAX
3

SELECT <Column list> Single table


FROM <table names> Multiple tables
(Join)
[WHERE <Condition>]
[GROUP BY <Column list>]
[HAVING <Condition>]
[ORDER BY <Column list>]
Left Join
4

Table One Table Two


X A X B
1 a 2 x
4 d 3 y
2 b 5 v

select *
from one left join two
on one.x = two.x;

X A X B
1 a .
2 b 2 x
4 d .
s105d07
Right Join
5
Table Two Table One
X B X A
2 x 1 a
3 y 4 d
5 v 2 b

select *
from two right join one
on one.x = two.x;

X B X A
. 1 a
2 x 2 b
. 4 d
Full Join
6

Table One Table Two


X A X B
1 a 2 x
4 d 3 y
2 b 5 v

select *
from one full join two
on one.x = two.x;
X A X B
1 a .
2 b 2 x
. 3 y
4 d .
. 5 v
Aliases and ‘Self-Joins’
7

Aliases can be used to copy a


table, so that it can be combined
with itself:
Employee
Get the names of all employees Name Dept
who work in the same department John Marketing
as Andy.
Mary Sales
Peter Sales
Andy Marketing
Anne Marketing
Aliases and Self-Joins
8

SELECT … FROM Employee A, Employee B


WHERE A.Dept = B.Dept AND B.Name = ‘Andy’
A.Name A.Dept B.Name B.Dept
John Marketing Andy Marketing
Andy Marketing Andy Marketing
Anne Marketing Andy Marketing
Aliases and Self-Joins
9

SELECT A.Name FROM Employee A, Employee B


WHERE A.Dept = B.Dept AND B.Name = ‘Andy’

A.Name
John
Andy
Anne

The result is the names of all employees who work in the


same department as Andy.
Set operator
10

 Union U
 Intersection ∩
 Difference -
The two relation
must be union
compatible
Union Operator
11

 The UNION operator is used to combine the result-set of


two or more SELECT statements.
 The relations need to be union compatible
 Each SELECT statement within UNION must have the same
number of columns
 The columns must also have similar data types

 The columns in each SELECT statement must also be in the


same order
Union
12
Customers(cid,cname,city)
Suppliers(sid,sname,city)

• Get the cities of all customers and suppliers in


ascending order
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Union
13
Customers(cid,cname,city, country)
Suppliers(sid,sname,city, country)

 If some customers or suppliers have the same city,


each city will only be listed once, because UNION
selects only distinct values.
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
SELECT City FROM Customers City

UNION Ann Arbor

SELECT City FROM Suppliers Berlin

ORDER BY City; London

Mexico D.F.

New Orleans
To select duplicate values
15

SELECT City FROM Customers


UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
City

Ann Arbor

Berlin

London

Mexico D.F.

Mexico D.F.

New Orleans
Exercise
16
 selects all the different German cities (only distinct
values) and the country from "Customers" and
"Suppliers“
 Customers(CID,Cname,City,Country)
 Supplier(SID,Sname, City,Country)
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Borrower( ID, Customer_name, amount)
Depositor( ID, customer_name, balance)
Set Operations
17
Find all customers who have both a loan, an account or both.
(select customer_name from depositor)
union
(select customer_name from borrower)
Find all customers who have both a loan and an account.
(select customer_name from depositor)
intersect
(select customer_name from borrower)
Find all customers who have an account but no loan.
(select customer_name from depositor)
except
(select customer_name from borrower)
Processing Multiple Tables -- Subqueries
18

 Subquery = placing an inner query (SELECT statement)


inside an outer query
 Options:
 In a condition of the WHERE clause
 As a “table” of the FROM clause
 Within the HAVING clause
Subqueries Types
19

1- Subqueries that return a single, scalar value


 operator (=, <, >, <>)

2- Subqueries that operate on lists but the values must be


from a single column of a table.
 IN
 ANY
 ALL
3- Subqueries that use the EXISTS operator to test the
existence of data rows satisfying specified criteria.
Example
20
Customer(Cid, Cname, Address, Phone)
Order(Oid,Date,customer_ID)
 Show all customers who have placed an order
The IN operator will test to see if the
SELECT Cname CUSTOMER_ID value of a row is
included in the list returned from the
From Customer subquery
WHERE Cid IN
(SELECT DISTINCT CUSTOMER_ID FROM ORDER);

Subquery is embedded in
parentheses. In this case it
returns a list that will be used
in the WHERE clause of the
outer query
Example
21

 Get all the products names that have been sold at least
once.

Product ProductSales
Pid Name Description ID Pid Unit price Qty

1 TV 52 inch 1 1 450 7

High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
22

Get all the products that have been sold at least once. 

Select Pid, Name


From Product
Outer
Where Pid in (select distinct pid from ProductSales)
Query
Product ProductSales
Pid Name Description ID Pid Unit price Qty Inner
1 TV 52 inch 1 1 450 7
Query
High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
 Get all the products that have been sold at least once.
23

Select Pid, Name


From Product
Where Pid in (select distinct pid from ProductSales)
Subquery Select Pid, Name
From Product ,ProductSales
Where product.Pid= ProductSales.Pid)
Product ProductSales
Pid Name Description ID Pid Unit price Qty Join
1 TV 52 inch 1 1 450 7

High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
Exercise 1
24

 Get all the product’s names that not sold.


 Get all the product’s id that not sold Write these queries
in two different
ways
Product ProductSales
Pid Name Description ID Pid Unit price Qty

1 TV 52 inch 1 1 450 7

High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
Exercise
25

 Get all the products that not sold.


Select Pid, Name
From Product
Where Pid not in (select distinct pid from ProductSales)

Product ProductSales
Pid Name Description ID Pid Unit price Qty

1 TV 52 inch 1 1 450 7

High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
Exercise
26

 Get all the products id that not sold.

Select Pid
From Product
Except
Select Pid
From productSales
Product
ProductSales
Pid Name Description
ID Pid Unit price Qty
1 TV 52 inch
1 1 450 7
High
2 Camera
resolution
2 3 1200 8
Very thin
3 Laptop
black color 3 3 1200 20
Exercise 2
28
Employee(SSN, Fname, Lname, Salary, DepId)
Dependent(DSSN, Name, SSN)
Department(DepId,Depname, Location)
Retrieve the name of each employee who has a dependent
with the same first name as the employee.[ write in two different ways]
Exercise 2
29
Employee(SSN, Fname, Lname, Salary, DepId)
Dependent(DSSN, Name, SSN)
Department(DepId,Depname, Location)
Retrieve the name of each employee who has a dependent
with the same first name as the employee.[ write in two different ways]
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT SSN
FROM DEPENDENT As D
WHERE E.FNAME=D.NAME)

SELECT E.FNAME, E.LNAME


FROM EMPLOYEE E, DEPENDENT D
WHERE E.SSN=D.SSN AND
E.FNAME=D.NAME

You might also like