SQL Count | Count functions in SQL - sql - sql tutorial - learn sql




What is count in SQL

SQL count query
  • SQL COUNT function is the simplest function and very useful in counting the number of records, which are expected to be returned by a SELECT statement.
  • The COUNT () function will return the number of rows that matches a specified criteria.
  • The COUNT function in SQL will be used to calculate the number of rows returned from the SQL statement.
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Syntax

  • The syntax for the COUNT function is,
                                     SELECT COUNT (<expression>)
                                     FROM "table_name";
  • <expression> can be a column name, an arithmetic operation, or a star (*). When we use COUNT(*), we mean "count everything."
  • It is also possible to have one or more columns.
  • In addition to the COUNT function in the SELECT statement.
  • In those cases, these columns need to be part of the GROUP BY clause as well:
  • COUNT is often combined with DISTINCT to calculate the number of unique values.
  • The syntax for this is as follows:
SELECT COUNT (DISTINCT <expression>)
FROM "table_name";

Examples

  • We use the following table for our examples.

Table Store_Information

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Example 1: Simple COUNT operation

  • To find the number of rows in this table
SELECT COUNT(Store_Name)
                           FROM Store_Information;
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

COUNT (Store_Name)
4
  • Note:
  • We can also use COUNT(*) instead of COUNT(Store_Name).
  • In this case the two will generate the same answer because none of the values in the Store_Name field is NULL
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Example 2: COUNT function with a GROUP BY clause

SQL count group by
  • To get the number of records for each store, we type in,
SELECT Store_Name, COUNT(*) FROM Store_Information GROUP BY Store_Name;
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

Store_Name COUNT(*)
Los Angeles 2
San Diego 1
Boston 1

Example 3: Use COUNT with DISTINCT

  • COUNT and DISTINCT can be used together in a statement to retrieve the number of distinct entries in a table.
  • For example, if we want to find out the number of distinct stores in the Store_Information table,
                                SELECT COUNT (DISTINCT Store_Name)
                                FROM Store_Information;
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Result:

COUNT (DISTINCT Store_Name)
3

SQL Count

SQL Count

This tutorial provides more the basic needs and informations on sql tutorial , pl sql tutorial , mysql tutorial , sql server , sqlcode , sql queries , sql , sql formatter , sql join , w3schools sql , oracle tutorial , mysql , pl sql , learn sql , sql tutorial for beginners , sql server tutorial , sql query tutorial , oracle sql tutorial , t sql tutorial , ms sql tutorial , database tutorial , sql tutorial point , oracle pl sql tutorial , oracle database tutorial , oracle tutorial for beginners , ms sql server tutorial , sql tutorial pdf

Related Searches to Count functions in SQL

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×