BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

SQL SELECT COUNT

Last Updated: November 22, 2018 by Chaitanya Singh | Filed Under: SQL

The count() function returns the number of rows that matches the given condition.

SQL COUNT() function Syntax

SELECT COUNT (column_name)  
FROM table_name 
WHERE condition;

SQL SELECT COUNT(column_name) Example

SELECT COUNT(column_name) counts the non-null values of column in the table.

Table: STUDENT

STU_ID      STU_NAME      STU_AGE     STU_DEPT
------      --------      --------    --------
1001         Steve          28         CSE
1002         Chaitanya      29         CSE
1003         Rick           27         ECE
1004         Ajeet          28         IT
1005         Robert         26         ME
1006         David          30         CL
1007         Lucy           30         null

Select Count Query:

SELECT COUNT(STU_DEPT)
FROM STUDENT;

Result:

6

The total number of STU_DEPT values in above table are 7 but one of them is null. Since count(column_name) counts non-null values of the given column, thus the output is 6.

SQL SELECT COUNT(*)

SELECT COUNT(*) counts the number of rows in the table.

Consider the same STUDENT table that we have seen in the above example.

Query:

SELECT COUNT(*)
FROM STUDENT;

Output:
7

SQL SELECT COUNT(DISTINCT column_name)

SELECT COUNT(DISTINCT column_name) counts the total number of distinct values of column in the table. Refer this guide – SQL DISTINCT to learn more about SQL SELECT DISTINCT statement.

Query:
Lets take the same table STUDENT that we have taken in the above example.

SELECT COUNT(DISTINCT STU_DEPT)
FROM STUDENT;

Output:
5

There are five distinct values of STU_DEPT in the table so the output is 5.

Top Related Articles:

  1. Introduction to SQL
  2. SQL ORDER BY Clause
  3. SQL SELECT Statement
  4. SQL SELECT AVG() Function
  5. DELETE Query in SQL

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

SQL Tutorial

  • SQL Tutorial
  • SQL Introduction
  • SQL Syntax
  • SQL Data Types

SQL Database

  • SQL CREATE DB
  • SQL DROP DB
  • SQL Rename DB
  • SQL USE DB

SQL Queries

  • SQL Select
  • SQL Select Distinct
  • SQL Select Count
  • SQL Select Top
  • SQL Where
  • SQL AND, OR & NOT
  • SQL Order By
  • SQL Insert Into
  • SQL Insert Into SELECT
  • SQL Select Random
  • SQL Alias
  • SQL NULL Check
  • SQL Update
  • SQL Delete
  • SQL MIN, MAX
  • SQL SUM
  • SQL AVG

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap