Compare and calculate in Where clause : Where « Where Clause « SQL / MySQL






Compare and calculate in Where clause

   
/*
mysql> Drop table Sale;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE Sale
    -> (
    ->    ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->    Name VARCHAR(50) NOT NULL,
    ->    InStock SMALLINT UNSIGNED NOT NULL,
    ->    OnOrder SMALLINT UNSIGNED NOT NULL,
    ->    Reserved SMALLINT UNSIGNED NOT NULL,
    ->    Department ENUM('Classical', 'Popular') NOT NULL,
    ->    Category VARCHAR(20)
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> INSERT INTO Sale (Name,      InStock, OnOrder, Reserved, Department, Cate
gory)
    -> VALUES          ('Bloodshot', 11,      6,       1,        'Popular',  'Ro
ck'),
    ->                 ('Opera',     12,      5,       2,        'Classical','Op
era'),
    ->                 ('Jazz',      13,      4,       3,        'Popular',  'Ja
zz'),
    ->                 ('Music',     4,       3,       4,        'Classical','Da
nce'),
    ->                 ('Violin',    25,      2,       5,        'Classical', NU
LL),
    ->                 ('Toscana',   16,      1,       6,        'Classical', NU
LL),
    ->                 ('Blues',     7,       22,      7,        'Popular',   'B
lues'),
    ->                 ('Pure',      38,      5,       11,       'Popular',   NU
LL),
    ->                 ('Mud',       19,      11,      12,       'Popular',  'Co
untry'),
    ->                 ('Essence',   5,       23,      12,       'Popular', 'New
 Age'),
    ->                 ('Embrace',   21,      12,      14,       'Popular', 'New
 Age'),
    ->                 ('Satie',     42,      17,      15,       'Classical', NU
LL),
    ->                 ('Lake',      23,      47,      28,       'Classical', 'D
ance'),
    ->                 ('Favorites', 34,      15,      12,       'Classical', 'G
eneral'),
    ->                 ('Boheme',    25,      12,       5,       'Classical', 'O
pera'),
    ->                 ('Cantatas',  26,      13,      8,        'Classical', 'G
eneral'),
    ->                 ('Road',      27,      13,      17,       'Popular', 'Cou
ntry'),
    ->                 ('Paris',     18,      25,      10,       'Popular', 'Jaz
z'),
    ->                 ('Woman',     29,      4,        7,       'Popular', 'Blu
es'),
    ->                 ('Bach',      21,      13,      16,       'Classical', 'G
eneral'),
    ->                 ('Opera',     12,      32,      12,       'Classical', 'O
pera'),
    ->                 ('Soul',      13,      30,      14,       'Popular', NULL
),
    ->                 ('Stages',    44,      0,       8,        'Popular', 'Blu
es'),
    ->                 ('Bach',      15,      1,       8,        'Classical', 'G
eneral');
Query OK, 24 rows affected (0.01 sec)
Records: 24  Duplicates: 0  Warnings: 0

mysql> select * from Sale;
+----+-----------+---------+---------+----------+------------+----------+
| ID | Name      | InStock | OnOrder | Reserved | Department | Category |
+----+-----------+---------+---------+----------+------------+----------+
|  1 | Bloodshot |      11 |       6 |        1 | Popular    | Rock     |
|  2 | Opera     |      12 |       5 |        2 | Classical  | Opera    |
|  3 | Jazz      |      13 |       4 |        3 | Popular    | Jazz     |
|  4 | Music     |       4 |       3 |        4 | Classical  | Dance    |
|  5 | Violin    |      25 |       2 |        5 | Classical  | NULL     |
|  6 | Toscana   |      16 |       1 |        6 | Classical  | NULL     |
|  7 | Blues     |       7 |      22 |        7 | Popular    | Blues    |
|  8 | Pure      |      38 |       5 |       11 | Popular    | NULL     |
|  9 | Mud       |      19 |      11 |       12 | Popular    | Country  |
| 10 | Essence   |       5 |      23 |       12 | Popular    | New Age  |
| 11 | Embrace   |      21 |      12 |       14 | Popular    | New Age  |
| 12 | Satie     |      42 |      17 |       15 | Classical  | NULL     |
| 13 | Lake      |      23 |      47 |       28 | Classical  | Dance    |
| 14 | Favorites |      34 |      15 |       12 | Classical  | General  |
| 15 | Boheme    |      25 |      12 |        5 | Classical  | Opera    |
| 16 | Cantatas  |      26 |      13 |        8 | Classical  | General  |
| 17 | Road      |      27 |      13 |       17 | Popular    | Country  |
| 18 | Paris     |      18 |      25 |       10 | Popular    | Jazz     |
| 19 | Woman     |      29 |       4 |        7 | Popular    | Blues    |
| 20 | Bach      |      21 |      13 |       16 | Classical  | General  |
| 21 | Opera     |      12 |      32 |       12 | Classical  | Opera    |
| 22 | Soul      |      13 |      30 |       14 | Popular    | NULL     |
| 23 | Stages    |      44 |       0 |        8 | Popular    | Blues    |
| 24 | Bach      |      15 |       1 |        8 | Classical  | General  |
+----+-----------+---------+---------+----------+------------+----------+
24 rows in set (0.01 sec)

mysql> SELECT Name, InStock, OnOrder, Reserved
    -> FROM Sale
    -> WHERE (InStock+OnOrder-Reserved)<20
    -> ORDER BY Name;
+-----------+---------+---------+----------+
| Name      | InStock | OnOrder | Reserved |
+-----------+---------+---------+----------+
| Bach      |      15 |       1 |        8 |
| Bach      |      21 |      13 |       16 |
| Bloodshot |      11 |       6 |        1 |
| Embrace   |      21 |      12 |       14 |
| Essence   |       5 |      23 |       12 |
| Jazz      |      13 |       4 |        3 |
| Mud       |      19 |      11 |       12 |
| Music     |       4 |       3 |        4 |
| Opera     |      12 |       5 |        2 |
| Toscana   |      16 |       1 |        6 |
+-----------+---------+---------+----------+
10 rows in set (0.00 sec)


*/

