मुख्य सामग्री पर जाएँ

COUNT() SQL FUNCTION

COUNT() lets you count the number of rows that match certain conditions. Learn how to use it in this tutorial.
· 3 मि॰ पढ़ें

AI के साथ खोजें

यह कहना आवश्यक नहीं कि पंक्तियाँ या मान गिनना डेटा विश्लेषण का एक अहम हिस्सा है। इसलिए इसमें आश्चर्य नहीं कि SQL के पास इसके लिए अपना फ़ंक्शन है। चाहे आप डुप्लिकेट पहचान रहे हों, समूह के कुल निकाल रहे हों, या डेटा फ़िल्टर कर रहे हों, COUNT() फ़ंक्शन आपके काम आता है।

इस लेख में, मैं दिखाऊँगा कि COUNT() कितने तरीकों से उपयोगी है—इसके मूल सिंटैक्स से लेकर GROUP BY और HAVING के साथ अधिक रोचक उपयोग मामलों तक। साथ ही, हमारे SQL Associate Certification में नामांकन करने पर भी विचार करें—यह सीखने और आपके रिज़्यूमे, दोनों के लिए शानदार है। 

SQL में COUNT() फ़ंक्शन क्या है?

COUNT() फ़ंक्शन उन पंक्तियों की संख्या लौटाता है जो किसी मानदंड से मेल खाती हों।

SQL COUNT() सिंटैक्स

COUNT() का मूल सिंटैक्स इस प्रकार है।

SELECT COUNT(column_name)
FROM table_name;

सिंटैक्स के अलग-अलग रूप अलग उद्देश्यों को हासिल करते हैं।

  • COUNT(*) सिंटैक्स हमें किसी तालिका में पंक्तियों की कुल संख्या गिनने देता है

  • COUNT(DISTINCT column) सिंटैक्स हमें किसी कॉलम में विशिष्ट (यूनिक) मानों की संख्या गिनने देता है 

  • COUNT(CASE WHEN condition THEN column END) सिंटैक्स हमें उन मानों की संख्या गिनने देता है जो शर्तों को पूरा करते हों।

COUNT(), used with GROUP BY, is useful for counting the number of rows that belong to each group.

COUNT(), used with HAVING, is useful for filtering groups according to the number of rows they have.

We will illustrate these with the examples below.

SQL COUNT() फ़ंक्शन के उदाहरण

इस ट्यूटोरियल का कोड ऑनलाइन चलाएं और संपादित करें

कोड चलाएं

COUNT(*) से पंक्तियों की संख्या गिनना

The table called products contains all the products a company sells. The COUNT(*) clause allows us to calculate the number of rows in the table. 

SELECT
    COUNT(*) AS number_of_rows
FROM products

number_of_rows

321

Counting unique values with COUNT(DISTINCT …)

The COUNT(DISTINCT column) syntax allows us to count the number of unique values in a column.

For example, each product has an associated brand in the products table. We can count the number of unique products and brands in the table.

SELECT
    COUNT(DISTINCT product_id) AS unique_product_count,
    COUNT(DISTINCT brand_id) AS unique_brand_count
FROM products

unique_product_count

unique_brand_count

321

9

Count rows that match a condition using COUNT() with CASE WHEN 

The COUNT(CASE WHEN condition THEN column END) syntax allows us to calculate the number of rows that match a condition.

For example, in the products table, each product has a list_price. We can calculate how many products are “expensive” (having a list price of more than $500) or otherwise.

SELECT
    COUNT(CASE WHEN list_price >= 500 THEN product_id END) AS expensive_product_count,
   COUNT(CASE WHEN list_price < 500 THEN product_id END) AS cheap_product_count
FROM products

expensive_product_count

cheap_product_count

213

108

Count rows in groups using COUNT() with GROUP BY

COUNT() can be used with GROUP BY to find the number of rows in each group.

For example, the product table contains bicycle models from 2016 to 2019. To find the distribution of the bicycle, we can use COUNT(product_id) and GROUP BY model_year to count the number of products from each year. 

SELECT
    model_year,
    COUNT(product_id) AS product_count
FROM products
GROUP BY model_year

model_year

product_count

2016

26

2017

85

2018

204

2019

6

Filter for groups using COUNT() with GROUP BY and HAVING

In example 4, we see that COUNT() can be used with GROUP BY. We can use the HAVING statement to filter for groups using the number of rows in that group.

For example, to find the number of years that have less than 50 products, we can use the following syntax. 

SELECT
   model_year
FROM products
GROUP BY model_year
HAVING COUNT(product_id) < 50

year_with_less_than_50_pdt

2016

2019

Technical Requirements

COUNT() is a function that is available to all versions of modern SQL. DataCamp offers tailored courses to help you master SQL within the context of your chosen database system. Here are some options to get started. I would think Oracle would be the most different since it has some specific functions and additional features. 

Learn More about SQL

Keep learning SQL with DataCamp. I've written a few other tutorials to get you started. I hope you find them helpful.

Also, consider our SQL Associate Certification, which I think looks great on your CV or resume.

Travis Tang

लेखक

Travis Tang

Tiktok में डेटा साइंटिस्ट और GeorgiaTech में मास्टर'स के छात्र के रूप में, मुझे डेटा साइंस के प्रति स्वाभाविक लगाव है। मैंने तकनीक, जोखिम और सतत आत्म-विकास को जोड़ते हुए एक अनोखा करियर मार्ग बनाया है। Merchant Platform में डेटा साइंटिस्ट के रूप में अपनी भूमिका के दौरान, मैंने जटिल डेटा मॉडलों का उपयोग करके जोखिम कम किए और धोखाधड़ी का मुकाबला किया, जिससे हर सप्ताह हजारों डॉलर की बचत हुई। स्व-अध्ययन में अनगिनत घंटे समर्पित कर, मैंने प्रतिष्ठित संस्थानों से प्रमाणपत्र प्राप्त किए और डीप लर्निंग, TensorFlow और सोशल नेटवर्क विश्लेषण जैसे विषयों में दक्षता हासिल की। DataCamp और अन्य शैक्षणिक प्लेटफ़ॉर्म पर, मैं इसी ज्ञान का उपयोग दूसरों को प्रेरित और शिक्षित करने में करता/करती हूं, डेटा साइंस के क्षेत्र में नए रास्ते बनाता/बनाती हूं और यह साबित करता/करती हूं कि दृढ़ संकल्प और सही डेटा के साथ, किसी भी जोखिम को अवसर में बदला जा सकता है।

SQL COUNT() FAQs

Can COUNT() count only specific rows?

Yes, you can use a WHERE clause with COUNT() to count only rows that meet specific criteria. For example:

SELECT COUNT(*) FROM employees WHERE department = 'Sales';
What is the difference between COUNT(*) and COUNT(column_name)?

COUNT(*) counts all rows in a table, including rows with NULL values. COUNT(column_name) counts only the rows where the specified column is not NULL.

Can I count distinct values using COUNT()?

Yes, you can count unique values in a column by combining COUNT() with the DISTINCT keyword:

SELECT COUNT(DISTINCT department) FROM employees;
How does COUNT() handle duplicate rows?

By default, COUNT(*) counts all rows, including duplicates. To count only unique rows, use COUNT(DISTINCT column_name).

विषय