SQL  

SQL

( .., ..)

  1. HAVING
  2. SQL
  3. EXISTS?
  4. EXISTS
  5. EXISTS
  6. EXISTS
  7. NOT EXISTS
  8. EXISTS
  9. SQL
  10. ANY, ALL SOME
  11. ANY SOME
  12. IN EXISTS ANY

, , 10 . , WHERE HAVING. , , , .

SQL, FROM, . , , .

SQL - .

, , , .

, 3- ( 11.1):

SELECT *
FROM Customers outer
WHERE 10/03/1990 IN (SELECT odate
FROM Orders inner
WHERE outer.cnum = inner.cnum);

=============== SQL Execution Log ============
| SELECT * |
| FROM Customers outer |
| WHERE 10/03/1990 IN |
| (SELECT odate |
| FROM Orders inner |
| WHERE outer.cnum = inner.cnum); |
| ============================================= |
| cnum cname city rating snum |
| ----- -------- ---- ------ ----- |
| 2001 Hoffman London 100 1001 |
| 2003 Liu San Jose 200 1002 |
| 2008 Cisneros San Jose 300 1007 |
| 2007 Pereira Rome 100 1004 |
===========
====================================

11.1. .

. Interbase "INNER" "OUTER" , (JOIN TYPE) , , :

SELECT *
FROM Customers out
WHERE CAST('10/03/1990' AS DATE) IN (SELECT odate
FROM Orders inn
WHERE out.cnum = inn.cnum);

, "" (inner) "" (outer), , 9. ; , . cnum , . , , -. , :

, SQL :

, , SQL . , , ( 11.2):

SELECT *
FROM Customers first, Orders second
WHERE first.cnum = second.cnum AND second.odate = 10/03/1990;

, Cisneros , , . , SELECT DISTINCT SELECT. . IN, , , , . , DISTINCT .

========================= SQL Execution Log ========================
|SELECT * |
|FROM Customers first, Orders second |
|WHERE first.cnum = second.cnum AND second.odate = 10/03/1990; |
|======================================================================|
|cnum cname city rating snum onum amt odate cnum1 snum1|
|---- -------- -------- ------ ---- ---- ------- ---------- ----- -----| |2001 Hoffman London 100 1001 3003 767,19 10/03/1990 2001 1001|
|1002 Liu San Jose 200 1002 3005 5160,45 10/03/1990 2003 1002|
|2007 Pereira Rome 100 1004 3002 1900,10 10/03/1990 2007 1004|
|2008 Cisneros San Jose 300 1007 3001 18,69 10/03/1990 2008 1007|
|2008 Cisneros San Jose 300 1007 3006 1098,16 10/03/1990 2008 1007|
======================================================================


11.2. .

, , . ( 11.3):

SELECT snum, sname
FROM Salespeople main
WHERE 1 < (SELECT COUNT (*)
FROM Customers
WHERE snum = main.snum);

=============== SQL Execution Log ==========
|SELECT snum, sname |
|FROM Salespeople main |
|WHERE 1 < (SELECT COUNT (*) |
| FROM Customers |
| WHERE snum = main.snum); | |=============================================|
|snum sname |
|-
---- ----- |
|1001 Peel |
|1002 Serres |
=============================================

11.3. , .

, FROM . , SQL , , FROM . ( snum) , SQL . , . , - .

, , . , , , , . . , , snum cnum , , . , , ( cnum, , ).

SELECT *
FROM Orders main
WHERE NOT snum = (SELECT snum
FROM Customers
WHERE cnum = main.cnum);

( 19), . , , , .

, , . . , ( 11.4):

SELECT *
FROM Orders outer
WHERE amt > (SELECT AVG (amt)
FROM Orders inner
WHERE inner.cnum = outer.cnum);

