Вы находитесь на странице: 1из 19

Relationsalgebra operationer

Selection Projection Set operationer

Cartesian product R x S

Union R S

Intersection : R S

Difference : R - S,

S - R

Division : R ÷ S Join

equi

theta

natural

inner

left / right outer

full outer

semi

Input: mängd Output: mängd
Input: mängd
Output: mängd

Relationsalgebra operationer

Input: mängd Output: mängd Relationsalgebra operationer Selection Projection ab12 ab34 cd12 cd34 ef 12 ef 34
Input: mängd Output: mängd Relationsalgebra operationer Selection Projection ab12 ab34 cd12 cd34 ef 12 ef 34

Selection

Projection ab12 ab34 cd12 cd34 ef 12 ef 34
Projection
ab12
ab34
cd12
cd34
ef
12
ef
34
a b 1 2 X c d 3 4 e f Kartesisk Produkt
a
b
1
2
X
c
d
3
4
e
f
Kartesisk Produkt

=

S : 3 tuplar och 2 kolumner R : 2 tupel och 2 kolumner S
S
: 3 tuplar och 2 kolumner
R
: 2 tupel och 2 kolumner
S * R
3
* 2 = tuplar
2
+ 2 = 4 kolumner
©Lunds Universitet-Informatik-Erdogan Uçan 1 ©Lunds Universitet-Informatik-Erdogan Uçan 2 Set operationer SQL
©Lunds Universitet-Informatik-Erdogan Uçan
1
©Lunds Universitet-Informatik-Erdogan Uçan
2
Set operationer
SQL
DDL Skapa tabell
=
=
-
=
=
÷
studenter som läser alla kurser som Anna läser
a
1
a
2
b
1
b
3
a
1
÷
=
c
1
c
Övningstabeller
2
c
2
Division
©Lunds Universitet-Informatik-Erdogan Uçan
3
©Lunds Universitet-Informatik-Erdogan Uçan
4
SQL DDL insättning SQL DDL Skapa tabell Övningstabeller Övningstabeller ©Lunds Universitet-Informatik-Erdogan
SQL
DDL insättning
SQL
DDL Skapa tabell
Övningstabeller
Övningstabeller
©Lunds Universitet-Informatik-Erdogan Uçan
5
©Lunds Universitet-Informatik-Erdogan Uçan
6
SQL
DDL insättning
SQL
DML: Projektion-Selection
Anställd
insert into Student values('P1','anna','lund','046-111') ;
insert into Student values('P2','anna','malmö','040-111') ;
insert into Student values('P3','eva','lund','046-222');
insert into Student values('P4','eva','malmö','040-222');
insert into Student values('P5','hans','eslöv','042-111');
pnr
namn
adress
lon
ansttid
avdnr
Övningstabeller
1 hans
Lund
10000
22-JUL-98
A1
2 eva
Malmö
20000
20-JUL-97
A2
3 eva
Dalby
12000
22-APR-98
A1
4 anna
Malmö
19000
22-DEC-98
A2
insert into Kurs values('K1','dat001','lund',10) ;
insert into Kurs values('K2','dat002','lund',5) ;
insert into Kurs values('K3','dat003','malmö',20);
insert into Kurs values('K4','inf001','lund',10);
insert into Kurs values('K5','inf002','lund',5);
insert into Kurs values('K6','inf003','malmö',20);
insert into Kurs values('K7','inf004','dalby',20);
5 lars
Eslöv
1200
22-NOV-98
A3
SELECT namn, avdnr
FROM anst
SELECT namn, avdnr
FROM anst
WHERE avdnr = ‘A1’
namn
avdnr
--------
-----------
namn
avdnr
hans
A1
insert into Laser values('P1','K1',6);
insert into Laser values('P1','K2',7);
insert into Laser values('P1','K3',8);
insert into Laser values('P1','K4',6);
--------
-----------
Selection och
eva
A2
Projektion
hans
A1
projektion
eva
A1
eva
A1
anna
A2
lars
A3
insert into Laser values('P2','K1',7);
insert into Laser values('P2','K2',9);
insert into Laser values('P2','K3',7);
spnr NyBetyg
insert into Laser values('P3','K1',6);
SELECT spnr, betyg * 1.5 NyBetyg
FROM laser
WHERE spnr = 'P3'
----- ---------------
beräkning med fält
P3
9.0
©Lunds Universitet-Informatik-Erdogan Uçan
7
©Lunds Universitet-Informatik-Erdogan Uçan
8

SQL Operatorer

Relationsoperatorer : > , <, !=, >=, <=, =

SELECT * FROM anst WHERE lon > 15000

SELECT namn FROM anst WHERE lon != 12000

Logiska operatorer: AND, OR, NOT

SELECT namn FROM anst WHERE lon > 12000 AND lon < 20000

SELECT namn FROM anst WHERE lon > 12000 OR adress ='Lund'

SQL -Operatorer

SQL Operatorer

Andra ops: LIKE,IN, ANY,SOME,EXISTS,BETWEEN, ISNULL

SELECT SELECT namn namn

FROM FROM anst anst

WHERE WHERE namn namn LIKE LIKE 'e%' 'e%'

SELECT SELECT * *

FROM FROM kurs kurs

WHERE WHERE kadress kadress LIKE LIKE 'lu_d%' 'lu_d%'

SELECT SELECT * *

FROM FROM kurs kurs

WHERE WHERE kadress kadress LIKE LIKE '%lund%' '%lund%'

-- alla namn som börjar med e

-- vilket tecken som helst i stället för _

-- vilken adress innehåller lund ?

©Lunds Universitet-Informatik-Erdogan Uçan 9 ©Lunds Universitet-Informatik-Erdogan Uçan 10 SQL Operatorer SQL
©Lunds Universitet-Informatik-Erdogan Uçan
9
©Lunds Universitet-Informatik-Erdogan Uçan
10
SQL Operatorer
SQL
Standart funktioner - aggregate
SELECT namn
FROM anst
WHERE adress IN ('lund', 'malmö')
SELECT COUNT(*) AS ANTALANSTÄLLDA
FROM anst
• COUNT(*) räknar även null
ANTALANSTÄLLDA
• SUM, AVG, COUNT, MAX, och
--------------
INSERT INTO kurs VALUES('K31',NULL,'eslöv',15)
5
MIN ignorer null värde
SELECT COUNT(*) AS ANTALANSTÄLLDA
SELECT *
FROM kurs
WHERE kname = NULL
FROM anst
WHERE avdnr = 'A1'
create table test
(id int, name varchar(10))
insert into test values(1, 'eva')
0 rows selected
ANTALANSTÄLLDA
insert into test values(null,'eva')
insert into test values(null,'anna')
--------------
SELECT *
FROM kurs
WHERE kname IS NULL
2
select count(id) from test
select count(*) from test
-- 1
-- 3
SELECT MAX(pnr)
FROM anst
select distinct * from test
select distinct name from test
-- 3
-- 2
KKOD KNAME
KADRESS
POANG
--------------
-----
K31 NULL
1 row selected.
----------
----------
----------
5
eslöv
15
MAX, MIN, AVG, SUM, COUNT
©Lunds Universitet-Informatik-Erdogan Uçan
11
©Lunds Universitet-Informatik-Erdogan Uçan
12

ORDER BY clause

 

spnr kkod betyg

         

----- ----- -----------

SPNR

KKOD

BETYG

P1

K1

6

P1

K1

6

P2

K1

7

P1

K2

7

P3

K1

6

P1

K3

8

P2

K2

9

 

P1

K4

6

P1

K2

7

P2

K1

7

P1

K3

8

P2

K2

9

P2

K3

7

P2

K3

7

P1

K4

6

P3

K1

6

 

Lite mer

SET ROWCOUNT 4

-- SET ROWCOUNT 0 tillbaka till normal

SELECT upper(kkod)U, substring(kkod,2,1)S, 'har' H, -betyg B, 2+4 M

FROM laser

 

U

U

S S

H H

B B

M M

 

----------

----------

---- ---- ---- ---- ----------- ----------- ----------- -----------

K1 K1

1 1

har har -6 -6

6 6

K2

K2

2 2

har har -7 -7

6 6

K3

K3

3 3

har har -8 -8

6 6

K4

K4

4 4

har har -6 -6

6 6

SELECT top 3 'DB är rolig' FROM laser SELECT 'DB är rolig' FROM laser WHERE
SELECT top 3 'DB är rolig'
FROM laser
SELECT 'DB är rolig'
FROM laser
WHERE rownum < 4;
SQL Server
-----------
-----------
DB DB är är rolig rolig
Oracle
DB DB är är rolig rolig
DB är rolig
DB är rolig
SELECT kkod+spnr KkodOchPnr, kkod
FROM laser
KkodOchPnr KkodOchPnr
kkod kkod
------------------ ------------------
-------- --------
K1P1 K1P1
K1 K1
K2P1 K2P1
K2 K2

spnr kkod betyg ----- ----- -----------

P1

K1

6

P2

K1

7

P3

K1

6

P1

K2

7

 

P2

K2

9

P1

K3

8

P2

K3

7

P1

K4

6

9 P1 K3 8 P2 K3 7 P1 K4 6 Asc eller desc Null räknas minst
Asc eller desc Null räknas minst Sorteringsorder : Collation
Asc eller desc
Null räknas minst
Sorteringsorder : Collation
©Lunds Universitet-Informatik-Erdogan Uçan 13 ©Lunds Universitet-Informatik-Erdogan Uçan 14 Lite mer Update och
©Lunds Universitet-Informatik-Erdogan Uçan
13
©Lunds Universitet-Informatik-Erdogan Uçan
14
Lite mer
Update och delete
SELECT ISNULL(name, 'okänd')
FROM test
SELECT DECODE
(sadress,null,'ingen', sadress)
UPDATE anst
SET namn = 'Eva'
FROM student;
UPDATE anst
SET namn = 'Eva'
WHERE namn = 'eva'
SQL Server
----------
----------
eva eva
Oracle
eva eva
anna anna
DELETE anst
WHERE namn = 'eva'
DELETE anst
okänd okänd
update och delete kan ingå i JOIN, SUBQUERY osv
som i SELECT
©Lunds Universitet-Informatik-Erdogan Uçan
15
©Lunds Universitet-Informatik-Erdogan Uçan
16

