SQL  

SQL

( .., ..)

  1. ?
  2. ?
  3. COUNT
  4. DISTINCT
  5. COUNT
  6. ,
  7. GROUP BY
  8. HAVING
  9. SQL
  10. NULL
  11. SQL

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

?

. . . :

COUNT NULL , .

SUM .

AVG .

MAX .

MIN .

?

SELECT , , . SUM AVG.

COUNT, MAX, MIN, . , MAX MIN ASCII, , MIN , MAX ( 4).

SUM , , 6.1:

SELECT SUM (amt)
FROM Orders;

=============== SQL Execution Log ============
| SELECT SUM (amt) |
| FROM Orders; |
| ==============================================|
| |
| ------- |
| 26658.4
|
===============================================

6.1
. .

, , , , , . - , , GROUP BY ( ). ( 6.2):

SELECT AVG (amt)
FROM Orders;

=============== SQL Execution Log ============
| SELECT AVG (amt) |
| FROM Orders; |
| ==============================================|
| |
| ---
---- |
| 2665.84 |
===============================================

6.2
. .

COUNT

COUNT . , . , DISTINCT, . , , ( 6.3):

SELECT COUNT (DISTINCT snum)
FROM Orders;

DISTINCT

, DISTINCT, , , SELECT, . DISTINCT COUNT , ANSI, .

=============== SQL Execution Log ============
| SELECT COUNT (DISTINCT snum) |
| FROM Orders; |
| ==============================================|
| |
| -------
|
| 5 |
===============================================

6.3:

(COUNT) DISTINCT , 3, DISTINCT. DISTINCT , , , COUNT. MAX MIN , SUM AVG , .

COUNT ,

, COUNT , , , , 6.4:

SELECT COUNT (*)
FROM Customers

=============== SQL Execution Log ============
| SELECT COUNT (*) |
| FROM Customers; |
| ==============================================|
| |
| ------- |
| 7 |
==
=============================================

6.4
.

COUNT NULL, , DISTINCT. DISTINCT , COUNT , , NULL . DISTINCT COUNT (*), , . , , , ( , ). , , - , , , COUNT .

( ) ALL, , DISTINCT, . ANSI COUNT, .

ALL * COUNT:

ALL .

ALL NULL.

* , NULL , COUNT; COUNT NULL . (COUNT) NULL rating ( ):

SELECT COUNT (ALL rating)
FROM Customers;

,

, . , , . ( , DISTINCT .) , , ( blnc) . , . :

SELECT MAX (blnc + amt)
FROM Orders;

blnc amt , . , , . , . , , .

, SQL, , 7.

GROUP BY

GROUP BY , . SELECT. , , . , MAX (amt) snum. GROUP BY, , :

SELECT snum, MAX (amt)
FROM Orders
GROUP BY snum;

6.5.

=============== SQL Execution Log ==============
| SELECT snum, MAX (amt) |
| FROM Orders |
| GROUP BY snum; |
| =============================================== |
| snum |
| ------ -------- |
| 1001 767.19 |
| 1002 1713.23 |
| 1003 75.75 |
| 1014 1309.95 |
| 1007 1098.16 |
====================
=============================

6.5. .

GROUP BY , . , snum, MAX . , GROUP BY, , , , . , . GROUP BY . , , . , , MAX , :

SELECT snum, odate, MAX (amt)
FROM Orders
GROUP BY snum, odate;

6.6.

=============== SQL Execution Log ==============
| SELECT snum, odate, MAX (amt) |
| FROM Orders |
| GROUP BY snum, odate; |
| =============================================== |
| snum odate |
| ------ ---------- -------- |
| 1001 10/03/1990 767.19 |
| 1001 10/05/1990 4723.00 |
| 1001 10/06/1990 9891.88 |
| 1002 10/03/1990 5160.45 |
| 1002 10/04/1990 75.75 |
| 1002 10/06/1990 1309.95 |
| 1003 10/04/19
90 1713.23 |
| 1014 10/03/1990 1900.10 |
| 1007 10/03/1990 1098.16 |
=================================================

6.6.

, , , , .

HAVING

, , , $3000.00. WHERE ( , ), , . , - :

SELECT snum, odate, MAX (amt)
FROM Oreders
WHERE MAX (amt) > 3000.00
GROUP BY snum, odate;

ANSI. $3000.00, HAVING. HAVING , , WHERE . :

SELECT snum, odate, MAX (amt)
FROM Orders
GROUP BY snum, odate
HAVING MAX (amt) > 3000.00;

6.7.

