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