=============== SQL Execution Log ==============
| SELECT * |
| FROM Orders outer |
| WHERE amt > |
| (SELECT AVG (amt) |
| FROM Orders inner |
| WHERE inner.cnum = outer.cnum |
| =============================================== |
| onum amt odate cnum snum |
| ----- -------- ---------- ----- ------ |
|
3006 1098.19 10/03/1990 2008 1007 |
| 3010 1309.00 10/06/1990 2004 1002 |
| 3011 9891.88 10/06/1990 2006 1001 |
=================================================

11.4. .

, , , , , . ( 11.5):

SELECT *
FROM Orders outer
WHERE amt >= (SELECT AVG (amt)
FROM Orders inner
WHERE inner.cnum = outer.cnum);

=============== SQL Execution Log ==============
| SELECT * |
| FROM Orders outer |
| WHERE amt > = |
| (SELECT AVG (amt) |
| FROM Orders inner |
| WHERE inner.cnum = outer.cnum); |
| =============================================== |
| onum amt odate cnum snum |
| ----- -------- ---------- ----- ------ |
| 3003 767.19 10/03/1990 2001 1001 |
| 3002 1900.10 10/03/1990 2007 1004 |
| 3005 5160.45 10/03/1990 2003 1002 |
| 3006 1098.19 10/03/1990 2008 1007 |
| 3009 1713.23 10/04/1990 2002 1003 |
| 3010 1309.95 10/06/1990 2004 1002 |
| 3011 9891.88 10/06/1990 2006 1001 |
==============
===================================

11.5. , >= .

, , , , ( , ).

. , , "inner" "outer", Interbase (, ) , , , .

HAVING

HAVING , . HAVING, , HAVING. 6, HAVING , SELECT GROUP BY. , . , HAVING , . , , .

, , , , (SUM) , , 2000.00 (MAX) :

Select odate, SUM (amt)
FROM orders a
GROUP BY odate
HAVING SUM (amt) > (SELECT 2000.00 + MAX (amt)
FROM Orders b
WHERE a.odate = b.odate);

MAX , . , , WHERE. GROUP BY HAVING.

, ( ) . , , , .

, DISTINCT . , , . , , , , , .

, , , . , , , , , , , .

SQL . , , , HAVING. , .

SQL. , IN, IN, . , 12, EXISTS.

SQL

    1. SELECT, , .
    2. , ( ), , . . ? (: , , , ).

(. A .)

12

EXISTS

, , , . . .

EXISTS , , . , () . , , , , NULL . , , , , .

EXISTS?

EXISTS , , , (. 4 ). , , AND, OR, NOT. , , , . , . , , , , , San Jose ( 12.1):

SELECT cnum, cname, city
FROM Customers
WHERE EXISTS (SELECT *
FROM Customers
WHERE city = 'San Jose');

=============== SQL execution Log ============
| SELECT snum, sname, city |
| FROM Customers |
| WHERE EXISTS |
| (SELECT * |
| FROM Customers |
| WHERE city = 'San Jose'); |
| ============================================= |
| cnum cname city |
| ----- -------- ---- |
| 2001 Hoffman London |
| 2002 Giovanni Rome |
| 2003 Liu San Jose |
| 2004 Grass Berlin |
| 2006 Clemens London |
| 2008
Cisneros San Jose |
| 2007 Pereira Rome |
===============================================

12.1
. EXISTS.

San Jose. EXISTS , , EXISTS , . ( ) , , , . EXISTS, , , .

EXISTS

, EXISTS , , , , (SELECT *). , ( 10) .

, EXISTS , , , , .

EXISTS

EXISTS , , , . EXISTS , , . , , , EXISTS . , , ( 12.2):

SELECT DISTINCT snum
FROM Customers outer
WHERE EXISTS (SELECT *
FROM Customers inner
WHERE inner.snum = outer.snum AND
inner.cnum <> outer.cnum);

============= SQL Execution Log ==============
| SELECT DISTINCT cnum |
| FROM Customers outer |
| WHERE EXISTS |
| (SELECT * |
| FROM Customers inner |
| WHERE inner.snum = outer.snum |
| AND inner.cnum <> outer.cnum); |
|===============================================|
| cnum |
| ----- |
| 1001 |
| 1002 |
===============================================


12.2
. EXISTS .

- ( , ), , snum ( ), cnum ( ). , , , (.. - ). EXISTS , (snum) , , . DISTINCT , , .

EXISTS

, . , ( 12.3):

SELECT DISTINCT first.snum, sname, first.city
FROM Salespeople first, Customers second
WHERE EXISTS (SELECT *
FROM Customers third
WHERE second.snum = third.snum AND
second.cnum <> third.cnum) AND
first.snum = second.snum;

============= SQL Execution Log ==============
| SELECT DISTINCT first.snum, sname, first.city |
| FROM Salespeople first, Customers second |
| WHERE EXISTS |
| (SELECT * |
| FROM Customers third |
| WHERE second.snum = third.snum |
| AND second.cnum <> third.cnum) |
| AND first.snum = second.snum; |
|===============================================|
| cnum cname city |
| ----- -------- ---- |
| 1001 Peel London |
| 1002 Serres San Jose |
=====
==========================================

12.3.
EXISTS .

, , , . , , . (AND first.snum = second.snum) , EXISTS. , snum, . - AND, . , , , . , .

NOT EXISTS

, EXISTS . , , EXISTS NOT. , , , . ( 12.4.)

SELECT DISTINCT snum
FROM Customers outer
WHERE NOT EXISTS (SELECT *
FROM Customers inner
WHERE inner.snum = outer.snum AND
inner.cnum <> outer.cnum);

============== SQL Execution Log =============
| SELECT DISTINCT snum |
| FROM Salespeople outer |
| WHERE NOT EXISTS |
| (SELECT * |
| FROM Customers inner |
| WHERE inner.snum = outer.snum |
| AND inner.cnum <> outer.cnum); |
|===============================================|
| cnum |
| ----- |
| 1003 |
| 1004 |
| 1007
|
===============================================

12.4
. NOT EXISTS.

EXISTS

, EXISTS . . , EXISTS , , ; , EXISTS .

EXISTS , , , .

, EXISTS . ( ). , , , . .

, , , . , EXISTS (SELECT COUNT (DISTINCT sname) FROM Salespeople) EXISTS (SELECT sname FROM Salespeople) .

.

, . , , SQL, .

, , . , , , SQL. :

SELECT *
FROM Salespeople first
WHERE EXISTS (SELECT *
FROM Customers second
WHERE first.snum = second.snum AND
1 < (SELECT COUNT (*)
FROM Orders
WHERE Orders.cnum = second.cnum));

12.5.

============== SQL Execution Log =============
| FROM Salespeople first |
| WHERE EXISTS |
| (SELECT * |
| FROM Customers second |
| WHERE first.snum = second.snum |
| AND 1 < |
| (SELECT CONT (*) |
| FROM Orders |
| WHERE Orders.cnum = second.cnum)); |
|===============================================|
| cnum cname city comm |
| ----- -------- ---- -------- |
| 1001 Peel London 0.17 |
| 1002 Serres San Jose 0.13 |
| 1007 Rifkin Barselona
0.15 |
===============================================

12.5
. EXISTS .

:

- ( ) . - ( ). (.. first.snum <> second.snum), . , , , , , . ( ). 1, , . EXISTS , , , , .

, . , . , , . , , SQL, .

, , , . , , , , ( , SQL). , , , , . , , ( , 20).

EXISTS, , SQL. . , EXISTS. , .

, , ANY, ALL, SOME. 13, , , .

SQL

    1. , EXISTS , 300.
    2. , ?
    3. , EXISTS , .
    4. , , , , ( ) (: ).

(. A .)

13

ANY, ALL SOME

, EXISTS, . (, , ANY SOME .) , , SQL. , , , , .

ANY, ALL, SOME EXISTS, ; EXISTS , . , IN, ; , , , . , IN, .

ANY SOME

SOME ANY ANY, SOME . , , . ; , , .

, ( 13.1):

SELECT *
FROM Salespeople
WHERE city = ANY (SELECT city
FROM Customers);

ANY , ( city ), , (ANY) .

=============== SQL Execution Log ============
| SELECT * |
| FROM Salespeople |
| WHERE city = ANY |
| (SELECT city |
| FROM Customers); |
| ============================================= |
| cnum cname city comm |
| ----- -------- ---- -------- |
| 1001 Peel London 0.12 |
| 1002 Serres San Jose 0.13 |
| 1004 Motika London 0.11 |
=====================
==========================

13.1.
ANY.

, , , . EXISTS, , , .

IN EXISTS ANY

IN, , :

SELECT *
from Salespeople
WHERE city IN (SELECT city
FROM Customers);

, 13.2.

=============== SQL Execution Log ============
| SELECT * |
| FROM Salespeople |
| WHERE city IN (SELECT city |
| FROM Customers); |
| ============================================= |
| cnum cname city comm |
| ----- -------- ---- -------- |
| 1001 Peel London 0.12 |
| 1002 Serres San Jose 0.13 |
| 1004 Motika London 0.11 |
===
============================================

13.2.
IN ANY.

ANY , (=), , , , IN. , , ( 13.3).

SELECT *
FROM Salespeople
WHERE sname < ANY (SELECT cname
FROM Customers);

=============== SQL execution Log ============
| SELECT * |
| FROM Salespeople |
| WHERE sname < ANY |
| (SELECT cname |
| FROM Customers); |
| ============================================= |
| cnum cname city comm |
| ----- -------- ---- -------- |
| 1001 Peel London 0.12 |
| 1004 Motika London 0.11 |
| 1003 Axelrod New York 0.10 |
===============================================

13.3.
ANY "" (<).

, Serres Rifkin, , . , EXISTS ( 13.4):

SELECT *
FROM Salespeople outer
WHERE EXISTS (SELECT *
FROM Customers inner
WHERE outer.sname < inner.cname);

=============== SQL Execution Log ============
| SELECT * |
| FROM Salespeople outer |
| WHERE EXISTS |
| (SELECT * |
| FROM Customers inner |
| WHERE outer.sname < inner.cname); |
| ============================================= |
| cnum cname city comm |
| ----- -------- ---- -------- |
| 1001 Peel London 0.12 |
| 1004 Motika London 0.11 |
| 1003 Axelrod New York 0.10 |
===============================================

13.4. EXISTS ANY.

, ANY (, , ALL), EXISTS, . , EXISTS ANY ALL - , (NULL) ( ). , , ANY ALL, EXISTS ( IS NULL).

, , ANY ALL , EXISTS, . , , ANY ALL , , , , EXISTS.

ANY ALL , . EXISTS, , , , . SQL , .

EXISTS ANY ALL , ANY ALL , - , . , , .

ANY

, ANY . , , , , , ( 13.5):

SELECT *
FROM Customers
WHERE rating > ANY (SELECT rating
FROM Customers
WHERE city = 'Rome');

, " ( city = 'Rome')", , , , city = 'Rome'. , ANY, SQL. ANY , , .

=============== SQL Execution Log ============
| SELECT * |
| FROM Customers |
| WHERE rating > ANY |
| (SELECT rating |
| FROM Customers |
| WHERE city = 'Rome'); |
| ============================================= |
| cnum cname city rating snum |
| ----- -------- ---- ------ ------ |
| 2002 Giovanni Rome 200 1003 |
| 2003 Liu San Jose 200 1002 |
| 2004 Grass Berlin 300 1002 |
| 2008 Cisneros San Jose 300 1007 |
==============================
=================

13.5. "
" (>) ANY.

ANY , , 300 Giovanni, 200. , ANY Periera 100. 200 , , (Giovanni), (, , , ). , , , , .

, , , , , 6- :

SELECT *
FROM Orders
WHERE amt > ANY (SELECT amt
FROM Orders
WHERE odate = 10/06/1990);

13.6.

=============== SQL Execution Log ==============
| SELECT * |
| FROM Orders |
| WHERE amt > ANY |
| (SELECT amt |
| FROM Orders |
| WHERE odate = 10/06/1990); |
| =============================================== |
| onum amt odate cnum snum |
| ----- -------- ---------- ----- ------ |
| 3002 1900.10 10/03/1990 2007 1004 |
| 3005 5160.45 10/03/1990 2003 1002 |
| 3009 1713.23 10/04/1990 2002 1003 |
| 3008 4723.00 10/05/1990 2006 1001 |
| 3011 989
1.88 10/06/1990 2006 1001 |
=================================================

13.6
. (ANY) 6- .

(9891.88) 6- , , 6- , = 1309.95. ">=" ">", , .

, ANY SQL , . , San Jose. ( 13.7):

SELECT *
FROM Orders
WHERE amt < ANY (SELECT amt
FROM Orders A, Customers b
WHERE a.cnum = b.cnum
AND b.city = 'San Jose');

San Jose, ; . , < ANY , > ANY .

=============== SQL Execution Log ==============
| WHERE amt > ANY |
| (SELECT amt |
| FROM Orders a, Customers b |
| WHERE a.cnum = b.cnum |
| AND b.city = 'San Jose'); |
| =============================================== |
| onum amt odate cnum snum |
| ----- -------- ---------- ----- ------ |
| 3001 18.69 10/03/1990 2008 1007 |
| 3003 767.10 10/03/1990 2001 1001 |
| 3002 1900.10 10/03/1990 2007 1004 |
| 3006 1098.10 10/03/1990 2008 1007 |
| 3009 1713.23 10/04/1990 2002 1003 |
| 3007 75.10 10/04/1990 2004 1002 |
| 3008 4723.00 10/05/1990 2006 1001 |
| 3010 1309.88 10/06/1990 2004 1002 |
================================
=================

13.7.
ANY .

, ( 13.8):

SELECT *
FROM Orders
WHERE amt < (SELECT MAX (amt)
FROM Orders a, Customers b
WHERE a.cnum = b.cnum AND b.city = 'San Jose');

=============== SQL Execution Log ==============
| WHERE amt < |
| (SELECT MAX (amt) |
| FROM Orders a, Customers b |
| WHERE a.cnum = b.cnum |
| AND b.city = 'San Jose'); |
| =============================================== |
| onum amt odate cnum snum |
| ----- -------- ---------- ----- ------ |
| 3002 1900.10 10/03/1990 2007 1004 |
| 3005 5160.45 10/03/1990 2003 1002 |
| 3009 1713.23 10/04/1990 2002 1003 |
| 3008 4723.00 10/05/1990 2006 1001 |
| 3011 9891.88 10/06/1990
2006 1001 |
=================================================

13.8.
ANY.

ALL

ALL, , . , , , , , , , 13.9:

SELECT *
FROM Customers
WHERE rating > ALL (SELECT rating
FROM Customers
WHERE city = 'Rome');

=============== SQL Execution Log ============
| SELECT * |
| FROM Customers |
| WHERE rating > ALL |
| (SELECT rating |
| FROM Customers |
| WHERE city = 'Rome'); |
| ============================================= |
| cnum cname city rating snum |
| ----- -------- ---- ------ ------ |
| 2004 Grass Berlin 300 1002 |
| 2008 Cisneros San Jose 300 1007 |
============================
===================

13.9
. ALL.

. , . Giovanni (200). , 200.

ANY, EXISTS ( 13.10):

SELECT *
FROM Customers outer
WHERE NOT EXISTS (SELECT *
FROM Customers inner
WHERE outer.rating <= inner.rating
AND inner.city = 'Rome');

=============== SQL Execution Log ============
| SELECT * |
| FROM Customers outer |
| WHERE NOT EXISTS |
| (SELECT * |
| FROM Customers inner |
| WHERE outer rating <= inner.rating |
| AND inner.city = 'Rome'); |
| ============================================= |
| cnum cname city rating snum |
| ----- -------- ---- ------ ------ |
| 2004 Grass Berlin 300 1002 |
| 2008 Cisne
ros San Jose 300 1007 |
===============================================

13.10.
EXISTS ALL.

ALL , , " " , , . :

SELECT *
FROM Customers
WHERE rating = ALL (SELECT rating
FROM Customers
WHERE city = 'San Jose');

, . San Jose . , :

SELECT *
FROM Customers
WHERE rating = (SELECT DISTINCT rating
FROM Customers
WHERE city = 'San Jose');

, , , ALL . , , , . , , .

, ALL , "<>". , SQL , , , a .

, , , . SQL, <> ALL " " . , , . , ( 13.11):

SELECT *
FROM Customers
WHERE rating <> ALL (SELECT rating
FROM Customers
WHERE city = 'San Jose');

=============== SQL Execution Log ============
| SELECT * |
| FROM Customers |
| WHERE rating <> ALL |
| (SELECT rating |
| FROM Customers |
| WHERE city = 'San Jose'); |
| ============================================= |
| cnum cname city rating snum |
| ----- -------- ---- ------ ------ |
| 2001 Hoffman London 100 1001 |
| 2006 Clemens London 100 1001 |
| 2007 Pereira Rome 10
0 1004 |
===============================================

13.11.
ALL <>.

San Jose. : 200 ( Liu) 300 ( Cisneros). , , , , 100. , NOT IN:

SELECT*
FROM Customers
WHERE rating NOT IN (SELECT rating
FROM Customers
WHERE city = 'San Jose');

ANY:

SELECT *
FROM Customers
WHERE NOT rating = ANY (SELECT rating
FROM Customers
WHERE city = 'San Jose');

.

ANY ALL

SQL , ( ) (ANY) , ( ) . , , (ALL) , , , .

ANY, ALL, EXIST

, EXISTS , , , NULL. ANY ALL , , , . , .

ALL ANY , . , , , ALL , ANY . ,

SELECT *
FROM Customers
WHERE rating > ANY (SELECT rating
FROM Customers
WHERE city = 'Boston');

,

SELECT *
FROM Customers
WHERE rating > ALL (SELECT rating
FROM Customers
WHERE city = 'Boston');

. Boston, , .

ANY ALL EXISTS (NULL)

NULL . SQL , (NULL), ( 5). , , , , , ALL ANY EXISTS. :

SELECT *
FROM Customers
WHERE rating > ANY (SELECT rating
FROM Customers
WHERE city = 'Rome');

:

SELECT *
FROM Customers outer
WHERE EXISTS (SELECT *
FROM Customers inner
WHERE outer.rating > inner.rating
AND inner.city = 'Rome');

, . , (NULL) rating :

CNUM

CNAME

CITY

RATING

SNUM

2003

Liu

SanJose

NULL

1002

ANY, Liu , NULL , Liu . , NOT EXISTS, , NULL , . , , EXISTS . , , NOT EXISTS . , Liu . , , EXISTS , . ANY. , NULL , . , , .

COUNT EXISTS

, ANY ALL EXISTS, . , EXISTS NOT EXISTS COUNT (*) SELECT . , EXISTS; NOT EXISTS. ( 13.12).

SELECT *
FROM Customers outer
WHERE NOT EXISTS (SELECT *
FROM customers inner
WHERE outer.rating <= inner.rating
AND inner.city = 'Rome');

=============== SQL Execution Log ============
| SELECT * |
| FROM Customers outer |
| WHERE NOT EXISTS |
| (SELECT * |
| FROM Customers inner |
| WHERE outer.rating <= inner.rating |
| AND inner.city = 'Rome'); |
| ============================================= |
| cnum cname city rating snum |
| ----- -------- -
--- ------ ------ |
| 2004 Grass Berlin 300 1002 |
| 2008 Cisneros San Jose 300 1007 |
===============================================

13.12.
EXISTS .

SELECT *
FROM Customers outer
WHERE 1 > (SELECT COUNT (*)
FROM Customers inner
WHERE outer.rating <= inner.rating
AND inner.city = 'Rome');

13.13. , SQL. , . , , . , , .

=============== SQL Execution Log ============
| SELECT * |
| FROM Customers outer |
| WHERE 1 > |
| (SELECT COUNT (*) |
| FROM Customers inner |
| WHERE outer.rating <= inner.rating |
| AND inner.city = 'Rome'); |
| ============================================= |
| cnum cname city rating snum |
| ----- -------- ---- ------ ------ |
| 2004 Grass Berlin 300 1002 |
| 2008 Cisneros San Jose 300 10
07 |
===============================================

13.13
. COUNT EXISTS.

, . , , . , . , , . , , (NULL) .

, , , , , , SQL, .

, , , , UNION.

SQL

    1. , , (ANY) Serres.
    2. ?
    3. , ANY ALL, , , .
    4. , ( ) .
    5. MAX.

(. A .)

14

UNION

, . .. . UNION SQL. UNION ( ) . , .

, UNION. UNION SQL . , , , , :

SELECT snum, sname
FROM Salespeople
WHERE city = 'London'
UNION
SELECT cnum, cname
FROM Customers
WHERE city = 'London';

, 14.1.

, , , , . , , . , ( 7, ).

, , . SQL, .

=============== SQL Execution Log ============
| |
| SELECT snum, sname |
| FROM Salespeople |
| WHERE city = 'London' |
| UNION |
| SELECT cnum, cname |
| FROM Customers |
| WHERE city = 'London'; |
| ============================================= |
| |
| ----- -------- |
| 1001 Peel |
| 1004 Motika |
| 2001 Hoffman |
| 2006 Clemens |
| |
========================================
=======

14.1. .

?

( ) , . , , , , , , . . ANSI , , ANSI , ( B ANSI). , .

, SQL , ANSI. , ANSI, DATA BINARY, .

. , UNION. , ( ANSI ), , . .

(NULL) , . (NULL) NOT NULL, 18. , UNION , SELECT . ( .)

UNION

UNION . SQL, DISTINCT, . , , 14.2,

SELECT snum, city
FROM Customers;

(snum=1001, city='London'), , SQL . , UNION , . 14.3 .

SELECT snum, city
FROM Customers

UNION

SELECT snum, city
FROM Salespeople;

=============== SQL Execution Log ============
| SELECT snum, city |
| FROM Customers; |
| ============================================= |
| snum city |
| ----- -------- |
| 1001 London |
| 1003 Rome |
| 1002 San Jose |
| 1002 Berlin |
| 1001 London
|
| 1004 Rome |
| 1007 San Jose |
===============================================

14.2. .

=============== SQL Execution Log ============
| FROM Customers |
| UNION |
| SELECT snum, city |
| FROM Salespeople; |
| ============================================= |
| |
| ----- -------- |
| 1001 London |
| 1002 Berlin |
| 1007 San Jose |
| 1007 New York |
| 1003 Rome |
| 1001 London |
| 1003 Rome |
| 1002 Barcelona |
| 1007 San Jose |
==============================
=================

14.3.
UNION .

SQL, UNION ALL UNION, :

SELECT snum, city
FROM Customers

UNION ALL

SELECT snum, city
FROM Salespeople;

UNION

, SELECT, UNION. ANSI, . , , , . , , , , .

, , . , , .

SELECT a.snum, sname, onum, 'Highest on', odate
FROM Salespeople a, Orders b
WHERE a.snum = b.snum AND b.amt = (SELECT MAX (amt)
FROM Orders c
WHERE c.odate = b.odate)

UNION

SELECT a.snum, sname, onum, 'Lowest on ', odate
FROM Salespeople a, Orders b
WHERE a.snum = b.snum AND b.amt = (SELECT MIN (amt)
FROM Orders c
WHERE c.odate = b.odate);

14.4.

'Lowest on', 'Highest on'. , Peel (, ) 5 . , .

=============== SQL Execution Log ============
| AND b.amt = |
| (SELECT min (amt) |
| FROM Orders c |
| WHERE c.odate = b.odate); |
| ============================================= |
| |
| ----- ------- ------ ---------- ----------- |
| 1001 Peel 3008 Highest on 10/05/1990 |
| 1001 Peel 3008 Lowest on 10/05/1990 |
| 1001 Peel 3011 Highest on 10/06/1990 |
| 1002 Serres 3005 Highest on 10/03/1990 |
| 1002 Serr
es 3007 Lowest on 10/04/1990 |
| 1002 Serres 3010 Lowest on 10/06/1990 |
| 1003 Axelrod 3009 Highest on 10/04/1990 |
| 1007 Rifkin 3001 Lowest on 10/03/1990 |
===============================================

14.4. .

UNION ORDER BY

, . , . , , . , . ORDER BY , . . , Peel , 14.5.

SELECT a.snum, sname, onum, 'Highest on', odate
FROM Salespeople a, Orders b
WHERE a.snum = b.snum AND b.amt = (SELECT MAX (amt)
FROM Orders c
WHERE c.odate = b.odate)

UNION

SELECT a.snum, sname, onum, 'Lowest on', odate
FROM Salespeople a, Orders b
WHERE a.snum = b.snum AND b.amt = (SELECT MIN (amt)
FROM Orders c
WHERE c.odate = b.odate)
ORDER BY 3;

=============== SQL Execution Log ============
| (SELECT min (amt) |
| FROM Orders c |
| WHERE c.odate = b.odate) |
| ORDER BY 3; |
| ============================================= |
| |
| ----- ------- ------ ---------- ----------- |
| 1007 Rifkin 3001 Lowest on 10/03/1990 |
| 1002 Serres 3005 Highest on 10/03/1990 |
| 1002 Serres 3007 Lowest on 10/04/1990 |
| 1001 Peel 3008 Highest on 10/05/1990 |
| 1001 Peel 3008 Lowest on 10/05/1990 |
| 1003 Axelrod 3009 Highest on 10/04/1990 |
| 1002 Serres 3010 Lowest on 10/06/1990 |
| 1001
Peel 3011 Highest on 10/06/1990 |
===============================================

14.5. ORDER BY.

ORDER BY , . , ASC DESC , . , 3 ORDER BY , SELECT . , , , , . ( 7, ).

, , , . , , . .

, . , , , . , , , (NULL) snum. , sname .

, , , . , . , . , , , , , , , . , 14.6, :

SELECT Salespeople.snum, sname, cname, comm
FROM Salespeople, Customers
WHERE Salespeople.city = Customers.city

UNION

SELECT snum, sname, 'NO MATCH ', comm
FROM Salespeople
WHERE NOT city = ANY (SELECT city
FROM Customers)
ORDER BY 2 DESC;

=============== SQL Execution Log ============
| FROM Salespeople |
| WHERE NOT city = ANY (SELECT city |
| FROM Customers) |
| ORDER BY 2 DESC; |
| ============================================= |
| |
| ----- ------- --------- ------------ |
| 1002 Serres Cisneros 0.1300 |
| 1002 Serres Liu 0.1300 |
| 1007 Rifkin NO MATCH 0.1500 |
| 1001 Peel Clemens 0.1200 |
| 1001 Peel Hoffman 0.1200 |
| 1004 Motika Clemens 0.1100 |
| 1004 Motika Hoffman 0.1100 |
| 1003 Axelrod NO MATCH 0.1000 |
===============================================

14.6. .

'NO MATCH' , cname ( SQL). , . , . , , . UNION , . , , , :

SELECT a.snum, sname, a.city, 'MATCHED '
FROM Salespeople a, Customers b
WHERE a.city = b.city

UNION

SELECT snum, sname, city, 'NO MATCH'
FROM Salespeople
WHERE NOT city = ANY (SELECT city
FROM Customers)
ORDER BY 2 DESC;

14.7 .

=============== SQL Execution Log ============
| WHERE a.city = b.city |
| union |
| SELECT snum,sname,city, 'NO MATCH' |
| FROM Salespeople |
| WHERE NOT city = (SELECT city |
| FROM Customers) |
| ORDER BY 2 DESC; |
| ============================================= |
| |
| ----- ------- ------------ --------- |
| 1002 Serres San Jose MATCHED |
| 1007 Rifkin Barselona NO MATCH |
| 1001 Peel London MATCHED |
| 1004 Motika London
MATCHED |
| 1003 Axelrod New York NO MATCH |
===============================================

14.7. .

, . , . , ( 14.8):

SELECT snum, city, 'SALESPERSON MATCH '
FROM Salespeople
WHERE NOT city = ANY (SELECT city
FROM Customers)

UNION

SELECT snum, city, 'SALESPERSON NO MATCH'
FROM Salespeople
WHERE NOT city = ANY (SELECT city
FROM Customers)

UNION

SELECT cnum, city, 'CUSTOMER MATCHED '
FROM Customers
WHERE city = ANY (SELECT city
FROM Salespeople)

UNION

SELECT cnum, city, 'CUSTOMER NO MATCH '
FROM Customers
WHERE NOT city = ANY (SELECT city
FROM Salespeople)

ORDER BY 2 DESC;

================= SQL Execution Log =============
| FROM Salespeople) |
| ORDER BY 2 DESC; |
| ================================================ |
| |
| ---- -------- ------------------------ |
| 2003 San Jose CUSTOMER MATCHED |
| 2008 San Jose CUSTOMER MATCHED |
| 2002 Rome CUSTOMER NO MATCH |
| 2007 Rome CUSTOMER NO MATCH |
| 1003 New York SALESPERSON MATCHED |
| 1003 New York SALESPERSON NO MATCH |
| 2001 London CUSTOMER MATCHED |
| 2006 London CUSTOMER MATCHED |
| 2004 Berlin CUSTOMER NO MATCH |
| 1007 Barcelona SALESPERSON MATCHED |
| 1007 Barcelona SALESPERSON NO MATCH |
=======================================
===========