Gruppering

SELECT AVG(betyg) FROM laser ----------- 7
SELECT AVG(betyg)
FROM laser
-----------
7

AVG(BETYG) : (single set) scalar aggregate query

SPNR

KKOD

BETYG

P1

K1

6

P1

K2

7

P1

K3

8

P1

K4

6

P2

K1

7

P2

K2

9

P2

K3

7

P3

K1

6

SELECT spnr, AVG(betyg) FROM laser

select spnr, AVG(betyg) from laser

*

ERROR at line 1:

ORA-00937: not a single-group group function

Server: Msg 8118, Level 16, State 1, Line 1 Column 'laser.spnr' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

 

Gruppering

SELECT spnr, AVG(betyg) Genomsnitt , COUNT(betyg) Antal

FROM laser

 

GROUP BY spnr

 

spnr : rad värde AVG(betyg), COUNT(betyg) : set värdevector aggregate query (multiplesetsmultiplesets )

spnr Genomsnitt Antal

-----

-----------

-------

P1

6.75

4

P2

7.6

3

 

P3

6

1

laser

P1

K1

6

P1

K2

7

P1

K3

8

P1

K4

6

P2

K1

7

P2

K2

9

P2

K3

7

P3

K1

6

©Lunds Universitet-Informatik-Erdogan Uçan 17 ©Lunds Universitet-Informatik-Erdogan Uçan 18 Gruppering Gruppering
©Lunds Universitet-Informatik-Erdogan Uçan
17
©Lunds Universitet-Informatik-Erdogan Uçan
18
Gruppering
Gruppering
SELECT spnr
FROM laser
GROUP BY spnr, kkod, betyg
--ok allt som finns i GROUP BY lista behöver inte finnas i SELECT lista
SELECT COUNT(*)antal, betyg
FROM laser
GROUP BY betyg
laser
antal
betyg
P1
K1
6
P1
K1
6
------
------
P1
K2
7
P1
K4
6
3
3
6
3
7
P1
K3
8
P3
K1
6
SELECT spnr,betyg
FROM laser
GROUP BY spnr
1
8
P1
K4
6
P1
K2
7
1
9
P2
K1
7
P2
K1
7
3
P2
K2
9
P2
K3
7
SELECT namn,adress
P2
K3
7
P1
K3
8
*
1
ORA-00979: not a GROUP BY expression
P3
K1
6
P3
K1
9
1
-- inte ok allt som finns i GROUP BY lista måste finnas i SELECT lista
©Lunds Universitet-Informatik-Erdogan Uçan
19
©Lunds Universitet-Informatik-Erdogan Uçan
20
Gruppering GROUP BY eliminerar dupletter laser SELECT COUNT(*)antal,spnr,betyg FROM laser GROUP BY spnr,betyg spnr
Gruppering
GROUP BY eliminerar dupletter
laser
SELECT COUNT(*)antal,spnr,betyg
FROM laser
GROUP BY spnr,betyg
spnr
sname
sadress
stel
P1
K1
6
P1
6
P1
Anna
Lund
111
P1
K2
7
P1
6
P2
Anna
Lund
222
antal
spnr betyg
P1
K3
8
P3
6
P3
Eva
Lund
333
--------
-----
--------
P1
K4
6
P1
7
2
P1
6
SELECT sname
SELECT
DISTINCT sname
P2
K1
7
P2
7
1
P3
6
FROM student
FROM student
1
P1
7
P2
K2
9
P2
7
sname
2
P2
7
sname
P2
K3
7
P1
8
----------
1
P1
8
----------
1
P2
9
P3
K1
6
P2
9
Anna
Anna
Anna
laser
Eva
SELECT COUNT(*)antal,spnr,betyg
FROM laser
GROUP BY betyg,spnr
eva
P1
K1
6
P1
6
P1
K2
7
P1
6
SELECT sname
antal
spnr betyg
P1
K3
8
P1
7
FROM student
GROUP BY sname
--------
-----
------
SELECT sname, COUNT(*) antal
FROM student
GROUP BY sname
P1
K4
6
P1
8
2
P1
6
sname
sname
antal
P2
K1
7
P2
7
1
P1
7
----------
---------- -----------
1
P1
8
P2
K2
9
P2
7
Anna
2
P2
7
anna
2
P2
K3
7
P2
9
Eva
1
P2
9
eva
1
1
P3
6
P3
K1
6
P3
6
©Lunds Universitet-Informatik-Erdogan Uçan
21
©Lunds Universitet-Informatik-Erdogan Uçan
22
WHERE vs HAVING
WHERE vs HAVING
laser
SELECTSELECT
spnrspnr,, COUNT(*)COUNT(*) AntalAntal,,
MAX(betygMAX(betyg)) MaximumMaximum
P1
K1
6
P1
7
FROMFROM laserlaser
WHEREWHERE betygbetyg >> 66
P1
K2
7
P1
8
*
WHERE kommer efter FROM
HAVING kommer efter GROUP BY
P1
K3
8
P2
7
GROUP BY spnr
P1
K4
6
P2
9
P2
K1
7
P2
7
*
WHERE kan inte innehålla aggretages
HAVING kan
spnr Antal
Maximum
P2
K2
9
-----
---------
-----------
P1
2
8
P2
K3
7
P2
3
9
*
WHERE eliminerar tuplar innan grouping
HAVING eliminerar efter grouping
P3
K1
6
SELECTSELECT
spnrspnr,, COUNT(*)COUNT(*) AntallAntall,, MAX(betygMAX(betyg))
spnr Antal MAX
-----
----- -----
FROMFROM laserlaser
GROUPGROUP BYBY spnrspnr
P1
4
8
P2
3
9
P3
1
6
HAVING MAX(betyg) > 6
spnr Antal Maximum
-----------
----- ------
P1
4
8
P2
3
9
©Lunds Universitet-Informatik-Erdogan Uçan
23
©Lunds Universitet-Informatik-Erdogan Uçan
24

