How To Write MySQL Count

How To Write MySQL Count

October 16, 2019
Oracle DBA
oracle dba oracle database management database management system database oracle performance tuning oracle sql learn sql learn personal injury attorney new york mesothelioma lawyers mesothelioma lawyers new york mesothelioma lawyer the stock market plumbing retirement planning barclays stock broker financial advisor personal retirement financial advisor

This online tutorial is based on examples to make it easier to follow. Function Count in MySQL returns summary of value lines. The most basic select syntax is:

SELECT COUNT(<column_name>) 
 FROM <table_name>;

MySQL count will count all table lines when no group column has declared in the Group By keyword as on the following example where we will count all lines in table CUSTOMERS. It is important to know that using MySQL SELECT on a table you would need to add the schema name too in front of the table name. You should note that we are using TEST schema table.

SELECT COUNT(*)
 FROM test.customers;

database software database personal injury attorney new york mesothelioma lawyers mesothelioma lawyers new york mesothelioma lawyer the stock market plumbing retirement planning barclays stock broker financial advisor personal retirement financial advisor

As you see on the output above the MySQL Count function returned in one line the customers amount that is 4444 lines.

Now in the following example we will count the lines per FORENAME. You may have noticed that in front of column names is word cus. This prefix is called as table alias it helps us to point to the correct table column. The table alias is defined after the table name.

SELECT cus.forename, COUNT(*)  
  FROM test.customers cus 
 GROUP BY cus.forename;

database software database personal injury attorney new york mesothelioma lawyers mesothelioma lawyers new york mesothelioma lawyer the stock market plumbing retirement planning barclays stock broker financial advisor personal retirement financial advisor

In the output above you can see that we have not showing all the result of 4444 customer forenames but only the first 3 lines. On the output you see that we got 111 Emma customers, 2 John and 11 Sean forenames.

Leave a Reply

Your email address will not be published. Required fields are marked *

*