14.8. .

, , ANY, .

, , , . , , . , , , . ,

query X UNION query Y UNION query Z;

,

(query X UNION query Y)UNION query Z;

query X UNION (query Y UNION query Z);

, UNION UNION ALL , , .

(query X UNION ALL query Y)UNION query Z;

query X UNION ALL(query Y UNION query Z);

.

, UNION, . (, ), , , . , , , , , .

. SQL. , . , , , .

SQL

    1. , , , . , rating=200 , , , " ", " ".
    2. , , . .
    3. . snum San Jose; , cnum San Jose; onum 3 . , . : . .

(. A .)

,

, , . , , , , .

, . , , .

.

DML

, DML ( ):

INSERT (),
UPDATE (),
DELETE ().

, SQL, .

SQL INSERT. , INSERT :

INSERT INTO <table name>
VALUES (<value>, <value> . . .);

, , , :

INSERT INTO Salespeople
VALUES (1001, 'Peel', 'London', .12);

DML , , .

( Salespeople ), CREATE TABLE (. 17), , , . ANSI, , , 3 , 2 + 1 . , , , , 1, 2, .

(NULL)

(NULL), . , city Peel. =NULL , :

INSERT INTO Salespeople
VALUES (1001, 'Peel', NULL, .12);

NULL , , .

