Code has been added to clipboard!

Using SQL COUNT

Reading time 1 min
Published Aug 9, 2017
Updated Oct 11, 2019

SQL COUNT: Main Tips

  • This function returns the amount of rows that satisfy the defined criteria.
  • COUNT(DISTINCT) is only supported by Microsoft SQL Server and ORACLE.

Syntax of SQL COUNT

COUNT(col_name)

Returns the amount of values (excluding NULL values) in the defined column:

Example
SELECT COUNT(col_name) FROM tbl_name
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

COUNT(*)

Returns the total amount of data records in the table:

Example
SELECT COUNT(*) FROM tbl_name

COUNT(DISTINCT col_name)

Brings back the amount of values that are distinct (counting duplicate values as one value) from a defined column:

Example
SELECT COUNT(DISTINCT col_name) FROM tbl_name

Demo Database

Below is a section of the customer_orders table – we'll use it as a demo database in our examples:

id customer_id employee_id
20408 2 7
85471 37 3
75864 77 8

SQL COUNT Examples

COUNT(col_name)

In the code example below the statement calculated the amount of orders from a particular customer:

Example
SELECT COUNT(ID) AS OrderFromCustomers FROM customer_orders WHERE customer_id = 2;

COUNT(*)

Here we calculate the full amount of orders:

Example
SELECT COUNT(DISTINCT customer_id) AS NumberOfCustomer FROM customer_orders;

COUNT(DISTINCT col_name)

The following query will return the amount of unique customers:

Example
SELECT COUNT(DISTINCT ID) AS NumberOfCustomer FROM Orders;

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 03/03/2021
3176 People Used
Only 97 Left
Rating
5.0