
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL SELECT products WHERE 'average price per product' < value?
Let us first create a table −
mysql> create table DemoTable848( ProductId int, ProductPrice int ); Query OK, 0 rows affected (1.20 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable848 values(100,30); Query OK, 1 row affected (0.57 sec) mysql> insert into DemoTable848 values(101,50); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable848 values(100,40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable848 values(101,25); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable848 values(100,20); Query OK, 1 row affected (0.31 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable848;
This will produce the following output −
+-----------+--------------+ | ProductId | ProductPrice | +-----------+--------------+ | 100 | 30 | | 101 | 50 | | 100 | 40 | | 101 | 25 | | 100 | 20 | +-----------+--------------+ 5 rows in set (0.00 sec)
Following is the query to select products WHERE 'average price per product' < value. Here, we wanted the average to be less than 35. The same is only valid for corresponding column values with ProductId 100 −
mysql> select ProductId,avg(ProductPrice) from DemoTable848 group by ProductId having AVG(ProductPrice) < 35;
This will produce the following output −
+-----------+-------------------+ | ProductId | avg(ProductPrice) | +-----------+-------------------+ | 100 | 30.0000 | +-----------+-------------------+ 1 row in set (0.00 sec)
- Related Articles
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- SELECT where row value contains string in MySQL?
- MySQL Select where value exists more than once
- Select multiple Book Titles that share the minimum (PRICE) value in MySQL?
- Select from table where value does not exist with MySQL?
- MySQL query to select column where value = one or value = two, value = three, etc?
- MySQL Select Rows where two columns do not have the same value?
- How can I calculate the total value of products from my MySQL product table?
- MySQL select query with multiple WHERE?
- SELECT WHERE IN null in MySQL?
- How to select data in MySQL where a field has a minimum value?
- How do you select from MySQL where last value in a string = x?
- Calculating average value per document with sort in MongoDB?
- MySQL Select where values IN List string?
- MySQL select count by value?

Advertisements