Drop table Sale;


CREATE TABLE Sale
(
   ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name VARCHAR(50) NOT NULL,
   InStock SMALLINT UNSIGNED NOT NULL,
   OnOrder SMALLINT UNSIGNED NOT NULL,
   Reserved SMALLINT UNSIGNED NOT NULL,
   Department ENUM('Classical', 'Popular') NOT NULL,
   Category VARCHAR(20)
);


INSERT INTO Sale (Name,      InStock, OnOrder, Reserved, Department, Category)
VALUES          ('Bloodshot', 11,      6,       1,        'Popular',  'Rock'),
                ('Opera',     12,      5,       2,        'Classical','Opera'),
                ('Jazz',      13,      4,       3,        'Popular',  'Jazz'),
                ('Music',     4,       3,       4,        'Classical','Dance'),
                ('Violin',    25,      2,       5,        'Classical', NULL),
                ('Toscana',   16,      1,       6,        'Classical', NULL),
                ('Blues',     7,       22,      7,        'Popular',   'Blues'),
                ('Pure',      38,      5,       11,       'Popular',   NULL),
                ('Mud',       19,      11,      12,       'Popular',  'Country'),
                ('Essence',   5,       23,      12,       'Popular', 'New Age'),
                ('Embrace',   21,      12,      14,       'Popular', 'New Age'),
                ('Satie',     42,      17,      15,       'Classical', NULL),
                ('Lake',      23,      47,      28,       'Classical', 'Dance'),
                ('Favorites', 34,      15,      12,       'Classical', 'General'),
                ('Boheme',    25,      12,       5,       'Classical', 'Opera'),
                ('Cantatas',  26,      13,      8,        'Classical', 'General'),
                ('Road',      27,      13,      17,       'Popular', 'Country'),
                ('Paris',     18,      25,      10,       'Popular', 'Jazz'),
                ('Woman',     29,      4,        7,       'Popular', 'Blues'),
                ('Bach',      21,      13,      16,       'Classical', 'General'),
                ('Opera',     12,      32,      12,       'Classical', 'Opera'),
                ('Soul',      13,      30,      14,       'Popular', NULL),
                ('Stages',    44,      0,       8,        'Popular', 'Blues'),
                ('Bach',      15,      1,       8,        'Classical', 'General');
  
select * from Sale;
  
SELECT Name, InStock, OnOrder, Reserved
FROM Sale
WHERE (InStock+OnOrder-Reserved)<20
ORDER BY Name;
           
         
    
    
  








Related examples in the same category

1.Not equal in where
2.Getting the List of Products That Are on Catalog Promotion
3.Using Where Conditions
4.WHERE Clause Comparisons
5.Combining WHERE Conditions
6.Calculation in WHERE clause
7.Do Calculation in Where and order
8.Where clause: nested conditions
9.Where clause: calculation and equal condition
10.Where clause: compare
11.Where clause: XOR
12.Use where clause the narrow down results
13.Use CURRENT_DATE in where clause
14.WHERE TRUE OR FALSE
15.Change localhost to the name of the machine where you'll be working.
16.WHERE clauses can test multiple conditions.
17.To put first those records where people sent messages to themselves
18.Using a WHERE clause that matches up values in the author ID column
19.Add a WHERE clause that looks for NULL values in the book column that is named in the ON clause
20.Add an expression to the WHERE clause that explicitly excludes the reference
21.Adds an additional condition to the WHERE clause and the calculated columns must have a total greater than 20
22.A WHERE clause that contains two expressions (conditions)
23.Searches for surnames that have an e anywhere in the name and then end with an e:
24.Show all records where the color is "Silver" and the price is above 100.00
25.Show records where make is "Krups", "Gaggia" or "DeLonghi" and the model is not "TSK-182" or "EC410"
26.Show all records where the color is not "Silver" or the price is below 100.00
27.Get the number of items for each color where the price exceeds 150.00 and when there is more than 1 item for t
28.Check day name in where clause
29.Compare the results from two statements