How to use Count in MySQL Query - MySQL Developer Tutorial

How to use Count in MySQL Query

Let's say if you need to get the count of all the rows from MySQL table, you can use COUNT Function.

Let's create our sample table customer and then insert some records before we run different scenarios for Count function in MySQL.


CREATE TABLE `customer` (
  `idcustomer` int,
  `firstname` varchar(50)  NULL,
  `lastname` varchar(30)  NULL,
  `age` int(11) DEFAULT NULL,
  `phonenumber` char(11) DEFAULT NULL,
  `dob` date DEFAULT NULL,
  `gender` char(1) NOT NULL
) ;

insert into customer(idcustomer,firstname,lastname,age,phonenumber,dob,gender)
values
(1,null,'Ali',39,'505-4141969','1980-01-01','M'),
(2,'Aamir','Naz',39,'505-4141969','1980-01-01','M'),
(3,'Aamir','Shahzad',39,'505-4141900','1980-01-01','M'),
(4,'Aamir1','Shahzad',39,'505-4141900','1980-01-01','M');


Get Total Rows from a Table: 

To get the total row count from a table, you can use below syntax.

Select count(*) From YourTableName; 


Now if we want to get the row count from our customer table, we will use below query.

Select count(*) from customer;


MySQL Tutorial- Get Row Count by using Count function in MySQL



Get Count of values in Table Column : 

Let's say if you want to get the count of values in column of a table, you can use below syntax, Remember that it will ignore the Null values in the Count.


Select count(columnName) from YourTableName;


Let's say if want to get count of values in firstName in customer table, we can use below statement, As there is one Null value, the count will ignore that and will return only 3 out of 4 rows.


Select count(firstname) from customer;




Get Distinct Count : 

If you are interested to get Distinct ( Different) values count, you can use Distinct with Count function in MySQL.
In below example, if we want to get distinct values for firstname column in customer table. we can use below script. Notice that it will not consider the Null values in Count.


Select count(distinct firstname)  from customer;



MySQL Distinct Count from Column Table






5 comments:

  1. Hey there, I need an Statistics Homework Help expert to help me understand the topic of piecewise regression. In our lectures, the concept seemed very hard, and I could not understand it completely. I need someone who can explain to me in a simpler way that I can understand the topic. he/she should explain to me which is the best model, the best data before the model and how to fit the model using SPSS. If you can deliver quality work then you would be my official Statistics Assignment Help partner.

    ReplyDelete
  2. I don’t have time to look for another expert and therefore I am going to hire you with the hope that I will get qualityeconomics assignment help. .Being aneconomics homework help professor I expect that your solutions are first class. All I want to tell you is that if the solutions are not up to the mark I am going to cancel the project.


    ReplyDelete
  3. That is a huge number of students. Are they from the same country or different countries? I also want your math assignment help. I want to perform in my assignments and since this is what you have been doing for years, I believe you are the right person for me. Let me know how much you charge for your math homework help services.

    ReplyDelete
  4. Hello. Please check the task I have just sent and reply as soon as possible. I want an adjustment assignment done within a period of one week. I have worked with an Accounting Homework Help tutor from your team and therefore I know it’s possible to complete it within that period. Let me know the cost so that I can settle it now as your Accounting Assignment Help experts work on it.

    ReplyDelete