WHERE vs HAVING

SELECT spnr, count(*) antal, max(betyg) maximum FROM laser GROUP BY spnr HAVING betyg > 6

Betyg not contained in either an aggregate function or the GROUP BY clause.

SELECT spnr, betyg ,COUNT(*) Antal FROM laser

GROUP BY spnr,betyg HAVING betyg =6

spnr betyg Antal

----- -------- -----------

P1

6

2

P3

6

1

laser

P1

K1

6

P1

K2

7

P1

K3

8

P1

K4

6

P2

K1

7

P2

K2

9

P2

K3

7

P3

K1

6

WHERE vs HAVING

SELECT spnr, count(*) antal, max(betyg) maximum FROM laser

GROUP BY spnr

HAVING spnr > ‘P1’

spnr antal

maximum

----- -----------

-----------

P2

3

9

P3

1

6

SELECT spnr, COUNT(*) Antal, AVG(betyg) Gnitt FROM laser GROUP BY spnr HAVING AVG(betyg) > 5

spnr Antal

Gnitt

----- -----------

-----------

P1

4

6.75

P2

3

7.6

P3

1

6

laser

P1

K1

6

P1

K2

7

P1

K3

8

P1

K4

6

P2

K1

7

P2

K2

9

P2

K3

7

P3

K1

6

 

©Lunds Universitet-Informatik-Erdogan Uçan

 

25

©Lunds Universitet-Informatik-Erdogan Uçan

 

26

 

Kartesisk produkt

 

Join

 
KartesiskKartesisk produktprodukt:: student * laser 3* 3 = 9 rader 4 + 3 = attribut
KartesiskKartesisk produktprodukt::
student * laser
3* 3 = 9 rader
4 + 3 = attribut
 

SELECT * FROM student s, laser l WHERE s.spnr = l.spnr

   
 

SELECT * FROM student, laser

   

spnrspnr :: JOINJOIN kolumn;kolumn; JOINJOIN kompatibelkompatibel VanligtvisVanligtvis PKPK och/ellleroch/elller FKFK

 
 

spnr

sname

sadress

stel

spnr

kkod

betyg

 

Student

Laser

P1

Anna

Lund

111

P1

K1

4

 

spnr

sname

sadress

 

stel

   

spnr

kkod

betyg

 

P1

Anna

Lund

111

P1

K2

5

                     

P1

Anna

Lund

 

111

 

P1

 

K1

 

4

 

P1

Anna

Lund

111

P3

K1

6

P2

Anna

Malmö

222

P1

K1

4

P2

Anna

Malmö

 

222

P1

K2

5

P2

Anna

Malmö

222

P1

K2

5

P3

Eva

Lund

 

333

P3

K1

6

P2

Anna

Malmö

222

P3

K1

6

 

P3

Eva

Lund

333

P1

K1

4

 

spnr

sname

sadress

stel

spnr

 

kkod

betyg

 

P3

Eva

Lund

333

P1

K2

5

P1

Anna

Lund

111

P1

 

K1

4

P3

Eva

Lund

333

P3

K1

6

P1

Anna

Lund

111

P1

 

K2

5

 

KP

+

 
P1 Anna Lund 111 P1   K2 5   KP +    
 
             

P1

Anna

Lund

111

P3

 

K1

6

selektion

P2

Anna

Malmö

222

P1

 

K1

4

P2

Anna

Malmö

222

P1

 

K2

5

 

spnr

sname

sadress

stel

spnr

kkod

betyg

 

P2

Anna

Malmö

222

P3

 

K1

6

P1

Anna

Lund

111

P1

K1

4

P3

Eva

Lund

333

P1

 

K1

4

P1

Anna

Lund

111

P1

K2

5

P3

Eva

Lund

333

P1

 

K2

5

P3

Eva

Lund

333

P3

K1

6

P3

 

Eva

Lund

333

P3

 

K1

6

 
 
 

©Lunds Universitet-Informatik-Erdogan Uçan

 

27

©Lunds Universitet-Informatik-Erdogan Uçan

 

28

Natural Join

Student

 

Laser

spnr

sname

sadress

stel

spnr

kkod

Betyg

P1

Anna

Lund

111

P1

K1

4

P2

Anna

Malmö

222

P1

K2

5

P3

Eva

Lund

333

P3

K1

6

SELECT s.*, l.kkod, l.betyg FROM student s, laser l WHERE s.spnr = l.spnr