(INSERT)

, . . , , , : city, cname, cnum, , :

INSERT INTO Customers (city, cnam, cnum)
VALUES ('London', 'Honman', 2001);

, rating snum . , . NULL , . NULL , , INSERT, ( 18 NULL " ").

INSERT, , . , VALUES ( ) :

INSERT INTO Londonstaff
SELECT *
FROM Salespeople
WHERE city = 'London';

, , city = "London" Londonstaff. , Londonstaff :

* CREATE TABLE.

* , ; , , , ( ).

, , , , .

Londonstaff , (Salespeople). , Londonstaff ( (VIEW), 20).

INSERT , , , , , .

, , Daytotals, . , Daytotals .

, , , , INSERT

INSERT INTO Daytotals (date, total)
SELECT odate, SUM (amt)
FROM Orders
GROUP BY odate;

, , Daytotals . , , , - .

DELETE. , , . , :

DELETE FROM Salespeople;

, , DROP TABLE ( 17).

, . , , . , Axelrod ,

DELETE FROM Salespeople
WHERE snum = 1003;

snum sname , , . .

, DELETE , , :

DELETE FROM Salespeople
WHERE city = 'London';

, , , . UPDATE.

UPDATE, , SET, , . , 200,

UPDATE Customers
SET rating = 200;

