Get the number of items for each color where the price exceeds 150.00 and when there is more than 1 item for t : Where « Where Clause « SQL / MySQL






Get the number of items for each color where the price exceeds 150.00 and when there is more than 1 item for t

       
hat color
mysql>
mysql> CREATE TABLE IF NOT EXISTS product
    -> (
    ->   id             INT     AUTO_INCREMENT  PRIMARY KEY,
    ->   name           CHAR(10)        NOT NULL,
    ->   color          CHAR(10)        NOT NULL,
    ->   price          DECIMAL(6,2)    NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> # insert 5 records into the "product" table
mysql> INSERT INTO product (name, color, price)   VALUES ("Milan", "Blue", 199.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, color, price)   VALUES ("Firenze", "Red", 144.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, color, price)   VALUES ("Vivaldi", "Terracotta", 199.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, color, price)   VALUES ("Vienna", "Blue", 164.99);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO product (name, color, price)   VALUES ("Roma", "Red", 249.99);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> # display all data in the "product" table
mysql> SELECT * FROM product;
+----+---------+------------+--------+
| id | name    | color      | price  |
+----+---------+------------+--------+
|  1 | Milan   | Blue       | 199.99 |
|  2 | Firenze | Red        | 144.99 |
|  3 | Vivaldi | Terracotta | 199.99 |
|  4 | Vienna  | Blue       | 164.99 |
|  5 | Roma    | Red        | 249.99 |
+----+---------+------------+--------+
5 rows in set (0.00 sec)

mysql>
mysql> SELECT color, COUNT(*) AS  num_items_over_150
    -> FROM product WHERE price >= 150.00
    -> GROUP BY color HAVING COUNT(*) > 1;
+-------+--------------------+
| color | num_items_over_150 |
+-------+--------------------+
| Blue  |                  2 |
+-------+--------------------+
1 row in set (0.00 sec)

mysql>
mysql> # delete this sample table
mysql> DROP TABLE IF EXISTS product;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
    
  








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.Compare and calculate in Where clause
8.Do Calculation in Where and order
9.Where clause: nested conditions
10.Where clause: calculation and equal condition
11.Where clause: compare
12.Where clause: XOR
13.Use where clause the narrow down results
14.Use CURRENT_DATE in where clause
15.WHERE TRUE OR FALSE
16.Change localhost to the name of the machine where you'll be working.
17.WHERE clauses can test multiple conditions.
18.To put first those records where people sent messages to themselves
19.Using a WHERE clause that matches up values in the author ID column
20.Add a WHERE clause that looks for NULL values in the book column that is named in the ON clause
21.Add an expression to the WHERE clause that explicitly excludes the reference
22.Adds an additional condition to the WHERE clause and the calculated columns must have a total greater than 20
23.A WHERE clause that contains two expressions (conditions)
24.Searches for surnames that have an e anywhere in the name and then end with an e:
25.Show all records where the color is "Silver" and the price is above 100.00
26.Show records where make is "Krups", "Gaggia" or "DeLonghi" and the model is not "TSK-182" or "EC410"
27.Show all records where the color is not "Silver" or the price is below 100.00
28.Check day name in where clause
29.Compare the results from two statements