l.betyg FROM student s, laser l WHERE s.spnr = l.spnr spnr sname sadress stel kkod Betyg

spnr

sname

sadress

stel

kkod

Betyg

P1

Anna

Lund

111

K1

4

P1

Anna

Lund

111

K2

5

P3

Eva

Lund

333

K1

6

111 K2 5 P3 Eva Lund 333 K1 6 SELECT s.*, l.kkod, l.betyg FROM student2 s

SELECT s.*, l.kkod, l.betyg FROM student2 s INNER JOIN laser2 l ON s.spnr = l.spnr

-- Samma resultat :

SQL 92 ; INNER behövs inte;

default

Theta Join

SELECT s.spnr,l.spnr FROM student s, laser l WHERE s.spnr > l.spnr

spnr spnr ----- ----- P2 P1 P2 P1 P3 P1 P3 P1
spnr spnr
----- -----
P2
P1
P2
P1
P3
P1
P3
P1

Theta Join

 

spnr

sname

sadress

stel

spnr

kkod

betyg

P1

Anna

Lund

111

P1

K1

4

P1

Anna

Lund

111

P1

K2

5

P1

Anna

Lund

111

P3

K1

6

P2  Anna Malmö 222 P1 K1 4

 

Anna

Malmö

222

P1

K1

4

P2  Anna Malmö 222 P1 K2 5

 

Anna

Malmö

222

P1

K2

5

P2

Anna

Malmö

222

P3

K1

6

P3  Eva Lund 333 P1 K1 4

 

Eva

Lund

333

P1

K1

4

P3  Eva Lund 333 P1 K2 5

 

Eva

Lund

333

P1

K2

5

P3

Eva

Lund

333

P3

K1

6

Först KP

sedan

tillämpa

join vilkor

©Lunds Universitet-Informatik-Erdogan Uçan 29 ©Lunds Universitet-Informatik-Erdogan Uçan 30 Själv join Själv
©Lunds Universitet-Informatik-Erdogan Uçan
29
©Lunds Universitet-Informatik-Erdogan Uçan
30
Själv join
Själv join
Student
Student
spnr
sname
sadress
spnr
sname
sadress
P1
Anna
Lund
P1
Anna
Lund
P2
Anna
Malmö
P2
Anna
Malmö
-- hjälper inte !
SELECT distinct s1.spnr,
s1.sname, s1.sadress
FROM student s1, student s2
WHERE s1.sadress = s2.sadress
P3
Eva
Lund
P3
Eva
Lund
spnr sname
sadress
-----
--------
------------
SELECT *
FROM student s1, student s2
WHERE s1.sadress = s2.sadress
-- ger samma relation; hjälper inte !
P1
Anna
Lund
P2
Anna
Malmö
SELECT *
FROM student s
WHERE s.sadress = s.sadress
P3
Eva
Lund
SELECT s1.spnr, s1.sname, s1.sadress
spnr sname
sadress
FROM student s1, student s2
P1
Anna
Lund
P1
Anna
Lund
-----
--------
------------
P3
Eva
Lund
P1
Anna
Lund
P1
Anna
Lund
WHERE s1.sadress = s2.sadress
AND s1.spnr != s2.spnr
P2
Anna
Malmö
P2
Anna
Malmö
P2
Anna
Malmö
P1
Anna
Lund
P3
Eva
Lund
P3
Eva
Lund
spnr sname
sadress
P3
Eva
Lund
P3
Eva
Lund
----- ----------
----------
P1
Anna
Lund
P3
Eva
Lund
Vilka personer bor på samma adress ?
©Lunds Universitet-Informatik-Erdogan Uçan
31
©Lunds Universitet-Informatik-Erdogan Uçan
32

Själv join - Rekursiv

Själv join

Själv join

anstnr

anstamn

anstadress lön

chefnr

anstnr

anstamn

anstadress lön

chefnr

anstnr

anstamn

anstadress lön

chefnr

anstnr

anstamn

anstadress lön

chefnr

---------

----------

----------

----

-------

---------

----------

----------

----

-------

---------

----------

----------

----

-------

---------

----------

----------

----

-------

A1

Anna

Lund

10

NULL

A1

Anna

Lund

10

NULL

A1

Anna

Lund

10

NULL

A1

Anna

Lund

10

NULL

A2

Eva

Lund

20

A1

A2

Eva

Lund

20

A1

A2

Eva

Lund

20

A1

A2

Eva

Lund

20

A1

A3

Anna

Malmö

5

A2

A3

Anna

Malmö

5

A2

A3

Anna

Malmö

5

A2

A3

Anna

Malmö

5

A2

A4

Lars

Eslöv

15

A2

A4

Lars

Eslöv

15

A2

A4

Lars

Eslöv

15

A2

A4

Lars

Eslöv

15

A2

A5

Hans

Dalby

8

A3

A5

Hans

Dalby

8

A3

A5

Hans

Dalby

8

A3

A5

Hans

Dalby

8

A3

SELECT arbetare.anstnr,arbetare.chefnr, chef.lön Clön, arbetare.lön Alön FROM anst arbetare, anst chef WHERE arbetare.chefnr = chef.anstnr AND chef.lön < arbetare.lön

anstnr