, , UPDATE, DELETE, . , , , Peel ( snum=1001):

UPDATE Customers
SET rating = 200
WHERE snum = 1001;

UPDATE

UPDATE. SET , . , . , Motika , :

UPDATE Salespeople
SET sname = 'Gibson', city = 'Boston', comm = .10
WHERE snum = 1004;

Gibson, Motika , , Motika snum.

, , , , SET. , "SET Salespeople.sname = Gibson" UPDATE, "SET sname = Gibson".

SET UPDATE, , , . VALUES INSERT, ; . , . :

UPDATE Salespeople
SET comm = comm * 2;

, SET, , - UPDATE. , , , , :

UPDATE Salespeople
SET comm = comm * 2
WHERE city = 'London';

(NULL)

SET . NULL , , - (, , IS NULL). , NULL, :

UPDATE customers
SET rating = NULL
WHERE city = 'London';

.

:

INSERT ;
DELETE ;
UPDATE .

UPDATE DELETE, , . , INSERT, INSERT. , INSERT, . .

, , . , NULL. , , UPDATE , INSERT .

, , . , , , DML, 16.

SQL

  1. , , , :
  2. city San Jose,
    name Bianco,
    comm NULL,
    cnum 1100.

  3. , Clemens .
  4. , 100.
  5. Serres . Motika.

