How to use the COUNT() Function In MySQL

Query-1

CREATE TABLE product (
  user_id INTEGER PRIMARY KEY,
  product_1 VARCHAR(50),
  product_2 VARCHAR(50),
  price INT
);

INSERT INTO product (product_1, price) VALUES ('rice', 400);
INSERT INTO product (product_2, price) VALUES ('grains', 600);

SELECT COUNT(user_id) FROM product;

Output:

 

 

Query-2

Using the COUNT() function and counting float values.

CREATE TABLE floats (
  user_id INTEGER PRIMARY KEY,
  float_val FLOAT
);


INSERT INTO floats (float_val) VALUES (3.5);
INSERT INTO floats (float_val) VALUES (2.1);
INSERT INTO floats (float_val) VALUES (6.3);
INSERT INTO floats (float_val) VALUES (9.9);
INSERT INTO floats (float_val) VALUES (7.0);

SELECT COUNT(*) FROM floats;

Output :

 

COUNT() Function in MySQL

Count() function in MySQL is used to find the number of indexes as returned from the query selected.

Features 

  1. This function finds the number of indexes as returned from the query selected.
  2. It comes under Numeric Functions.
  3. It accepts only one parameter namely expression.
  4. This function ignores NULL values and doesn’t count them.

Syntax:

COUNT(expression)

  • Parameter values: This method accepts only one parameter as given below:
  • Expression: A specified expression can either be a field or a string-type value.
  • Returns: It returns the number of indexes as returned from the query selected.

Similar Reads

Using the COUNT() Function

Query-1...

Count() With Where Clause

Query-3...

Count() Function with GROUP BY Clause

Using the Count() Function with GROUP BY Clause...

Contact Us