chefnr

Clön

Alön

--------

--------

------

----

A2

A1

10

20

A5

A3

5

8

Vem tjänar mer än sin chef ?
Vem tjänar mer än sin chef ?

SELECT arbetare.anstnr, arbetare.chefnr FROM anst arbetare, anst chef WHERE arbetare.chefnr = chef.anstnr

AND arbetare.anstnr = 'A2‘

-- ‘A2’ chef : A2 roll: arbetare anstnr chefnr ---------- ----------

A2

A1

SELECT arbetare.anstnr, arbetare.chefnr, FROM anst arbetare, anst chef WHERE arbetare.chefnr = chef.anstnr

AND chef.anstnr = 'A2‘

-- ‘A2’ anställda: A2 roll: chef anstnr chefnr ---------- ----------

A3

A4

A2

A2

©Lunds Universitet-Informatik-Erdogan Uçan 33 ©Lunds Universitet-Informatik-Erdogan Uçan 34 Left-right outer join
©Lunds Universitet-Informatik-Erdogan Uçan
33
©Lunds Universitet-Informatik-Erdogan Uçan
34
Left-right outer join
Left-right outer join
SELECT *
FROM student s, kurs k
WHERE s.sadress = k.kadress
SELECT *
FROM student s, kurs k
WHERE s.sadress *= k.kadress
P1
Anna
Lund
111
K1
INF001
Lund
10
Student
Kurs
P2
Anna
Eslöv
222
Null
Null
Null
Null
P3
Eva
Malmö
333
K3
INF052
Malmö
5
P1
Anna
Lund
111
K1
INF001
Lund
10
P2
Anna
Eslöv
222
K2
INF051
Dalby
10
SELECT *
P3
Eva
Malmö
333
K3
INF052
Malmö
5
FROM student s, kurs k
WHERE s.sadress =* k.kadress
P1
Anna
Lund
111
K1
INF001
Lund
10
null
null
null
null
K2
INF051
Dalby
10
P3
Eva
Malmö
333
K3
INF052
Malmö
5
P1
Anna
Lund
111
K1
INF001
Lund
10
P3
Eva
Malmö
333
K3
INF052
Malmö
5
SELECT *
FROM student s, kurs k
WHERE s.sadress (+) = k.kadress
*=, =*
SQL SERVER
(+) ORACLE
©Lunds Universitet-Informatik-Erdogan Uçan
35
©Lunds Universitet-Informatik-Erdogan Uçan
36

Left-right outer join

SELECT s.spnr,s.sadress, k.kkod, k.kadress FROM student s LEFT OUTER JOIN kurs k ON s.sadress = k.kadress

-- eller ; samma resultat SELECT * FROM kurs k RIGHT OUTER JOIN student s ON s.sadress = k.kadress

Spnr sadress kkod

----- ----------

kadress

-----

----------

P1

lund

K1

lund

P2

Eslöv

NULL

NULL

P3

Malmö

K3

Malmö

SELECT s.spnr,s.sadress, k.kkod, k.kadress FROM student2 s RIGHT OUTER JOIN kurs2 k ON s.sadress = k.kadress

-- eller ; samma resultat

SELECT s.spnr,s.sadress, k.kkod, k.kadress FROM kurs2 k LEFT OUTER JOIN student2 s ON s.sadress = k.kadress

spnr sadress

kkod kadress

-----

----------

-----

----------

P1

lund

K1

lund

NULL NULL

K2

Dalby

P3

Malmö

K3

Malmö

SQLSQLSQL 929292

Full outer join

SELECT k.kkod,k.kadress,s.spnr, s.sadress FROM kurs2 k FULL OUTER JOIN student2 s ON s.sadress = k.kadress

kkod kadress

spnr

sadress

----- ----------

-----

----------

K1

lund

P1

lund

NULL NULL

P2

Eslöv

K3

Malmö

P3

Malmö

K2

Dalby

NULL NULL

©Lunds Universitet-Informatik-Erdogan Uçan 37 ©Lunds Universitet-Informatik-Erdogan Uçan 38 Subqueries Subqueries
©Lunds Universitet-Informatik-Erdogan Uçan
37
©Lunds Universitet-Informatik-Erdogan Uçan
38
Subqueries
Subqueries
--Vem har samma namn
-- som person P1 ?
-- Vem har högsta betyg ?
SUBQ som returnerar mer än ett värde:
SELECT *
FROM student
WHERE spnr ='P1'
SELECT MAX(betyg)
FROM laser
MAX(BETYG)
spnr sname
sadress
stel
----------
----- --------
--------
-----
9
P1
eva
dalby
111
SELECT *
FROM student
WHERE sadress =
(SELECT kadress
FROM kurs
WHERE kadress ='lund')
SELECT *
FROM student
WHERE sname =’eva’
SELECT *
FROM laser
WHERE betyg = 9
SELECT *
FROM student
WHERE sname =
(SELECT sname
FROM student
WHERE spnr ='P1')
SELECT *
FROM laser
WHERE betyg =
(SELECT MAX(betyg)
FROM laser)
ORA-01427: single-row subquery returns more than one row
Enbart en tupel om dessa relationsoperatorer används :=, !=, <, <= , >, >=
©Lunds Universitet-Informatik-Erdogan Uçan
39
©Lunds Universitet-Informatik-Erdogan Uçan
40