(. A .)

, , .

, . , SELECT, , . SELECT , , , UPDATE DELETE. , INSERT, , .

, , , FROM , . . , , , , , .

, , .

INSERT

INSERT . , . , INSERT , .. HAVING.

, SJpeople, . , , , , San Jose:

INSERT INTO SJpeople
SELECT *
FROM Salespeople
WHERE city = 'San Jose';

, SJpeople , San Jose, , :

INSERT INTO Sjpeople
SELECT *
FROM Salespeople
WHERE snum = ANY (SELECT snum
FROM Customers
WHERE city = 'San Jose');

, INSERT. San Jose snum. Salespeople, snum . , Rifkin Serres, San Jose Liu Cisneros, SJpeople.

. Serres San Jose, , , . , San Jose. SJpeople, , ( ). (c. 18 ).

, - , , , ( EXISTS, IN, <> ALL, ) .

, , SJpeople FROM , , , , () .

INSERT, , , . , , INSERT, . " " , INSERT .

,

, INSERT, , , FROM SELECT. , , INSERT, ; , , . , Samecity, .

, :

INSERT INTO (Samecity
SELECT *
FROM (Salespeople outer
WHERE city IN (SELECT city
FROM Customers inner
WHERE inner.snum = outer.snum);

Samecity, INSERT.

, , , . Bonus, snum , odate amt. , , :

INSERT INTO Bonus
SELECT snum, odate, amt
FROM Orders a
WHERE amt = (SELECT MAX (amt)
FROM Orders b
WHERE a.odate = b.odate);

, , , Bonus, . .

, , , . , , , Bonus.

DELETE

DELETE. , , , , , , .

, , , , :

DELETE
FROM Customers
WHERE snum = ANY (SELECT snum
FROM Salespeople
WHERE city = 'London');

Hoffman Clemens ( Peel), Periera ( Motika).

, , , Peel Motika.

. , , , , , . , , , .

, , city , , .

, , . SQL , ( 19), .

, , FROM , - , , . , . , INSERT, - , , .

DELETE FROM Salespeople
WHERE EXISTS (SELECT *
FROM Customers
WHERE rating = 100 AND
Salespeople.snum = Customers.snum);

, AND . , , . , , 100 .

, :

DELETE FROM Salespeople
WHERE 100 IN (SELECT rating
FROM Customers
WHERE Salespeople.snum = Customers.snum);

, rating = 100.

, , ( DELETE) . , , :

DELETE FROM Salespeople
WHERE (snum IN (SELECT snum
FROM Orders a
WHERE amt = (SELECT MIN (amt)
FROM Orders b
WHERE a.odate = b.odate));

DELETE . . , , , , . snum , , DELETE, snum . snum , , snum . , .

snum = 1007, , 3 ; snum = 1002, 4 ; snum = 1001, 5 ( , Peel, 5 , ).

Peel, , :

DELETE FROM Salespeople
WHERE snum IN (SELECT snum
FROM Orders a
WHERE amt = (SELECT MIN (amt)
FROM Orders b
WHERE a.odate = b.odate)
AND 1 < (SELECT COUNT onum
FROM Orders b
WHERE a.odate = b.odate));

, , COUNT = 1 . , snum .

UPDATE

UPDATE , DELETE . DELETE , . , , , :

UPDATE Salespeople
SET comm = comm + .01
WHERE 2 <= (SELECT COUNT (cnum)
FROM Customers
WHERE Customers.snum = Salespeople.snum);

Peel Serres, , .

DELETE. , , :

UPDATE salespeople
SET comm = comm - .01
WHERE snum IN (SELECT snum
FROM Orders a
WHERE amt = (SELECT MIN (amt)
FROM Orders b
WHERE a.odate = b.odate));

DML

, (UPDATE), .

, , . , ( 1.), , , ( 2.), :

1.

SELECT AVG (rating)
FROM Customers;

= 200.

2.

DELETE
FROM Customers
WHERE rating < 200;

, . , , , , , .

: INSERT, . VALUES ( ), ( ). , , , , FROM, ( ). .

, , , FROM , FROM ( ).

DELETE UPDATE , , , . , , , . , , , . , , , FROM.

, SQL, . , . , . , , .

, , , ( ), ( ), .

SQL

    1. , , Multicust, , . , ( ) .
    2. , , .
    3. , , $3,000.

(. A .)

    SQL  

, - - :
- - ;
- (obj) (dll).




 10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _.
10.11.2021 - 12:36: - Conscience -> . ? - _.
10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _.
10.11.2021 - 12:35: - Ecology -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> - _.
10.11.2021 - 12:34: , - War, Politics and Science -> . - _.
10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _.
10.11.2021 - 09:18: - New Technologies -> , 5G- - _.
10.11.2021 - 09:18: - Ecology -> - _.
10.11.2021 - 09:16: - Ecology -> - _.
10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _.
10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _.
Bourabai Research -  XXI Bourabai Research Institution