=============== SQL Execution Log ==============
| SELECT snum, odate, MAX (amt) |
| FROM Orders |
| GROUP BY snum, odate |
| HAVING MAX (amt) > 3000.00; |
| =============================================== |
| snum odate |
| ------ ---------- -------- |
| 1001 10/05/1990 4723.00 |
| 1001 10/06/1990 9891.88 |
| 1002 10/03/1990 5160.45 |
===================================
==============

6.7.

HAVING , SELECT, GROUP BY. . :

SELECT snum, MAX (amt)
FROM Orders
GROUP BY snum
HAVING odate = 10/03/1988;

date HAVING, ( ) . , HAVING GROUP BY. :

SELECT snum, MAX (amt)
FROM Orders
WHERE odate = 10/03/1990
GROUP BY snum;

// Interbase:

WHERE odate = CAST('10/03/1988' AS DATE)

6.8.

=============== SQL Execution Log ==============
| SELECT snum, odate, MAX (amt) |
| FROM Orders |
| GROUP BY snum, odate; |
| =============================================== |
| snum |
| ------ -------- |
| 1001 767.19 |
| 1002 5160.45 |
| 1014 1900.10
|
| 1007 1098.16 |
=================================================

6.8: 3
.

odate , , , . , , - , : " 3 ". 7, , .

, HAVING , . , , GROUP BY . , Serres Rifkin:

SELECT snum, MAX (amt)
FROM Orders
GROUP BY snum
HAVING snum IN (1002,1007);

6.9.

=============== SQL Execution Log ==============
| SELECT snum, MAX (amt) |
| FROM Orders |
| GROUP BY snum |
| HAVING snum IN (1002, 1007); |
| =============================================== |
| snum |
| ------ -------- |
| 1002 5160.45
|
| 1007 1098.16 |
=================================================

6.9.
HAVING GROUP BY.

ANSI SQL, . , , . ,

SELECT odate, MAX (SUM (amt))
FROM Orders
GROUP BY odate;

, , ( , , , ). , , SUM odate, MAX , . , GROUP BY , odate.

-. , , . , , , . , . , , , .

GROUP BY. , , . , , .

, , . , HAVING.

, , , , 7, , .

SQL

  1. , 3 .
  2. , , NULL city .
  3. , .
  4. , , G.
  5. , .
  6. , , . ( , .)

(. A .)

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

SQL , . , , , , SQL , .

, , , . SQL . SELECT, . , , , . :

SELECT snum, sname, city, comm * 100
FROM Salespeople;

7.1.

=============== SQL Execution Log ============
| SELECT snum, sname, city, comm * 100 |
| FROM Salespeople; |
| ==============================================|
| snum sname city |
| ------ --------- ----------- --------- |
| 1001 Peel London 12.000000 |
| 1002 Serres San Jose 13.000000 |
| 1004 Motika London 11.000000 |
| 1007 Rifkin Barcelona 15.000000 |
| 1003 Axelrod New York 10.000000 |
===============================================


7.1
. .

(.. ), . , . , , , SELECT . , . , , , .

'A', , , 1. SELECT , . , , . 1 + 2 SELECT, 'A' + 'B'; , 'A' 'B' , .

, .

(%). , , , (  7.2)

SELECT snum, sname, city, ' % ', comm * 100
FROM Salespeople;

=============== SQL Execution Log ============
| SELECT snum, sname, city, '%', comm * 100 |
| FROM Salespeople; |
| ==============================================|
| snum sname city |
| ------ -------- ----------- ---- --------- |
| 1001 Peel London % 12.000000 |
| 1002 Serres San Jose % 13.000000 |
| 1004 Motika London % 11.000000 |
|
1007 Rifkin Barcelona % 15.000000 |
| 1003 Axelrod New York % 10.000000 |
===============================================

7.2.
.

, . , . , , . , , . (. 7.3) :

SELECT ' For ', odate, ', there are ',
COUNT (DISTINCT onum), 'orders.'
FROM Orders
GROUP BY odate;

=============== SQL Execution Log ==============
| SELECT 'For', odate, ', ' there are ' , |
| COUNT (DISTINCT onum), ' orders ' |
| FROM Orders |
| GROUP BY odate; |
| =============================================== |
| odate |
| ------ ---------- --------- ------ ------- |
| For 10/03/1990 , there are 5 orders. |
| For 10/04/1990 , there are 2 orders. |
| For 10/05/1990 , there are 1 orders. |
| For 10/06/1990 , there are 2 orders.
|
=================================================

7.3. , ,
.

, 5 , , , , ( UNION, 14). , , . , .

, SQL, (, Report Writer), , . SQL , . SQL , , . , , , , SQL, . , , SQL.

, , , , - . SQL ORDER BY, . . , GROUP BY, (ASC) (DESC) . . , ( cnum ):

SELECT *
FROM Orders
ORDER BY cnum DESC;

7.4.