Subqueries

Laser Student spnr kkod Betyg spnr sname sadress stel P1 K1 6 P1 Anna Lund
Laser
Student
spnr
kkod
Betyg
spnr
sname
sadress
stel
P1
K1
6
P1
Anna
Lund
111
P1
K2
7
P2
Anna
Eslöv
222
P1
K3
8
P3
Eva
Malmö
333
P2
K1
7
P4
Eva
Malmö
444
P2
K2
9
P5
Hans
Eslöv
555
P3
K1
6
P1
--Inner Query
P1
SELECT spnr
P1
Outer
SELECT SELECT spnr spnr
FROM laser
P2
Query
FROM FROM student student
P2
WHERE WHERE spnr spnr IN IN
P3
Inner
(SELECT (SELECT spnr spnr
Query
FROM FROM laser) laser)
Spnr
----
SELECT SELECT spnr spnr
P1
FROM FROM student student
P2
WHERE WHERE spnr spnr IN IN
P3
( ( ’P1’,P1’,’P1’,’P2’, ’P1’,P1’,’P1’,’P2’, ’P2’,’P3’) ’P2’,’P3’)

Vad är frågan ?

Subqueries

SELECT * FROM student WHERE spnr IN (SELECT spnr FROM laser)

spnr

sname

sadress

stel

------

---------

------------

-------

P1

Anna

Lund

111

P2

Anna

Eslöv

222

P3

Eva

Malmö

333

 

IN tolkas som Intersection ; mängd operation

SELECT * FROM student s ,laser l WHERE s.spnr = l.spnr

DISTINCT s.* ger samma resultat som ovan

spnr sname

sadress

stel

spnr

kkod betyg

-----

------- ----------

-----

-----

-----

-------

P1

anna

lund

111

P1

K1

6

P1

anna

lund

111

P1

K2

7

P1

anna

lund

111

P1

K3

8

P2

anna

eslöv

222

P2

K1

7

P2

anna

eslöv

222

P2

K2

9

P3

eva

malmö

333

P3

K1

6

©Lunds Universitet-Informatik-Erdogan Uçan 41 ©Lunds Universitet-Informatik-Erdogan Uçan 42 Correlaterade Queries
©Lunds Universitet-Informatik-Erdogan Uçan
41
©Lunds Universitet-Informatik-Erdogan Uçan
42
Correlaterade Queries
Correlaterade Queries
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = s.spnr)
spnr
snamn
sadress
stel
spnr
kkod
Betyg
P1
Anna
Lund
111
-- Vem fick betyg 6 ?
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = s.spnr)
P1
K1
6
P2
Anna
Eslöv
222
SELECT *
FROM student
WHERE spnr IN
(SELECT spnr
FROM laser
WHERE betyg = 5)
P1
K2
7
P3
Eva
Malmö
333
P1
K3
8
P2
K1
7
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = ’P1’)
betyg
P2
K2
9
------
K1
6
1
6
7
P3
8
SELECT *
FROM student s
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = ’P2’)
betyg
Inner Query är oberoende.
Evauleras först.
Passar resultatet till outer Q uery.
Inner Query är beroende av
outer Query. Behöver värde
från outer Query.
Passar resultatet till outer Query.
------
2
7
WHERE 5 IN (6,7,8)
9
SELECT *
FROM student s
WHERE 5 IN (7,9)
SELECT *
FROM student s
WHERE 5 IN
(SELECT betyg
FROM laser
WHERE spnr = ’P3’)
3
betyg
------
6
SELECT *
FROM student s
WHERE 5 IN (6)
©Lunds Universitet-Informatik-Erdogan Uçan
43
©Lunds Universitet-Informatik-Erdogan Uçan
44
Mängd operationer -- Rader som har anna -- Dum men kul lösning SELECT * FROM
Mängd operationer
-- Rader som har anna
-- Dum men kul lösning
SELECT *
FROM student
WHERE sname IN
(SELECT sname
FROM student
WHERE sname = 'anna')
-- en annan dum lösning
SELECT s1.spnr, s1.sname, s2.spnr
FROM student s1, student s2
WHERE s1.sname = 'anna'
AND s2.spnr = s1.spnr
SELECT *
FROM student
WHERE sname = 'anna'
----studenterstudenter somsom inteinte llääserser nnåågragra kurserkurser ---- DIFFERENCEDIFFERENCE (( studentstudent –– laser)laser)
SELECT *
FROM student
WHERE spnr NOT IN
(SELECT spnr
FROM laser)
-- Fråga ?
SELECT *
FROM kurs
WHERE kkod NOT IN
(SELECT kkod
FROM laser)
 

IN vs Join

 

SELECT * FROM student s WHERE spnr IN (SELECT spnr FROM laser)

   

spnr

sname

sadress

stel

 

-----

----------

----------

--------

 

P1

Anna

Lund

111

P2

Anna

Eslöv

222

P3

Eva

Malmö

333

SELECT *

 

FROM

student s, kurs k, laser l

 

WHERE l.kkod = k.kkod

 

AND

l.spnr = s.spnr

 

Spnr

sname

sadr

stel

kkod

kname

kadress

poang

spnr

kkod

-----

-------

-------

-------

--------

----------

-----------

---------

---------

---------

P1

Anna

Lund

111

K1

INF001

lund

10

P1

K1

P1

Anna

Lund

111

K2

INF051

Dalby

10

P1

K2

P1

Anna

Lund

111

K3

INF052

Malmö

5

P1

K3

P2

Anna

Eslöv

222

K1

INF001

Lund

10

P2

K1

P2

Anna

Eslöv

222

K2

INF051

Dalby

10

P2

K2

P3

Eva

Malmö

333

K1

INF001

Lund

10

P3

K1

 

©Lunds Universitet-Informatik-Erdogan Uçan

 
©Lunds Universitet-Informatik-Erdogan Uçan 45 46 IN vs EXISTS IN vs EXISTS SELECT * FROM student
©Lunds Universitet-Informatik-Erdogan Uçan
45
46
IN vs EXISTS
IN vs EXISTS
SELECT *
FROM student
WHERE spnr NOT IN
( SELECT spnr
FROM laser)
spnr
sname
sadress
stel
spnr
sname
sadress
stel
spnr
kkod
betyg
P1
Anna
Lund
111
P1
Anna
Lund
111
P1
K1
6
P2
Anna
Eslöv
222
P2
Anna
Eslöv
222
P1
K2
7
P3
Eva
Malmö
333
P3
Eva
Malmö
333
P1
K3
8
SELECT *
FROM student s
WHERE NOT EXISTS
( SELECT spnr
FROM laser l
WHERE l.spnr = s.spnr)
P4
Eva
Malmö
444
P4
Eva
Malmö
444
P2
K1
7
P2
K2
9
spnr
kkod
betyg
SELECTSELECT **
3
P3
K1
6
P1
K1
6
FROMFROM studentstudent – P3 från student
WHEREWHERE NOTNOT EXISTSEXISTS
spnr
-----
P1
K2
7
(( SELECTSELECT spnrspnr
P3
SELECT *
1
spnr
P1
K3
8
FROMFROM laserlaser
FROM student -- P1 från student
-----
WHEREWHERE spnrspnr == ‘‘P3P3’’))
---- falsefalse
P2
K1
7
WHERE NOT EXISTS
P1
( SELECT spnr
FROM laser
WHERE spnr = ‘P1’)
P2
K2
9
P1
--svaret
P1
P3
K1
6
SELECTSELECT **
FROMFROM studentstudent – P4 från student
WHEREWHERE NOTNOT EXISTSEXISTS
4
-- false
spnr
SELECT *
2
-----
spnr
spnr sname sadress stel
----- ---------- ---------- ----------
P4 eva malmö 040-222
tom
FROM student -- P2 från student
-----
WHERE NOT EXISTS
P2
( SELECT spnr
FROM laser
WHERE spnr = ‘P2’)
(( SELECTSELECT spnrspnr
FROMFROM laserlaser
WHEREWHERE spnrspnr == ‘‘P4P4’’))
---- truetrue
P2
-- false
©Lunds Universitet-Informatik-Erdogan Uçan
47
©Lunds Universitet-Informatik-Erdogan Uçan
48

IN vs EXISTS vs Join

IN vs EXISTS vs Join

SELECT distinct s.spnr, s.* FROM student s, kurs k, laser l WHERE s.spnr = l.spnr

AND

k.kkod = l.kkod

SELECT * FROM student s WHERE spnr IN (SELECT spnr FROM laser WHERE kkod IN (SELECT kkod FROM kurs) )

spnr FROM laser WHERE kkod IN (SELECT kkod FROM kurs) ) SELECT * FROM student s

SELECT * FROM student s WHERE EXISTS (SELECT spnr FROM laser l WHERE EXISTS (SELECT kkod FROM kurs k WHERE kkod = l.kkod AND s.spnr = l.spnr) )

spnr sname

sadress

stel

 

-----

-----

----------

------

P1

anna

lund

111

P2

anna

eslöv

222

P3

eva

malmö

333

 

©Lunds Universitet-Informatik-Erdogan Uçan

50

spnr

sname

sadress

stel

 

P1

Anna

Lund

111

student

P2

Anna

Eslöv

222

P3

Eva

Malmö

333

spnr

kkod

betyg

 

P1

K1

6

P1

K2

7

P1

K3

8

läser

P2

K1

7

P2

K2

9

P3

K1

6

kkod

knamn

kadress

Poang

K1

INF001

Lund

10

K2

INF051

Dalby

10

K3

INF052

Malmö

5

kurs

©Lunds Universitet-Informatik-Erdogan Uçan 49 EXISTS För starka nerver -överkurs EXISTS För starka nerver
©Lunds Universitet-Informatik-Erdogan Uçan
49
EXISTS För starka nerver -överkurs
EXISTS För starka nerver -överkurs
Studenter som läser alla kurser
(0 row(s) affected)
SELECT *
FROM student s
WHERE NOT EXISTS
spnr sname
COUNT
SELECT s.spnr, s.sname, COUNT(*)
FROM student s, laser l
WHERE s.spnr = l.spnr
GROUP BY s.spnr, s.sname