Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/SQL/How do you count occurrences in a column in SQL?

How do you count occurrences in a column in SQL?

SQLeasy2 min read

Reviewed by Aditya Kumar · Last reviewed 2026-08-08

Group by the column and count. That gives one row per distinct value with its frequency. The COUNT variants differ, and the difference is the interview question COUNT( ) counts rows, including rows…

🤖 Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
487
questions in SQL
Difficulty Split
130E|271M|86H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
Globant
Key Concepts Tested
sql

Why This Question Matters

This easy-level SQL question appears frequently in data engineering interviews at companies like Globant. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (sql) will help you answer variations of this question confidently.

How to Approach This

Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.

Expert Answer
314 wordsIncludes code

Group by the column and count. That gives one row per distinct value with its frequency.

SELECT status, COUNT(*) AS occurrences
FROM   orders
GROUP  BY status
ORDER  BY occurrences DESC;

The COUNT variants differ, and the difference is the interview question

  • COUNT(*) counts rows, including rows where every column is NULL.
  • COUNT(column) counts rows where that column is not NULL. This is the one people get wrong.
  • COUNT(DISTINCT column) counts distinct non-NULL values.
  • So on a column with 100 rows of which 10 are NULL, COUNT(*) returns 100 and COUNT(status) returns 90. Subtracting the two is the idiomatic way to count NULLs:

    SELECT COUNT(*) - COUNT(status) AS null_count FROM orders;
    

    Note also that GROUP BY treats all NULLs as a single group, even though NULL does not equal NULL in a comparison — a deliberate exception in the standard.

    Counting one specific value

    SELECT COUNT(*) FROM orders WHERE status = 'shipped';
    

    Counting several conditions in one pass

    Rather than running separate queries, count conditionally. Because COUNT ignores NULLs, a CASE with no ELSE acts as a filter:

    SELECT COUNT(CASE WHEN status = 'shipped'   THEN 1 END) AS shipped,
           COUNT(CASE WHEN status = 'cancelled' THEN 1 END) AS cancelled
    FROM   orders;
    

    PostgreSQL and SQLite support the cleaner COUNT(*) FILTER (WHERE status = 'shipped'). SUM(CASE WHEN ... THEN 1 ELSE 0 END) works everywhere and is equivalent.

    Performance

    An index on the grouped column lets the engine use an index scan and, in some engines, skip the sort. COUNT(DISTINCT ...) is markedly more expensive than COUNT(*) because it must deduplicate; at large scale, approximate functions such as APPROX_COUNT_DISTINCT or HyperLogLog trade a small error for a large speedup.

    In the interview, also mention that COUNT(1) and COUNT(*) are identical in every major engine — the supposed performance difference is a myth.

    ⚡
    Pro Tip

    Red Flag: COUNT(*) when you need non-null count—different semantics. Pro-Move: 'We used COUNT(CASE WHEN status='active' THEN 1 END) for conditional count in single pass.'

    Want all answers as a PDF for offline study?
    Seven focused volumes with 750+ in-depth answers — Answer Vault →

    Related SQL Questions

    mediumWrite an SQL query to find the second-highest salary from an employee table.FreemediumDemonstrate the difference between DENSE_RANK() and RANK()FreemediumDiscuss differences between ROW_NUMBER(), RANK(), and DENSE_RANK(), and provide examples from your projects.FreemediumExplain the differences between Data Warehouse, Data Lake, and Delta LakeFreemediumExplain the differences between Repartition and Coalesce. When would you use each?Free

    Level up your prep

    Recommended
    Educative
    Educative Unlimited

    800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

    Start learning →

    Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.

    According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.

    ← Back to all questionsMore SQL questions →
    Categories
    All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
    By Company
    AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
    Interview Guides
    All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
    Products
    AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
    Company
    About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
    © 2026 DataEngPrep.tech. All rights reserved.
    AboutBlogContactDisclaimer