=============== SQL Execution Log ==============
| SELECT * |
| FROM Orders |
| ORDER BY cnum DESC; |
| =============================================== |
| onum amt odate cnum snum |
| ------ -------- ---------- ----- ----- |
| 3001 18.69 10/03/1990 2008 1007 |
| 3006 1098.16 10/03/1990 2008 1007 |
| 3002 1900.10 10/03/1990 2007 1004 |
| 3008 4723.00 10/05/1990 2006 1001 |
| 3011 9891.88 10/06/1990 2006 1001 |
| 3007 75.75 10/04/1990 2004 1002 |
| 3010 1309.95 10/06/1990 2004 1002 |
| 3
005 5160.45 10/03/1990 2003 1002 |
| 3009 1713.23 10/04/1990 2002 1003 |
| 3003 767.19 10/03/1990 2001 1001 |
=================================================

7.4.
.

, , amt, cnum. ( 7.5):

SELECT *
FROM Orders
ORDER BY cnum DESC, amt DESC;

=============== SQL Execution Log ==============
| SELECT * |
| FROM Orders |
| ORDER BY cnum DESC, amt DESC; |
| =============================================== |
| onum amt odate cnum snum |
| ------ -------- ---------- ----- ----- |
| 3006 1098.16 10/03/1990 2008 1007 |
| 3001 18.69 10/03/1990 2008 1007 |
| 3002 1900.10 10/03/1990 2007 1004 |
| 3011 9891.88 10/06/1990 2006 1001 |
| 3008 4723.00 10/05/1990 2006 1001 |
| 3010 1309.95 10/06/1990 2004 1002 |
| 3007 75.75 10/04/1990 2004 1002 |
| 3005 5160.45 10/03/1990 2003 1002 |
| 3009 1713.23 10/04/1990 2002 1003 |
| 3003 767.19 10/03/1990 2001 1001 |
===============================
==================

7.5. .

ORDER BY . , , SELECT. ANSI, , , . , , :

SELECT cname, city
FROM Customers
GROUP BY cnum;

cnum , GROUP BY , . , , ( SELECT) , ORDER BY, .

ORDER BY , GROUP BY . , ORDER BY . ORDER BY. , , :

SELECT snum, odate, MAX (amt)
FROM Orders
GROUP BY snum, odate
ORDER BY snum;

7.6.

=============== SQL Execution Log ==============
| SELECT snum, odate, MAX (amt) |
| FROM Orders |
| GROUP BY snum, odate |
| ORDER BY snum ; |
| =============================================== |
| snum odate amt |
| ----- ---------- -------- |
| 1001 10/06/1990 767.19 |
| 1001 10/05/1990 4723.00 |
| 1001 10/05/1990 9891.88 |
| 1002 10/06/1990 5160.45 |
| 1002 10/04/1990 75.75 |
| 1002 10/03/1990 1309.95 |
| 1003 10/04/1990 1713.23 |
| 1004 10/03/1990 1900.10
|
| 1007 10/03/1990 1098.16 |
=================================================

7.6:

, .

, . , . , SELECT , ORDER BY 1, . , , , ( 7.7):

SELECT sname, comm
FROM Salespeople
GROUP BY 2 DESC;

=============== SQL Execution Log ============
| (SELECT sname, comm |
| FROM Salespeople |
| ORDER BY 2 DESC; |
| ============================================= |
| sname comm |
| -------- -------- |
| Peel 0.17
|
| Serres 0.13 |
| Rifkin 0.15 |
===============================================

7.7:

ORDER BY GROUP BY . , , SELECT , GROUP BY, . , , , 7.8:

SELECT snum, COUNT (DISTINCT onum)
FROM Orders
GROUP BY snum
ORDER BY 2 DESC;

=============== SQL Execution Log ==============
| SELECT snum, odate, MAX (amt) |
| FROM Orders |
| GROUP BY snum |
| ORDER BY 2 DESC; |
| =============================================== |
| snum |
| ----- ---------- |
| 1001 3 |
| 1002 3 |

| 1007 2 |
| 1003 1 |
| 1004 1 |
=================================================

7.8
. .

, , ; . , ANSI SQL, , :

SELECT snum, COUNT (DISTINCT onum)
FROM Orders
GROUP BY snum
GROUP BY COUNT (DISTINCT onum) DESC;

!

NULL

(NULL) , , , . , ANSI . .

, , . : , 10 . , , , , .

.

, . , ORDER BY . , .

. , , , ORDER BY.

, , , , , . 8.

SQL

    1. , 12% . , , , .
    2. , . :
      For the city (city), the highest rating is: (rating).
    3. , . (rating) .
    4. , .

(. A .)

    SQL  

, , Sequence diagram - - , . .




 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